【问题标题】:Python script to send mail发送邮件的 Python 脚本
【发布时间】:2018-11-13 10:06:18
【问题描述】:
import smtplib

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

server.ehlo()

server.starttls()

server.login("******@gmail.com", "*******")

msg = "Hello!"

server.sendmail("rajesh.debugs@gmail.com", "rjucsm@gmail.com", msg)

输出:

C:\Users\Admin\PycharmProjects\Gabbar\venv\Scripts\python.exe C:/Users/Admin/PycharmProjects/Gabbar/MadhuBhai/t3.py
Traceback (most recent call last):
  File "C:/Users/Admin/PycharmProjects/Gabbar/MadhuBhai/t3.py", line 5, in <module>
    server.login("******@gmail.com", "******")
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36-32\lib\smtplib.py", line 730, in login
    raise last_exception
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36-32\lib\smtplib.py", line 721, in login
    initial_response_ok=initial_response_ok)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36-32\lib\smtplib.py", line 642, in auth
    raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (534, b'5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbtX\n5.7.14 HDzghr7H0UegF2rvoxWT6p9FwK8ct-IgZQXTa09qiineo743EE4PjOLOukbW-7fN2_FfIx\n5.7.14 qBwOghPCGmq1zlaUP3231EHWXgeut6dhRtiEjEVKAd-VKglbnUqvCyPMLKlADKhWt56L_5\n5.7.14 afzoYLGapj8SmZxp_W6VMrkj10aK9xthTsrmUerV9bkqgILAnKh9SWOO2n-7WsHO43reIf\n5.7.14 MQqmW0G2lyXQWbYt-8LxUHRt3ATI8> Please log in via your web browser and\n5.7.14 then try again.\n5.7.14  Learn more at\n5.7.14  https://support.google.com/mail/answer/78754 v5-v6sm6337091pfd.1 - gsmtp')

Process finished with exit code 1

【问题讨论】:

    标签: python email smtp send


    【解决方案1】:
    【解决方案2】:

    如果您不打算使用smtplib,那么我建议您使用SendGrid 发送电子邮件(如果您每天发送

    import sendgrid
    from sendgrid.helpers.mail import *
    
    sg = sendgrid.SendGridAPIClient(apikey=sendgrid_api_key)
    from_email = Email("test@example.com")
    to_email = Email("test@example.com")
    subject = "Sending with SendGrid is Fun"
    content = Content("text/plain", "content")
    mail = Mail(from_email, subject, to_email, content)
    response = sg.client.mail.send.post(request_body=mail.get())
    
    print(response.status_code)
    

    【讨论】:

    • 错误:C:\Users\Admin\PycharmProjects\Gabbar\venv\Scripts\python.exe C:/Users/Admin/PycharmProjects/Gabbar/May09/testi.py Traceback(最近一次通话最后):文件“C:/Users/Admin/PycharmProjects/Gabbar/May09/testi.py”,第 2 行,在 中从 sendgrid.helpers.mail 导入 * ModuleNotFoundError:没有名为“sendgrid.helpers”的模块进程完成退出代码 1
    • sendgrid 是第 3 方库,不是原版 Python 的一部分。您需要先从终端使用 pip install send grid 进行 pip 安装(确保使用它安装到运行代码的同一 Python 环境中。
    【解决方案3】:

    您需要将email.mimesmtp 一起使用。试试下面:

    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText 
    from email.mime.image import MIMEImage
    import smtplib
    
    strFrom = 'xyz.uvw@gmail.com'
    strTo = ['efg.abc@gmail.com','hij.lmn@gmail.com']    
    attachment = '<path to attachment if any>'
    
    msgRoot = MIMEMultipart('related')
    msgRoot['Subject'] = 'TEST'
    msgRoot['From'] = strFrom
    msgRoot['To'] = ", ".join(strTo)
    msgRoot.preamble = 'This is a multi-part message in MIME format.'
    
    msgAlternative = MIMEMultipart('alternative')
    msgRoot.attach(msgAlternative)
    
    msgText = MIMEText('This is the alternative plain text message.')
    msgAlternative.attach(msgText)
    
    msgText = MIMEText('<b>Summary</b>', 'html')
    msgAlternative.attach(msgText)
    
    fp = open(attachment, 'rb')  
    msgImage = MIMEImage(fp.read())
    fp.close()
    
    msgImage.add_header('Content-ID', '<image1>')
    msgRoot.attach(msgImage)
    
    smtp = smtplib.SMTP("YOUREMAILHOST", 25, timeout=120)
    smtp.sendmail(strFrom, strTo, msgRoot.as_string())
    smtp.close()
    

    【讨论】:

    • 您使用的是哪个版本的 Python?这个链接应该可以帮助你 - docs.python.org/2/library/email.html
    • 使用 Python 3.6,from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.base import MIMEBase
    猜你喜欢
    • 2014-09-20
    • 2014-05-25
    • 2015-01-16
    • 2018-01-15
    • 1970-01-01
    • 2012-05-13
    • 2016-04-24
    • 2010-09-14
    • 1970-01-01
    相关资源
    最近更新 更多