#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    3
    Rep Power
    0

    Question smtplib: Can't seem to get starttls and auth to work


    I've been trying to get this to work to no avail. Any help would be appreciated.

    Code:
    import sys, smtplib
    
    def prompt(prompt):
        sys.stdout.write(prompt + ": ")
        return sys.stdin.readline().strip()
    
    fromaddr = prompt("From")
    toaddrs  = prompt("To").split(',')
    print "Enter message, end with ^D:"
    msg = ''
    while 1:
        line = sys.stdin.readline()
        if not line:
            break
        msg = msg + line
    print "Message length is %d" % len(msg)
    
    server = smtplib.SMTP('smtp.SERVER.com')
    server.set_debuglevel(1)
    server.ehlo()
    server.starttls()
    server.login('USERID', 'PASSWORD')
    server.sendmail(fromaddr, toaddrs, msg)
    server.quit()
    Here's my output:

    From: user@user.net
    To: user@user.net
    Enter message, end with ^D:
    asdf
    Message length is 5
    send: 'ehlo mybox.mybox.com\r\n'
    reply: '250-Pleased to meet you\r\n'
    reply: '250-STARTTLS\r\n'
    reply: '250-AUTH PLAIN LOGIN\r\n'
    reply: '250-AUTH=PLAIN LOGIN\r\n'
    reply: '250-PIPELINING\r\n'
    reply: '250-8BITMIME\r\n'
    reply: '250-SIZE 20971520\r\n'
    reply: '250 HELP\r\n'
    reply: retcode (250); Msg: Pleased to meet you
    STARTTLS
    AUTH PLAIN LOGIN
    AUTH=PLAIN LOGIN
    PIPELINING
    8BITMIME
    SIZE 20971520
    HELP
    send: 'STARTTLS\r\n'
    reply: '220 Go ahead\r\n'
    reply: retcode (220); Msg: Go ahead
    send: 'AUTH PLAIN AUTHSTRINGDELETEDHEREFORMYSAFETY =\r\n'
    reply: '503 bad sequence of commands\r\n'
    reply: retcode (503); Msg: bad sequence of commands
    send: 'mail FROM:<user@user.net> size=5\r\n'
    reply: '503 bad sequence of commands\r\n'
    reply: retcode (503); Msg: bad sequence of commands
    send: 'rset\r\n'
    reply: '250 OK\r\n'
    reply: retcode (250); Msg: OK
    Traceback (most recent call last):
    File "C:\mysmtp.py", line 25, in ?
    server.sendmail(fromaddr, toaddrs, msg)
    File "C:\Python23\lib\smtplib.py", line 676, in sendmail
    raise SMTPSenderRefused(code, resp, from_addr)
    SMTPSenderRefused: (503, 'bad sequence of commands', 'user@user.net')

    I've tried it interactively in the idle and I always get 503 bad sequence but when I sniffed the traffic it looks like my data is encrypted and everything is fine. Then all of a sudden the connection stops. I must be missing something simple but I can't figure it out.

    Thanks for any help.
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    I've had some big problems with smtplib in the past, but i've never had this error.

    So, all i can recommend right now would be to try a different server (mail.yahoo.com for instance). Also, in the smtplib example, you need to end your message with ^D... which caught me out at first .

    Give my mail function a go and let me know if you get the same problems:

    http://www.devshed.com/c/a/Python/Python-on-the-Web/3/

    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    3
    Rep Power
    0

    Talking


    Originally Posted by randd
    I've been trying to get this to work to no avail. Any help would be appreciated.
    Code:
    server.ehlo()
    server.starttls()
    server.login('USERID', 'PASSWORD')
    Ah, I figured it out. After reviewing the RFCs I found that they like you to send an ehlo after the initialization of the starttls() command.
    Code:
    server.ehlo()
    server.starttls()
    server.ehlo()
    server.login('USERID', 'PASSWORD')
    This works now.

    Thanks.

IMN logo majestic logo threadwatch logo seochat tools logo