Skip to content

smtplib cannot sendmail over TLS #48171

Closed

Description

@shidot
mannequin
BPO 3921
Nosy @devdanzin
Superseder
  • bpo-5259: smtplib is broken in Python3
  • Files
  • sendmail_test.py
  • smtplib.diff: Quick and dirty workaround
  • smtpbug.py: Example script
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2009-04-25.22:44:26.762>
    created_at = <Date 2008-09-21.08:11:47.474>
    labels = ['easy', 'type-bug', 'library']
    title = 'smtplib cannot sendmail over TLS'
    updated_at = <Date 2009-04-25.22:44:26.761>
    user = 'https://bugs.python.org/shidot'

    bugs.python.org fields:

    activity = <Date 2009-04-25.22:44:26.761>
    actor = 'ajaksu2'
    assignee = 'none'
    closed = True
    closed_date = <Date 2009-04-25.22:44:26.762>
    closer = 'ajaksu2'
    components = ['Library (Lib)']
    creation = <Date 2008-09-21.08:11:47.474>
    creator = 'shidot'
    dependencies = []
    files = ['11540', '13736', '13782']
    hgrepos = []
    issue_num = 3921
    keywords = ['patch', 'easy']
    message_count = 5.0
    messages = ['73504', '82339', '86296', '86560', '86561']
    nosy_count = 3.0
    nosy_names = ['ajaksu2', 'shidot', 'miwa']
    pr_nums = []
    priority = 'high'
    resolution = 'duplicate'
    stage = 'test needed'
    status = 'closed'
    superseder = '5259'
    type = 'behavior'
    url = 'https://bugs.python.org/issue3921'
    versions = ['Python 3.0', 'Python 3.1']

    Activity

    1. shidot commented on Sep 21, 2008

      shidotmannequin
      MannequinAuthor

      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:<[email protected]> 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:<[email protected]> size=176\r\n'
      reply: '250 2.1.0 OK\r\n'
      reply: retcode (250); Msg: 2.1.0 OK
      send: 'rcpt TO:<[email protected]>\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: [email protected]\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:<[email protected]> 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
      , '[email protected]')

      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 = '[email protected]'
          TK = '[email protected]'
          send_mail('smtp.gmail.com', ME, (TK,), \
            make_mime_text(ME, TK, "test", "This is a test."), True, ME,
      '********')
    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      on Sep 21, 2008
    3. miwa commented on Feb 17, 2009

      miwamannequin
      Mannequin

      The difference is this line.

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

      (Python3, error)
      send: 'AUTH PLAIN AGxhbWJkYS5sZXRAZ21haWwuY29tAGNoaWtha29zMDA=\n\r\n'

    4. devdanzin commented on Apr 22, 2009

      devdanzinmannequin
      Mannequin

      The attached patch makes using smtplib in py3k possible. It isn't
      proposed as a correct fix.

    5. devdanzin commented on Apr 25, 2009

      devdanzinmannequin
      Mannequin

      As pointed by Mariano on IRC, this is closely related to bpo-5259 and bpo-5304.

    6. devdanzin commented on Apr 25, 2009

      devdanzinmannequin
      Mannequin

      Actually, this is fixed by the patch in bpo-5259 (also has test).

    7. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      easystdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions