[issue3921] smtplib cannot sendmail over TLS

Takafumi SHIDO report at bugs.python.org
Sun Sep 21 10:11:48 CEST 2008


New submission from Takafumi SHIDO <takafumi at shido.info>:

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 at 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 at 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 at 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 at gmail.com\r\nTo:
takafumi at 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 at 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 at 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 at gmail.com'
    TK = 'takafumi at shido.info'
    send_mail('smtp.gmail.com', ME, (TK,), \
      make_mime_text(ME, TK, "test", "This is a test."), True, ME,
'********')

----------
components: Library (Lib)
files: sendmail_test.py
messages: 73504
nosy: shidot
severity: normal
status: open
title: smtplib cannot sendmail over TLS
versions: Python 3.0, Python 3.1
Added file: http://bugs.python.org/file11540/sendmail_test.py

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue3921>
_______________________________________


More information about the Python-bugs-list mailing list