Failing to send email with the Python example

后端 未结 10 937
南旧
南旧 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:23

    Okay, found out that this line of code does the trick!

    server = smtplib.SMTP('smtp.gmail.com', 587 )

    Turned out to be GMAIL didn't support SSL on port 25 (and port 465 caused a hang for some reason).

    Thanks guys!

    0 讨论(0)
  • 2020-12-04 16:24

    You should check your port, I'm not sure that google's SMTP port is 65, that would explain the timeout.

    Modify your sources as such:

    smtplib.SMTP_SSL('smtp.google.com', 465)

    If, however, you are certain that it ought to work and it doesn't, it appears that there are some problems with smtplib.SMTP_SSL, and there's an available patch for it here.

    0 讨论(0)
  • 2020-12-04 16:26
    import smtplib
    
    content = 'example email stuff here'
    
    mail = smtplib.SMTP('smtp.gmail.com', 587)
    
    mail.ehlo()
    
    mail.starttls()
    
    mail.login('email@gmail.com','password')
    
    mail.sendmail('email@gmail.com', 'email@yahoo.com', content)
    
    mail.close()
    
    0 讨论(0)
  • 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).

    0 讨论(0)
  • 2020-12-04 16:36

    Here's a simple throw away solution. Meant to paste this earlier, but fell asleep at my chair.

    
    import smtplib
    import email
    import os
    
    username = "user@gmail.com"
    passwd = "password"
    
    def mail(to, subject, text, attach):
       msg = MIMEMultipart()
    
       msg['From'] = username
       msg['To'] = to
       msg['Subject'] = subject
    
       msg.attach(MIMEText(text)) 
    
       part = MIMEBase('application', 'octet-stream')
       part.set_payload(open(attach, 'rb').read())
       Encoders.encode_base64(part)
       part.add_header('Content-Disposition',
               'attachment; filename="%s"' % os.path.basename(attach))
       msg.attach(part)
    
       server = smtplib.SMTP("smtp.gmail.com", 495)
       server.ehlo()
       server.starttls()
       server.ehlo()
       server.login(username, passwd)
       server.sendmail(username, to, msg.as_string())
       server.close()
    
    mail("you", "hi", "hi", "webcam.jpg")
    
    

    It's my assumption that most people on this thread that have had successful attempts with their code aren't on win32. ;)

    *edit: See http://docs.python.org/library/email-examples.html for some good "official" examples.

    0 讨论(0)
  • 2020-12-04 16:40
    from smtplib import SMTP_SSL as SMTP
    from email.mime.text import MIMEText
    
    HOST = 'smtp.gmail.com'
    PORT = 465
    
    USERNAME = 'oltjano13@gmail.com'
    PASSWORD = ''
    
    SENDER = 'oltjano13@gmail.com'
    RECIPIENT = 'oltjano13@gmail.com'
    
    text_subtype = 'plain'
    
    with open('textfile', 'rb') as f:
        msg = MIMEText(f.read(), text_subtype)
    
    
    msg['Subject'] = 'Python Script'
    msg['From'] = SENDER
    msg['To'] = RECIPIENT
    
    try:
        connection = SMTP(HOST, PORT)
        connection.login(USERNAME, PASSWORD)
        connection.sendmail(SENDER, RECIPIENT, msg.as_string())
    except Exception, e:
        print(e)
    

    The above code works fine for me. As you can see the PORT = 465 is being used in this example since I am using SSL. If you plan to use the port 587 then TLS is required.

    0 讨论(0)
提交回复
热议问题