summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 2f181f7)
raw | patch | inline | side by side (parent: 2f181f7)
author | richard <richard@57a73879-2fb5-44c3-a270-3262357dd7e2> | |
Mon, 1 Feb 2010 03:59:02 +0000 (03:59 +0000) | ||
committer | richard <richard@57a73879-2fb5-44c3-a270-3262357dd7e2> | |
Mon, 1 Feb 2010 03:59:02 +0000 (03:59 +0000) |
git-svn-id: http://svn.roundup-tracker.org/svnroot/roundup/roundup/trunk@4442 57a73879-2fb5-44c3-a270-3262357dd7e2
CHANGES.txt | patch | blob | history | |
roundup/mailer.py | patch | blob | history | |
roundup/roundupdb.py | patch | blob | history | |
test/test_mailer.py | [new file with mode: 0644] | patch | blob |
diff --git a/CHANGES.txt b/CHANGES.txt
index 2262b94fe172730542c3d47e91835fba314fd731..e311524ab9914681496739b5d2e014c01edb560d 100644 (file)
--- a/CHANGES.txt
+++ b/CHANGES.txt
- 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 f1a1af3bdf45a20f469dd7132b022449cf922df1..729aaeab4d7200fbef196db904d53497f59fbde4 100644 (file)
--- a/roundup/mailer.py
+++ b/roundup/mailer.py
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
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):
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 9e241e9309e1516aa3ab46406f5b21066cf30999..80f4ee108eea9a71cc814822ccb18862c8115e05 100644 (file)
--- a/roundup/roundupdb.py
+++ b/roundup/roundupdb.py
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:
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
--- /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 <ascii@test.com>')
+ a(u'café', 'ascii@test.com', 'latin1',
+ '=?latin1?q?caf=E9?= <ascii@test.com>')
+ a('as"ii', 'ascii@test.com', 'latin1', '"as\\"ii" <ascii@test.com>')
+
+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 :