Problem to send email in HTML

Get help with all aspects of SABnzbd
Forum rules
Help us help you:
  • Are you using the latest stable version of SABnzbd? Downloads page.
  • Tell us what system you run SABnzbd on.
  • Adhere to the forum rules.
  • Do you experience problems during downloading?
    Check your connection in Status and Interface settings window.
    Use Test Server in Config > Servers.
    We will probably ask you to do a test using only basic settings.
  • Do you experience problems during repair or unpacking?
    Enable +Debug logging in the Status and Interface settings window and share the relevant parts of the log here using [ code ] sections.
Post Reply
Hellnino18
Newbie
Newbie
Posts: 12
Joined: January 9th, 2011, 6:48 pm

Problem to send email in HTML

Post by Hellnino18 »

Hi Guys,

I tried to make a html email template for sabnzbd :

Code: Select all

#encoding UTF-8
##
## Template Email pour SABnzbd
## Ceci est un template Cheetah
## Documentation: http://sabnzbd.wikidot.com/email-templates
##
## Les retours à la ligne et les espaces sont importants !
##
## Entêtes de l'email
Content-Type: text/html; charset="iso-8859-1"
to: $to
from: $from
date: $date
subject: <!--#if $status#-->[SUCCES]<!--#else#-->[ECHEC]<!--#end if#--> du téléchargement : $name
X-priority: 5
X-MS-priority: 5
## Le contenu du message, la ligne vide est obligatoire !

<html>
<body>
Bonjour,
<!--#if $status #-->
<b><font color="#3333FF">[SUCCES] :</font></b> SABnzbd a téléchargé le fichier suivant : <b>"$name"</b><!--#if $msgid=="" then "" else "(newzbin #" + $msgid + ")"#-->.
<!--#else#-->
<b><font color="#FF0000">[ECHEC] :</font></b> SABnzbd n'a pas téléchargé le fichier suivant : <b>"$name"</b><!--#if $msgid=="" then "" else "(newzbin #" + $msgid + ")"#-->.
<!--#end if#-->
<b>Terminé le : </b><i><font color="#3333FF">$end_time</font></i>
<b>Taille du Téléchargement : </b><i><font color="#3333FF">$size</font></i>
<!--#if $cat=="tv" #-->
<b>Catégorie : </b><i>Séries</i>
<!--#end if#-->


<b><u>Résultat du téléchargement</u></b>
<!--#for $stage in $stages #-->
<b>* Etape $stage</b> :<!--#slurp#-->
<!--#for $result in $stages[$stage]#-->
    <i>$result</i> <!--#slurp#-->
<!--#end for#-->
<!--#end for#-->
<!--#if $script!="" #-->
Sortie du script utilisateur "<b><i>$script</i></b>" :
$script_output
<!--#end if#-->

A bienotôt !
</body>
</html>
I think everything is fine, but when I receive it into my mailbox, html isn't interpreted. If i take a look into the source code I see that there is the mime header "Content-Type" twice. Like that :

Code: Select all

MIME-Version: 1.0
Content-Type: text/plain; charset="iso-8859-1"
Content-Type: text/html; charset="iso-8859-1"
Is it possible to remove the first occurrence with the text/plain, because I think it's the problem ?

Thanks in advance.
User avatar
shypike
Administrator
Administrator
Posts: 19774
Joined: January 18th, 2008, 12:49 pm

Re: Problem to send email in HTML

Post by shypike »

I'll look into it.
The text/plain header isn't added explicitly added by SABnzbd,
so it might come from the email library instead.
Might be a bit tricky to solve this.
Hellnino18
Newbie
Newbie
Posts: 12
Joined: January 9th, 2011, 6:48 pm

Re: Problem to send email in HTML

Post by Hellnino18 »

Yeah I know it's not SABnzbd that add this explicitely because I have take a look into emailer.py and don't see anythnig about this. I have tried to make like for the Content-Transfer-Encoding in emailer.py and add :

Code: Select all

del msg['Content-Type']
But it doesn't work...
User avatar
shypike
Administrator
Administrator
Posts: 19774
Joined: January 18th, 2008, 12:49 pm

Re: Problem to send email in HTML

Post by shypike »

I suspect that the encode_quopri(msg) call adds it.
I'll run it through the debugger when I can find the time.
Hellnino18
Newbie
Newbie
Posts: 12
Joined: January 9th, 2011, 6:48 pm

Re: Problem to send email in HTML

Post by Hellnino18 »

Ok ty Shypike, I just need to wait that you test :)
User avatar
shypike
Administrator
Administrator
Posts: 19774
Joined: January 18th, 2008, 12:49 pm

Re: Problem to send email in HTML

Post by shypike »

Replace the _prepare_message() function with this:

Code: Select all

def _prepare_message(txt):
    """ Apply the proper encoding to all email fields.
        The body will be Latin-1, the headers will be 'quopri'd when necessary.
    """
    def plain(val):
        """ Return True when val is plain ASCII """
        try:
            val.decode('ascii')
            return True
        except:
            return False

    # Use Latin-1 because not all email clients know UTF-8.
    code = 'ISO-8859-1'

    msg = Message()
    payload = []
    body = False
    header = False
    for line in txt.encode(code, 'replace').split('\n'):
        if header and not line:
            body = True
        if body:
            payload.append(line)
        else:
            m = RE_HEADER.search(line)
            if m:
                header = True
                keyword = m.group(1).strip()
                value = m.group(2).strip()
                if plain(value):
                    # Don't encode if not needed, because some email clients
                    # choke when headers like "date" are encoded.
                    msg.add_header(keyword, value)
                else:
                    header = Header(value, code)
                    msg[keyword] = header

    msg.set_payload('\n'.join(payload), code)

    # Check for proper encoding, else call it explicitly
    if not msg.has_key('Content-Transfer-Encoding'):
        encode_quopri(msg)

    return msg.as_string()
Previous testing did not reveal that the message body is already print-quoted and that the current code doubles up the encoding.
Apparently not hurtful for plain text, but it's killing for HTML.
Also the call to msg.set_charset() is not needed and premature (causing the double header).

BTW: your template needs some work too.
You need to add lots of codes, because the normal linefeeds have no meaning in HTML.

I'll add a solution to 0.6.0 as we have no plans for a new 0.5.x
This will work for you I think, but it probably isn't a final solution.
Last edited by shypike on January 10th, 2011, 4:25 pm, edited 1 time in total.
Hellnino18
Newbie
Newbie
Posts: 12
Joined: January 9th, 2011, 6:48 pm

Re: Problem to send email in HTML

Post by Hellnino18 »

Thanks for the fast response Shypike, it's works, now I need to finish my script but it's okay. Thanks, because your response were very fast. (and sorry for my english, because I'm french) :)
Hellnino18
Newbie
Newbie
Posts: 12
Joined: January 9th, 2011, 6:48 pm

Re: Problem to send email in HTML

Post by Hellnino18 »

Hi,

For the people who are interessting with this idea, I have finished my script for html email in French (I can make the same for other country) or you just need to adapt the same structure with your language.

This is the code for French format :

Code: Select all

#encoding UTF-8
##
## Template Email pour SABnzbd
## Ceci est un template Cheetah
## Documentation: http://sabnzbd.wikidot.com/email-templates
##
## Les retours à la ligne et les espaces sont importants !
##
## Entêtes de l'email
Content-Type: text/html; charset="iso-8859-1"
to: $to
from: $from
date: $date
subject: <!--#if $status#-->[SUCCES]<!--#else#-->[ECHEC]<!--#end if#--> du téléchargement : $name
X-priority: 5
X-MS-priority: 5
## Le contenu du message, la ligne vide est obligatoire !

<html>
        <body>
        Bonjour,<br/><br/><br/>

        <b><u>Résultat du téléchargement</u></b><br/><br/>
        <!--#if $status #-->
                <b>Status : <font color="#33CC33">Succès</font></b><br/><br/>
        <!--#else#-->
                <b>Status : <font color="#FF0000">Echec</font></b><br/><br/>
        <!--#end if#-->

        <!--#if $cat=="tv" #-->
                <b>Catégorie : </b><i>Séries</i><br/><br/>
        <!--#end if#-->

        <b>Nom du fichier : <i>$name</i></b><br/><br/>

        <b>Terminé le : </b><i><font color="#999999">$end_time</font></i><br/>
        <b>Taille du Téléchargement : </b><i><font color="#999999">$size</font></i><br/>

        <br/>

        <!--#for $stage in $stages #-->
                <b>* Etape $stage</b> :<br/><!--#slurp#-->
                <!--#for $result in $stages[$stage]#-->
                    <span style="padding-left: 30px;"><i>$result</i></span><br/><br/> <!--#slurp#-->
                <!--#end for#-->
        <!--#end for#-->

        <!--#if $script!="" #-->
                Sortie du script utilisateur "<b><i>$script</i></b>" :<br/>
                $script_output<br/><br/>
        <!--#end if#-->

        A bientôt !
        <br/>
        </body>
</html>
The result is in attachment.
Last edited by Hellnino18 on January 16th, 2011, 9:40 am, edited 1 time in total.
Post Reply