Sending mail error with python smtplib

后端 未结 2 1046
难免孤独
难免孤独 2020-11-29 11:08

I am attempting to use the python 3.2 SMTPlib.sendmail() function to send a message, after some modifcation of the SMTP library (namely commenting out the rset() function wh

相关标签:
2条回答
  • 2020-11-29 11:24

    Add the following to your code before logging in and have a try again;

    try:
            self.smtp.ehlo()
            self.smtp.starttls()
            self.smtp.ehlo
    except:
            print "No TLS :("
    
    #do login here
    
    0 讨论(0)
  • 2020-11-29 11:29

    The following works for microsoft, google, yahoo accounts on Python 2.7 and Python 3.2:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    """Send email via smtp_host."""
    import smtplib
    from email.mime.text import MIMEText
    from email.header    import Header
    
    ####smtp_host = 'smtp.live.com'        # microsoft
    ####smtp_host = 'smtp.gmail.com'       # google
    smtp_host = 'smtp.mail.yahoo.com'  # yahoo
    login, password = ...
    recipients_emails = [login]
    
    msg = MIMEText('body…', 'plain', 'utf-8')
    msg['Subject'] = Header('subject…', 'utf-8')
    msg['From'] = login
    msg['To'] = ", ".join(recipients_emails)
    
    s = smtplib.SMTP(smtp_host, 587, timeout=10)
    s.set_debuglevel(1)
    try:
        s.starttls()
        s.login(login, password)
        s.sendmail(msg['From'], recipients_emails, msg.as_string())
    finally:
        s.quit()
    
    0 讨论(0)
提交回复
热议问题