From 0f5738d746a141877e29ef248a323dcdd44cbdf9 Mon Sep 17 00:00:00 2001 From: ShyPike Date: Sun, 28 Mar 2010 22:53:13 +0200 Subject: [PATCH 1/3] Emails should have proper character encodings. Use the email.message library to prepare it properly. Put some accented characters in the email test message. --- sabnzbd/emailer.py | 29 +++++++++++++++++++++++++---- sabnzbd/interface.py | 6 +++--- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/sabnzbd/emailer.py b/sabnzbd/emailer.py index ccff1f4..7168400 100644 --- a/sabnzbd/emailer.py +++ b/sabnzbd/emailer.py @@ -98,11 +98,9 @@ def send(message): logging.error(Ta('error-mailAuth')) return failure + message = _prepare_message(message) try: - if isinstance(message, unicode): - message = message.encode('utf8') - for recipient in cfg.email_to(): - mailconn.sendmail(cfg.EMAIL_FROM.get(), recipient, message) + mailconn.sendmail(cfg.email_from(), cfg.email_to(), message) except: logging.error(Ta('error-mailSend')) return failure @@ -222,3 +220,26 @@ def _decode_file(path): fp.close() return source.decode(encoding) + + +################################################################################ +from email.message import Message +RE_HEADER = re.compile(r'^([^:]+):(.*)') +def _prepare_message(txt): + """ Do the proper message encodings + """ + msg = Message() + msg.set_charset('iso-8859-1') + payload = [] + body = False + for line in txt.encode('latin-1').split('\n'): + if not line: + body = True + if body: + payload.append(line) + else: + m = RE_HEADER.search(line) + if m: + msg.add_header(m.group(1).strip(), m.group(2).strip()) + msg.set_payload('\n'.join(payload), 'iso-8859-1') + return msg.as_string() diff --git a/sabnzbd/interface.py b/sabnzbd/interface.py index 5f50f0e..3cec329 100644 --- a/sabnzbd/interface.py +++ b/sabnzbd/interface.py @@ -2558,9 +2558,9 @@ class ConnectionInfo: pack['download'] = ['action 1', 'action 2'] pack['unpack'] = ['action 1', 'action 2'] - self.__lastmail= emailer.endjob('Test Job', 123, 'unknown', True, - os.path.normpath(os.path.join(cfg.complete_dir.get_path(), '/unknown/Test Job')), - str(123*MEBI), pack, 'my_script', 'Line 1\nLine 2\nLine 3\n', 0) + self.__lastmail= emailer.endjob('T\xe9st Job', 123, 'unknown', True, + os.path.normpath(os.path.join(cfg.complete_dir.get_path(), '/unknown/T\xe9st Job')), + str(123*MEBI), pack, 'my_script', 'Line 1\nLine 2\nLin\xe9 3\n', 0) raise dcRaiser(self.__root, kwargs) @cherrypy.expose From 2fae668276e87ea88557946a28f23f0a57aaabe3 Mon Sep 17 00:00:00 2001 From: ShyPike Date: Sun, 28 Mar 2010 22:53:36 +0200 Subject: [PATCH 2/3] The French email template should have encoding header UTF-8. --- language/email-fr-fr.tmpl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/language/email-fr-fr.tmpl b/language/email-fr-fr.tmpl index d0e14aa..3ffa9a2 100644 --- a/language/email-fr-fr.tmpl +++ b/language/email-fr-fr.tmpl @@ -1,3 +1,4 @@ +#encoding UTF-8 ## ## Template Email pour SABnzbd ## Ceci est un template Cheetah @@ -9,7 +10,7 @@ to: $to from: $from date: $date -subject: SABnzbd du téléchargement $name +subject: SABnzbd SuccèsEchec du téléchargement $name X-priority: 5 X-MS-priority: 5 ## Le contenu du message, la ligne vide est obligatoire ! From 4904c5de6b618e28a5f74315c011bb49bf2d26b4 Mon Sep 17 00:00:00 2001 From: ShyPike Date: Sun, 28 Mar 2010 23:11:47 +0200 Subject: [PATCH 3/3] Change email encoding from Latin-1 to UTF-8. --- sabnzbd/emailer.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sabnzbd/emailer.py b/sabnzbd/emailer.py index 7168400..01b439f 100644 --- a/sabnzbd/emailer.py +++ b/sabnzbd/emailer.py @@ -229,10 +229,10 @@ def _prepare_message(txt): """ Do the proper message encodings """ msg = Message() - msg.set_charset('iso-8859-1') + msg.set_charset('UTF-8') payload = [] body = False - for line in txt.encode('latin-1').split('\n'): + for line in txt.encode('utf-8').split('\n'): if not line: body = True if body: @@ -241,5 +241,5 @@ def _prepare_message(txt): m = RE_HEADER.search(line) if m: msg.add_header(m.group(1).strip(), m.group(2).strip()) - msg.set_payload('\n'.join(payload), 'iso-8859-1') + msg.set_payload('\n'.join(payload), 'UTF-8') return msg.as_string()