Failing to send email with the Python example

后端 未结 10 940
南旧
南旧 2020-12-04 15:56

I\'ve been trying (and failing) to figure out how to send email via Python.

Trying the example from here: http://docs.python.org/library/smtplib.html#smtplib.SMTP

10条回答
  •  情深已故
    2020-12-04 16:36

    The following code works for me:

    import smtplib
    
    FROMADDR = "my.real.address@gmail.com"
    LOGIN    = FROMADDR
    PASSWORD = "my.real.password"
    TOADDRS  = ["my.real.address@gmail.com"]
    SUBJECT  = "Test"
    
    msg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n"
           % (FROMADDR, ", ".join(TOADDRS), SUBJECT) )
    msg += "some text\r\n"
    
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.set_debuglevel(1)
    server.ehlo()
    server.starttls()
    server.login(LOGIN, PASSWORD)
    server.sendmail(FROMADDR, TOADDRS, msg)
    server.quit()
    
    

    I'm using Python 2.5.2.

    Edit: changed port from 25 to 587 as suggested by ΤΖΩΤΖΙΟΥ, and dropped the second ehlo(). Now I would love to know why port 25 works perfectly from my machine (and port 465 does not).

提交回复
热议问题