changeset 4711:4692e88cebb6

fix sendmail.encodeAddress (do not [QP] encode blanks, do not unnecessarily use [QP] encoding for pure ascii mail addrs), fix tests
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 25 Apr 2009 00:14:33 +0200
parents 34f0fe3ff120
children 7d1c139a52b7
files MoinMoin/mail/_tests/test_sendmail.py MoinMoin/mail/sendmail.py
diffstat 2 files changed, 22 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/mail/_tests/test_sendmail.py	Mon Apr 20 20:11:47 2009 +0200
+++ b/MoinMoin/mail/_tests/test_sendmail.py	Sat Apr 25 00:14:33 2009 +0200
@@ -89,21 +89,20 @@
     def testComposite(self):
         """ mail.sendmail: encode address: 'Phrase <local@domain>' """
         address = u'Phrase <local@domain>'
-        phrase = str(Header(u'Phrase '.encode('utf-8'), self.charset))
-        expected = phrase + '<local@domain>'
+        expected = str(address)
         assert sendmail.encodeAddress(address, self.charset) == expected
 
     def testCompositeUnicode(self):
         """ mail.sendmail: encode Uncode address: 'ויקי <local@domain>' """
         address = u'ויקי <local@domain>'
-        phrase = str(Header(u'ויקי '.encode('utf-8'), self.charset))
-        expected = phrase + '<local@domain>'
+        phrase = str(Header(u'ויקי'.encode('utf-8'), self.charset))
+        expected = phrase + ' ' + '<local@domain>'
         assert sendmail.encodeAddress(address, self.charset) == expected
 
     def testEmptyPhrase(self):
         """ mail.sendmail: encode address with empty phrase: '<local@domain>' """
         address = u'<local@domain>'
-        expected = address.encode(config.charset)
+        expected = 'local@domain'
         assert sendmail.encodeAddress(address, self.charset) == expected
 
     def testEmptyAddress(self):
@@ -113,8 +112,7 @@
         case, but we don't do error checking for mail addresses.
         """
         address = u'Phrase <>'
-        phrase = str(Header(u'Phrase '.encode('utf-8'), self.charset))
-        expected = phrase + '<>'
+        expected = str(address)
         assert sendmail.encodeAddress(address, self.charset) == expected
 
     def testInvalidAddress(self):
@@ -125,7 +123,7 @@
         out.
         """
         address = u'Phrase <blah'
-        expected = address.encode(config.charset)
+        expected = str(address)
         assert sendmail.encodeAddress(address, self.charset) == expected
 
 coverage_modules = ['MoinMoin.mail.sendmail']
--- a/MoinMoin/mail/sendmail.py	Mon Apr 20 20:11:47 2009 +0200
+++ b/MoinMoin/mail/sendmail.py	Sat Apr 25 00:14:33 2009 +0200
@@ -32,15 +32,25 @@
     @rtype: string
     @return: encoded address
     """
-    composite = re.compile(r'(?P<phrase>.+)(?P<angle_addr>\<.*\>)', re.UNICODE)
+    assert isinstance(address, unicode)
+    composite = re.compile(r'(?P<phrase>.*?)(?P<blanks>\s*)\<(?P<addr>.*)\>', re.UNICODE)
     match = composite.match(address)
     if match:
-        phrase = match.group('phrase').encode(config.charset)
-        phrase = str(Header(phrase, charset))
-        angle_addr = match.group('angle_addr').encode(config.charset)
-        return phrase + angle_addr
+        phrase = match.group('phrase')
+        try:
+            str(phrase)  # is it pure ascii?
+        except UnicodeEncodeError:
+            phrase = phrase.encode(config.charset)
+            phrase = Header(phrase, charset)
+        blanks = match.group('blanks')
+        addr = match.group('addr')
+        if phrase:
+            return "%s%s<%s>" % (str(phrase), str(blanks), str(addr))
+        else:
+            return str(addr)
     else:
-        return address.encode(config.charset)
+        # a pure email address, should encode to ascii without problem
+        return str(address)
 
 
 def sendmail(request, to, subject, text, mail_from=None):