This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: From, To, Cc lines break when calling send_message()
Type: behavior Stage:
Components: email Versions: Python 3.6
process
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 2022-04-11 14:58 by admin.

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="jens.troeger@gmail.com")
    >>> msg["From"] = Address(display_name="Jens Troeger", addr_spec="jens.troeger@gmail.com")
    >>> msg.set_content("Some content.")
    >>> msg.as_string()
    'To: Jens =?utf-8?q?Tr=C3=B6ger?= <jens.troeger@gmail.com>\nContent-Type: text/plain; charset="utf-8"\nContent-Transfer-Encoding: 7bit\nMIME-Version: 1.0\nFrom: Jens Troeger <jens.troeger@gmail.com>\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:<jens.troeger@gmail.com> 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:<jens.troeger@gmail.com>\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?= <jens.troeger@gmail.com>\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 <jens.troeger@gmail.com>\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: <5ae8513e.17b9620a.eebf7.d56e@mx.google.com>                                                             
    Date: Tue, 01 May 2018 04:36:30 -0700 (PDT)                                                                          
    From: jens.troeger@gmail.com                                                                                               
    To: Jens Tröger <jens.troeger@gmail.com>                                                                             
                                                                                                                     
                                                                                                                     
                                                                                                                     
    Content-Type: text/plain; charset="utf-8"                                                                            
    Content-Transfer-Encoding: 7bit                                                                                      
    MIME-Version: 1.0                                                                                                    
    From: Jens Troeger <jens.troeger@gmail.com>                                                                          
                                                                                                                     
    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.
msg322769 - (view) Author: Jens Troeger (_savage) * Date: 2018-07-31 13:01
See also this issue comment: https://bugs.python.org/issue24218#msg322761
History
Date User Action Args
2022-04-11 14:58:59adminsetgithub: 77579
2018-07-31 13:01:46_savagesetmessages: + msg322769
2018-05-01 11:51:56_savagecreate