New submission from Jens Troeger <[email protected]>:
It looks like non-ascii characters in an Address()’s display_name parameter
cause their lines in the header to get mangled when the message is being sent.
For example, a case to reproduce:
>>> msg = EmailMessage()
>>> msg["To"] = Address(display_name="Jens Tröger",
addr_spec="[email protected]")
>>> msg["From"] = Address(display_name="Jens Troeger",
addr_spec="[email protected]")
>>> msg.set_content("Some content.")
>>> msg.as_string()
'To: Jens =?utf-8?q?Tr=C3=B6ger?= <[email protected]>\nContent-Type:
text/plain; charset="utf-8"\nContent-Transfer-Encoding: 7bit\nMIME-Version:
1.0\nFrom: Jens Troeger <[email protected]>\n\nSome content.\n'
Sending this email creates the following SMTP debug output:
>>> smtpsrv = smtplib.SMTP("smtp.gmail.com:587")
>>> …
>>> smtpsrv.send_message(msg)
send: 'mail FROM:<[email protected]> size=220\r\n'
reply: b'250 2.1.0 OK z23sm16924622pfe.110 - gsmtp\r\n'
reply: retcode (250); Msg: b'2.1.0 OK z23sm16924622pfe.110 - gsmtp'
send: 'rcpt TO:<[email protected]>\r\n'
reply: b'250 2.1.5 OK z23sm16924622pfe.110 - gsmtp\r\n'
reply: retcode (250); Msg: b'2.1.5 OK z23sm16924622pfe.110 - gsmtp'
send: 'data\r\n'
reply: b'354 Go ahead z23sm16924622pfe.110 - gsmtp\r\n'
reply: retcode (354); Msg: b'Go ahead z23sm16924622pfe.110 - gsmtp'
data: (354, b'Go ahead z23sm16924622pfe.110 - gsmtp')
send: b'To: Jens =?utf-8?q?Tr=C3=B6ger?=
<[email protected]>\r\r\r\r\r\nContent-Type: text/plain;
charset="utf-8"\r\nContent-Transfer-
Encoding: 7bit\r\nMIME-Version: 1.0\r\nFrom: Jens Troeger
<[email protected]>\r\n\r\nSome content.\r\n.\r\n'
reply: b'250 2.0.0 OK 1525174591 z23sm16924622pfe.110 - gsmtp\r\n'
reply: retcode (250); Msg: b'2.0.0 OK 1525174591 z23sm16924622pfe.110 -
gsmtp'
data: (250, b'2.0.0 OK 1525174591 z23sm16924622pfe.110 - gsmtp')
{}
Notice the string of "\r\r\…" for the "To" field which consequently breaks off
the remainder of the email’s header into a premature body:
[…]
Message-ID: <[email protected]>
Date: Tue, 01 May 2018 04:36:30 -0700 (PDT)
From: [email protected]
To: Jens Tröger <[email protected]>
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit
MIME-Version: 1.0
From: Jens Troeger <[email protected]>
Some content.
Also notice the two From fields. The first one, I suspect, is supplied from the
SMTP server’s login, the second one from them EmailMessage. Without a From in
the EmailMessage, I get the following error:
>>> smtpsrv.send_message(msg)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/…/lib/python3.6/smtplib.py", line 936, in send_message
from_addr = email.utils.getaddresses([from_addr])[0][1]
File "/…/lib/python3.6/email/utils.py", line 112, in getaddresses
all = COMMASPACE.join(fieldvalues)
TypeError: sequence item 0: expected str instance, NoneType found
Similar breakage of the header into premature body can be achieved with the Cc
header field.
----------
components: email
messages: 315994
nosy: _savage, barry, r.david.murray
priority: normal
severity: normal
status: open
title: From, To, Cc lines break when calling send_message()
type: behavior
versions: Python 3.6
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue33398>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com