From 08ac81ed41efcadad93d6976d3b7319d159ba442 Mon Sep 17 00:00:00 2001 From: richard Date: Mon, 1 Feb 2010 03:59:02 +0000 Subject: [PATCH] Attempt to generate more human-readable addresses in email git-svn-id: http://svn.roundup-tracker.org/svnroot/roundup/roundup/trunk@4442 57a73879-2fb5-44c3-a270-3262357dd7e2 --- CHANGES.txt | 2 ++ roundup/mailer.py | 23 +++++++++++++++++------ roundup/roundupdb.py | 8 +++++--- test/test_mailer.py | 24 ++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 9 deletions(-) create mode 100644 test/test_mailer.py diff --git a/CHANGES.txt b/CHANGES.txt index 2262b94..e311524 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -25,6 +25,8 @@ Fixes: - Display 'today' in the account user's timezone, thanks David Wolever - Fix file handle leak in some web interfaces with logging turned on, fixes issue1675845 +- Attempt to generate more human-readable addresses in email, fixes + issue2550632 2009-12-21 1.4.11 (r4413) diff --git a/roundup/mailer.py b/roundup/mailer.py index f1a1af3..729aaea 100644 --- a/roundup/mailer.py +++ b/roundup/mailer.py @@ -9,7 +9,7 @@ from cStringIO import StringIO from roundup import __version__ from roundup.date import get_timezone -from email.Utils import formatdate, formataddr +from email.Utils import formatdate, formataddr, specialsre, escapesre from email.Message import Message from email.Header import Header from email.MIMEText import MIMEText @@ -25,6 +25,21 @@ def encode_quopri(msg): del msg['Content-Transfer-Encoding'] msg['Content-Transfer-Encoding'] = 'quoted-printable' +def nice_sender_header(name, address, charset): + # construct an address header so it's as human-readable as possible + # even in the presence of a non-ASCII name part + h = Header(charset=charset) + # the important bits of formataddr() + if specialsre.search(name): + name = '"%s"'%escapesre.sub(r'\\\g<0>', name) + try: + name.encode('ASCII') + h.append(name, 'ASCII') + except UnicodeEncodeError: + h.append(name) + h.append('<%s>'%address, 'ASCII') + return str(h) + class Mailer: """Roundup-specific mail sending.""" def __init__(self, config): @@ -65,11 +80,7 @@ class Mailer: name = author[0] else: name = unicode(author[0], 'utf-8') - try: - name = name.encode('ascii') - except UnicodeError: - name = Header(name, charset).encode() - author = formataddr((name, author[1])) + author = nice_sender_header(name, author[1], charset) if multipart: message = MIMEMultipart() diff --git a/roundup/roundupdb.py b/roundup/roundupdb.py index 9e241e9..80f4ee1 100644 --- a/roundup/roundupdb.py +++ b/roundup/roundupdb.py @@ -35,7 +35,8 @@ from roundup import password, date, hyperdb from roundup.i18n import _ # MessageSendError is imported for backwards compatibility -from roundup.mailer import Mailer, MessageSendError, encode_quopri +from roundup.mailer import Mailer, MessageSendError, encode_quopri, \ + nice_sender_header class Database: @@ -406,9 +407,10 @@ class IssueClass: else: sendto = [sendto] + # tracker sender info tracker_name = unicode(self.db.config.TRACKER_NAME, 'utf-8') - tracker_name = formataddr((tracker_name, from_address)) - tracker_name = Header(tracker_name, charset) + tracker_name = nice_sender_header(tracker_name, from_address, + charset) # now send one or more messages # TODO: I believe we have to create a new message each time as we diff --git a/test/test_mailer.py b/test/test_mailer.py new file mode 100644 index 0000000..95d0df4 --- /dev/null +++ b/test/test_mailer.py @@ -0,0 +1,24 @@ +#-*- encoding: utf8 -*- +import unittest + +from roundup import mailer + +class EncodingTestCase(unittest.TestCase): + def test(self): + a = lambda n, a, c, o: self.assertEquals(mailer.nice_sender_header(n, + a, c), o) + a('ascii', 'ascii@test.com', 'latin1', 'ascii ') + a(u'café', 'ascii@test.com', 'latin1', + '=?latin1?q?caf=E9?= ') + a('as"ii', 'ascii@test.com', 'latin1', '"as\\"ii" ') + +def test_suite(): + suite = unittest.TestSuite() + suite.addTest(unittest.makeSuite(EncodingTestCase)) + return suite + +if __name__ == '__main__': + runner = unittest.TextTestRunner() + unittest.main(testRunner=runner) + +# vim: set et sts=4 sw=4 : -- 2.30.2