zedshaw / lamson

Pythonic SMTP Application Server
http://lamsonproject.org/
Other
733 stars 199 forks source link

`encoding.to_message` ignores multiline headers when there's non-ascii chars #30

Open EmilStenstrom opened 10 years ago

EmilStenstrom commented 10 years ago
>>> msg1 = email.message_from_string("To: Person1 <person1@domain.com>, Person2\n\t<person2@domain.com>")
>>> msg2 = email.message_from_string("To: =?utf-8?q?Emil_Stenstr=C3=B6m?= <person1@domain.com>, Person2\n\t<person2@domain.com>")
>>> print encoding.to_message(encoding.from_message(msg1))
From nobody Tue Dec  3 12:42:05 2013
MIME-Version: 1.0
To: Person1 <person1@domain.com>, Person2
    <person2@domain.com>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
>>> print encoding.to_message(encoding.from_message(msg2))
From nobody Tue Dec  3 12:42:09 2013
MIME-Version: 1.0
To: "=?utf-8?q?Emil_Stenstr=C3=B6m?=" <person1@domain.com>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit

Note that the from_message step seems to work fine:

>>> print encoding.from_message(msg2)["to"]
Emil Stenström <person1@domain.com>, Person2
    <person2@domain.com>
EmilStenstrom commented 10 years ago

The problem is on properly_encode_header. email.parseaddr just parses and returns the first address if there are more than one. Fix is to split the address on "," and then use email.getaddresses instead. Pull request on the way.

EmilStenstrom commented 10 years ago

After this fix, the test case I gave above works fine:

>>> msg2 = email.message_from_string("To: =?utf-8?q?Emil_Stenstr=C3=B6m?= <person1@domain.com>, Person2\n\t<person2@domain.com>")
>>> print encoding.to_message(encoding.from_message(msg2))
From nobody Tue Dec  3 13:52:03 2013
MIME-Version: 1.0
To: "=?utf-8?q?Emil_Stenstr=C3=B6m?=" <person1@domain.com>,
 "=?utf-8?q?Person2?=" <person2@domain.com>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
EmilStenstrom commented 10 years ago

@zedshaw Any chance to get this merged so I can stop using my own fork?

tino commented 10 years ago

Probably not. Look around, you'll see Zed abandoned this project. Please use https://github.com/moggers87/salmon/ as it is the best maintained fork