Index: email/test/test_email.py =================================================================== --- email/test/test_email.py (revision 79918) +++ email/test/test_email.py (working copy) @@ -1554,7 +1554,7 @@ def test_rfc2047_without_whitespace(self): s = 'Sm=?ISO-8859-1?B?9g==?=rg=?ISO-8859-1?B?5Q==?=sbord' dh = decode_header(s) - self.assertEqual(dh, [(s, None)]) + self.assertEqual(dh, [('Sm\xf6rg\xe5sbord', 'iso-8859-1')]) def test_rfc2047_with_whitespace(self): s = 'Sm =?ISO-8859-1?B?9g==?= rg =?ISO-8859-1?B?5Q==?= sbord' @@ -1563,7 +1563,12 @@ ('rg', None), ('\xe5', 'iso-8859-1'), ('sbord', None)]) + def test_multiple_encoded_words(self): + s = 'aaa=?iso-8859-1?q?bbb?=xxx asdf =?iso-8859-1?q?jkl?=' + dh = decode_header(s) + self.assertEqual(dh, [('aaabbbxxx asdf jkl', 'iso-8859-1')]) + # Test the MIMEMessage class class TestMIMEMessage(TestEmailBase): Index: email/header.py =================================================================== --- email/header.py (revision 79918) +++ email/header.py (working copy) @@ -39,7 +39,6 @@ \? # literal ? (?P.*?) # non-greedy up to the next ?= is the encoded string \?= # literal ?= - (?=[ \t]|$) # whitespace or the end of the string ''', re.VERBOSE | re.IGNORECASE | re.MULTILINE) # Field name regexp, including trailing colon, but not separating whitespace, @@ -47,7 +46,11 @@ # For use with .match() fcre = re.compile(r'[\041-\176]+:$') +# Match whitespace at the beginning/end of a string +wbeg = re.compile(r'^\s') +wend = re.compile(r'\s$') + # Helpers _max_append = email.quoprimime._max_append @@ -78,8 +81,13 @@ continue parts = ecre.split(line) while parts: + embed = False + if len(parts) > 4 and len(parts[0]) > 0 and not wend.search(parts[0]): + embed = True + if len(parts) > 4 and len(parts[4]) > 0 and not wbeg.search(parts[4]): + embed = True unenc = parts.pop(0).strip() - if unenc: + if unenc and not embed: # Should we continue a long line? if decoded and decoded[-1][1] is None: decoded[-1] = (decoded[-1][0] + SPACE + unenc, None) @@ -102,6 +110,9 @@ if dec is None: dec = encoded + if embed: + dec = unenc.encode(charset) + dec + parts[3].encode(charset) + parts[3] = '' if decoded and decoded[-1][1] == charset: decoded[-1] = (decoded[-1][0] + dec, decoded[-1][1]) else: