Skip to content
Advertisement

Problem with a mail message created by a parser

If I create a message this way (using real addresses, of course):

JavaScript

I can successfully send it using smtplib. No problem with the Unicode characters in the body. The received message has these headers:

JavaScript

If I try to create the same message in this alternative way:

JavaScript

I can’t send it. send_message() from smtplib fails with

JavaScript

and obviously expects ascii, not Unicode. What causes the difference and how to fix it properly?

(code is based on these examples)

Advertisement

Answer

The error can be avoided by encoding msgsource and then parsing the resulting bytes:

JavaScript

outputs

JavaScript

sending it to Python’s SMTP DebuggingServer produces

JavaScript

Note that no encoding headers are written: I’m guessing that the parsers attempt to reproduce the message from the source string or bytes as faithfully as possible, making as few additional assumptions as possible. The Parser docs

[Parser is] an API that can be used to parse a message when the complete contents of the message are available in a [string/bytes/file]

seem to me to support this interpretation.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement