Ignore:
Timestamp:
7 May 2012, 10:53:25 (13 years ago)
Author:
uli
Message:

Handle unusual SMTP address parts more predictable.

Location:
main/waeup.kofa/trunk/src/waeup/kofa
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/smtp.py

    r8379 r8382  
    9393
    9494def encode_header_item(item):
     95    """Turns `item`, a string into an SMTP header part string.
     96
     97    Encodings are checked carefully (we try to encode as ASCII,
     98    Latin-1 and UTF-8 in that order).
     99
     100    If `item` is not a basestring, `None` is returned.
     101    """
     102    if not isinstance(item, basestring):
     103        return None
    95104    if not isinstance(item, unicode):
    96         try:
    97             item = unicode(item, 'utf-8')
    98         except TypeError:
    99             return None
     105        item = unicode(item, 'utf-8')
    100106    return str(Header(item, 'iso-8859-1')) # try ascii, then latin1, then utf-8
    101107
  • main/waeup.kofa/trunk/src/waeup/kofa/tests/test_smtp.py

    r7819 r8382  
    7575        result3 = encode_header_item(u'Plain Name')
    7676        result4 = encode_header_item(u'Name with umläut')
     77        result5 = encode_header_item(None)
    7778        self.assertEqual(result1, u'Plain Name')
    7879        self.assertEqual(result2, u'=?iso-8859-1?q?Name_with_uml=E4ut?=')
    7980        self.assertEqual(result3, u'Plain Name')
    8081        self.assertEqual(result4, u'=?iso-8859-1?q?Name_with_uml=E4ut?=')
     82        self.assertTrue(result5 is None)
    8183        return
    8284
Note: See TracChangeset for help on using the changeset viewer.