Title: From, To, Cc lines break when calling send_message()
Type: behavior Stage:
Components: email Versions: Python 3.6
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: _savage, barry, r.david.murray
Priority: normal Keywords:

Created on 2018-05-01 11:51 by _savage, last changed 2018-07-31 13:01 by _savage.

Messages (2)
msg315994 - (view) Author: Jens Troeger (_savage) * Date: 2018-05-01 11:51
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="")
    >>> msg["From"] = Address(display_name="Jens Troeger", addr_spec="")
    >>> msg.set_content("Some content.")
    >>> msg.as_string()
    'To: Jens =?utf-8?q?Tr=C3=B6ger?= <>\nContent-Type: text/plain; charset="utf-8"\nContent-Transfer-Encoding: 7bit\nMIME-Version: 1.0\nFrom: Jens Troeger <>\n\nSome content.\n'

Sending this email creates the following SMTP debug output:

    >>> smtpsrv = smtplib.SMTP("")
    >>> …
    >>> smtpsrv.send_message(msg)
    send: 'mail FROM:<> 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:<>\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?= <>\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 <>\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: <>                                                             
    Date: Tue, 01 May 2018 04:36:30 -0700 (PDT)                                                                          
    To: Jens Tröger <>                                                                             
    Content-Type: text/plain; charset="utf-8"                                                                            
    Content-Transfer-Encoding: 7bit                                                                                      
    MIME-Version: 1.0                                                                                                    
    From: Jens Troeger <>                                                                          
    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/", line 936, in send_message
        from_addr = email.utils.getaddresses([from_addr])[0][1]
      File "/…/lib/python3.6/email/", 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.
msg322769 - (view) Author: Jens Troeger (_savage) * Date: 2018-07-31 13:01
See also this issue comment:
Date User Action Args
2018-07-31 13:01:46_savagesetmessages: + msg322769
2018-05-01 11:51:56_savagecreate