New submission from Jens Troeger <jens.troe...@gmail.com>:

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.troe...@gmail.com")
    >>> msg["From"] = Address(display_name="Jens Troeger", 
addr_spec="jens.troe...@gmail.com")
    >>> msg.set_content("Some content.")
    >>> msg.as_string()
    'To: Jens =?utf-8?q?Tr=C3=B6ger?= <jens.troe...@gmail.com>\nContent-Type: 
text/plain; charset="utf-8"\nContent-Transfer-Encoding: 7bit\nMIME-Version: 
1.0\nFrom: Jens Troeger <jens.troe...@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.troe...@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.troe...@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.troe...@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.troe...@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.d...@mx.google.com>                    
                                         
    Date: Tue, 01 May 2018 04:36:30 -0700 (PDT)                                 
                                         
    From: jens.troe...@gmail.com                                                
                                               
    To: Jens Tröger <jens.troe...@gmail.com>                                    
                                         
                                                                                
                                     
                                                                                
                                     
                                                                                
                                     
    Content-Type: text/plain; charset="utf-8"                                   
                                         
    Content-Transfer-Encoding: 7bit                                             
                                         
    MIME-Version: 1.0                                                           
                                         
    From: Jens Troeger <jens.troe...@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.

----------
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 <rep...@bugs.python.org>
<https://bugs.python.org/issue33398>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to