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: smtplib cannot sendmail over TLS
Type: behavior Stage: test needed
Components: Library (Lib) Versions: Python 3.0, Python 3.1
process
Status: closed Resolution: duplicate
Dependencies: Superseder: smtplib is broken in Python3
View: 5259
Assigned To: Nosy List: ajaksu2, miwa, shidot
Priority: high Keywords: easy, patch

Created on 2008-09-21 08:11 by shidot, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
sendmail_test.py shidot, 2008-09-21 08:11
smtplib.diff ajaksu2, 2009-04-22 14:40 Quick and dirty workaround
smtpbug.py ajaksu2, 2009-04-25 22:32 Example script
Messages (5)
msg73504 - (view) Author: Takafumi SHIDO (shidot) Date: 2008-09-21 08:11
when a SMTP object tries to send a mail through TLS, the smtp server 
replies retcode 502.

When a test code (sendmail_test.py) is executed on Python 3,
it stacks on sending mail while the test code works on Python 2.5.

Following is the result of the test.

The first half is the result on Python 2.5. 
A mail has been sent without any problem.

The second half is the result on Python 3.
Upto AUTH, it works fine.
But when it sends mail, the server replies retcode 502.
This is the message of failure:

send: 'mail FROM:<lambda.let@gmail.com> size=176\r\n'
reply: b'502 5.5.1 Unrecognized command. u8sm8612791tia.6\r\n'
 

====================== RESULT ============================
Z:\doc\monthly\08-09\py_book3\script\net>python mail2.py
send: 'ehlo [192.168.1.8]\r\n'
reply: '250-mx.google.com at your service, [124.41.68.137]\r\n'
reply: '250-SIZE 35651584\r\n'
reply: '250-8BITMIME\r\n'
reply: '250-STARTTLS\r\n'
reply: '250 ENHANCEDSTATUSCODES\r\n'
reply: retcode (250); Msg: mx.google.com at your service, [124.41.68.137]
SIZE 35651584
8BITMIME
STARTTLS
ENHANCEDSTATUSCODES
send: 'STARTTLS\r\n'
reply: '220 2.0.0 Ready to start TLS\r\n'
reply: retcode (220); Msg: 2.0.0 Ready to start TLS
send: 'ehlo [192.168.1.8]\r\n'
reply: '250-mx.google.com at your service, [124.41.68.137]\r\n'
reply: '250-SIZE 35651584\r\n'
reply: '250-8BITMIME\r\n'
reply: '250-AUTH LOGIN PLAIN\r\n'
reply: '250 ENHANCEDSTATUSCODES\r\n'
reply: retcode (250); Msg: mx.google.com at your service, [124.41.68.137]
SIZE 35651584
8BITMIME
AUTH LOGIN PLAIN
ENHANCEDSTATUSCODES
send: 'AUTH PLAIN AGxhbWJkYS5sZXRAZ21haWwuY29tAGNoaWtha29zMDA=\r\n'
reply: '235 2.7.0 Accepted\r\n'
reply: retcode (235); Msg: 2.7.0 Accepted
send: 'mail FROM:<lambda.let@gmail.com> size=176\r\n'
reply: '250 2.1.0 OK\r\n'
reply: retcode (250); Msg: 2.1.0 OK
send: 'rcpt TO:<takafumi@shido.info>\r\n'
reply: '250 2.1.5 OK\r\n'
reply: retcode (250); Msg: 2.1.5 OK
send: 'data\r\n'
reply: '354 Go ahead\r\n'
reply: retcode (354); Msg: Go ahead
data: (354, 'Go ahead')
send: 'MIME-Version: 1.0\r\nContent-Type: text/plain;
charset="us-ascii"\r\nCon
ent-Transfer-Encoding: 7bit\r\nFrom: lambda.let@gmail.com\r\nTo:
takafumi@shido
info\r\nSubject: test\r\n\r\nThis is a test.\r\n.\r\n'
reply: '250 2.0.0 OK 1221982163 22sm8756554tim.10\r\n'
reply: retcode (250); Msg: 2.0.0 OK 1221982163 22sm8756554tim.10
data: (250, '2.0.0 OK 1221982163 22sm8756554tim.10')
send: 'quit\r\n'
reply: '221 2.0.0 mx.google.com closing connection 22sm8756554tim.10\r\n'
reply: retcode (221); Msg: 2.0.0 mx.google.com closing connection
22sm8756554ti
.10

Z:\doc\monthly\08-09\py_book3\script\net>py3 mail2.py

Z:\doc\monthly\08-09\py_book3\script\net>c:\python30\python.exe mail2.py

send: 'ehlo [192.168.1.8]\r\n'
reply: b'250-mx.google.com at your service, [124.41.68.137]\r\n'
reply: b'250-SIZE 35651584\r\n'
reply: b'250-8BITMIME\r\n'
reply: b'250-STARTTLS\r\n'
reply: b'250 ENHANCEDSTATUSCODES\r\n'
reply: retcode (250); Msg: b'mx.google.com at your service,
[124.41.68.137]\nSI
E 35651584\n8BITMIME\nSTARTTLS\nENHANCEDSTATUSCODES'
send: 'STARTTLS\r\n'
reply: b'220 2.0.0 Ready to start TLS\r\n'
reply: retcode (220); Msg: b'2.0.0 Ready to start TLS'
send: 'ehlo [192.168.1.8]\r\n'
reply: b'250-mx.google.com at your service, [124.41.68.137]\r\n'
reply: b'250-SIZE 35651584\r\n'
reply: b'250-8BITMIME\r\n'
reply: b'250-AUTH LOGIN PLAIN\r\n'
reply: b'250 ENHANCEDSTATUSCODES\r\n'
reply: retcode (250); Msg: b'mx.google.com at your service,
[124.41.68.137]\nSI
E 35651584\n8BITMIME\nAUTH LOGIN PLAIN\nENHANCEDSTATUSCODES'
send: 'AUTH PLAIN AGxhbWJkYS5sZXRAZ21haWwuY29tAGNoaWtha29zMDA=\n\r\n'
reply: b'235 2.7.0 Accepted\r\n'
reply: retcode (235); Msg: b'2.7.0 Accepted'
send: 'mail FROM:<lambda.let@gmail.com> size=176\r\n'
reply: b'502 5.5.1 Unrecognized command. u8sm8612791tia.6\r\n'
reply: retcode (502); Msg: b'5.5.1 Unrecognized command. u8sm8612791tia.6'
send: 'rset\r\n'
reply: b'250 2.1.0 OK\r\n'
reply: retcode (250); Msg: b'2.1.0 OK'
Traceback (most recent call last):
  File "sendmail_test.py", line 43, in <module>

  File "sendmail_test.py", line 35, in send_mail

  File "c:\python30\lib\smtplib.py", line 701, in sendmail
    raise SMTPSenderRefused(code, resp, from_addr)
smtplib.SMTPSenderRefused: (502, b'5.5.1 Unrecognized command.
u8sm8612791tia.6
, 'lambda.let@gmail.com')

Z:\doc\monthly\08-09\py_book3\script\net>

==================== Test Code (sendmail_test.py) =============

#! python

''' send mail using Python 2.5 and 3'''

import smtplib
from email.mime.text import MIMEText
from email.header import Header


def make_mime_text(mail_from, mail_to, subject, body):
    '''create MIMEText'''

    msg = MIMEText(body, 'plain', 'ascii')
    msg['From'] = Header(mail_from, 'ascii')
    msg['To'] = Header(mail_to,  'ascii')
    msg['Subject'] = Header(subject, 'ascii')
    return msg

def send_mail(server, mail_from, mail_to, msg, use_tls=False, user='',
passwd='', ):
    '''sendmail, if use_tls=True, use TLS'''

    sender = smtplib.SMTP(server, (587 if use_tls else 25))
    sender.set_debuglevel(1)
    
    if use_tls:
        sender.ehlo()
        sender.starttls()
        sender.ehlo()
        sender.login(user, passwd)
        
    sender.sendmail(mail_from, mail_to, msg.as_string())
    sender.quit()


if __name__ =='__main__':
    ME = 'lambda.let@gmail.com'
    TK = 'takafumi@shido.info'
    send_mail('smtp.gmail.com', ME, (TK,), \
      make_mime_text(ME, TK, "test", "This is a test."), True, ME,
'********')
msg82339 - (view) Author: Musashi Tamura (miwa) Date: 2009-02-17 14:30
The difference is this line.

(2.5, work)
send: 'AUTH PLAIN AGxhbWJkYS5sZXRAZ21haWwuY29tAGNoaWtha29zMDA=\r\n'

(Python3, error)
send: 'AUTH PLAIN AGxhbWJkYS5sZXRAZ21haWwuY29tAGNoaWtha29zMDA=\n\r\n'
msg86296 - (view) Author: Daniel Diniz (ajaksu2) * (Python triager) Date: 2009-04-22 14:40
The attached patch makes using smtplib in py3k possible. It isn't
proposed as a correct fix.
msg86560 - (view) Author: Daniel Diniz (ajaksu2) * (Python triager) Date: 2009-04-25 22:32
As pointed by Mariano on IRC, this is closely related to #5259 and #5304.
msg86561 - (view) Author: Daniel Diniz (ajaksu2) * (Python triager) Date: 2009-04-25 22:44
Actually, this is fixed by the patch in issue 5259 (also has test).
History
Date User Action Args
2022-04-11 14:56:39adminsetgithub: 48171
2009-04-25 22:44:26ajaksu2setstatus: open -> closed
resolution: duplicate
superseder: smtplib is broken in Python3
messages: + msg86561
2009-04-25 22:32:33ajaksu2setfiles: + smtpbug.py

messages: + msg86560
2009-04-22 14:40:30ajaksu2setfiles: + smtplib.diff
priority: high
type: behavior


keywords: + patch, easy
nosy: + ajaksu2
messages: + msg86296
stage: test needed
2009-04-22 14:40:11ajaksu2linkissue5259 dependencies
2009-02-17 14:30:51miwasetnosy: + miwa
messages: + msg82339
2008-09-21 08:11:47shidotcreate