diff roundup/mailer.py @ 4340:ac3f80e39d7a

handle quoting/escaping specials after encoding; fix tests to handle new, more consistent header wrapping
author Richard Jones <richard@users.sourceforge.net>
date Mon, 01 Feb 2010 05:46:59 +0000
parents 94ee533613ac
children 7f67092fe03d
line wrap: on
line diff
--- a/roundup/mailer.py	Mon Feb 01 04:54:59 2010 +0000
+++ b/roundup/mailer.py	Mon Feb 01 05:46:59 2010 +0000
@@ -28,17 +28,20 @@
 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)
+    if not name:
+        return address
     try:
-        name.encode('ASCII')
-        h.append(name, 'ASCII')
+        encname = name.encode('ASCII')
     except UnicodeEncodeError:
-        h.append(name)
-    h.append('<%s>'%address, 'ASCII')
-    return str(h)
+        # use Header to encode correctly.
+        encname = Header(name, charset=charset).encode()
+
+    # the important bits of formataddr()
+    if specialsre.search(encname):
+        encname = '"%s"'%escapesre.sub(r'\\\g<0>', encname)
+
+    # now use Header again to wrap the line if necessary
+    return '%s <%s>'%(encname, address)
 
 class Mailer:
     """Roundup-specific mail sending."""

Roundup Issue Tracker: http://roundup-tracker.org/