简体   繁体   English

smtplib 没有发送 email 并且没有出现错误

[英]smtplib No email being sent and no errors appear

I am trying to send a message through smtplib server:我正在尝试通过 smtplib 服务器发送消息:

msg = EmailMessage()
msg['Subject'] = 'Product in Stock Alert'
msg['From'] = 'sender'
msg['To'] = 'reciever'
msg.set_content("Hey, {} is now available in stock\n"
                "Check it out soon : {}".format('hamza', 'google.com'))
with smtplib.SMTP('smtp.gmail.com', 587) as smtp:
    smtp.ehlo()
    smtp.starttls()
    smtp.ehlo()
    smtp.login('gmail', 'code')
    smtp.sendmail('sender', 'reciever' ,msg)

i run it through the cmd, but nothing appear or happen, i have lesssecureapps on and 2-step verification off in my gmail account.我通过 cmd 运行它,但没有出现或发生任何事情,我在我的 gmail 帐户中打开了较少的安全应用程序并关闭了两步验证。 Thanks in advance提前致谢

add .as_string() method to msg:在 msg 中添加.as_string()方法:

smtp.sendmail('sender', 'reciever', msg.as_string())

so, code should look like:因此,代码应如下所示:

msg = EmailMessage()
msg['Subject'] = 'Product in Stock Alert'
msg['From'] = 'sender'
msg['To'] = 'reciever'
msg.set_content("Hey, {} is now available in stock\n"
                "Check it out soon : {}".format('hamza', 'google.com'))
with smtplib.SMTP('smtp.gmail.com', 587) as smtp:
    smtp.ehlo()
    smtp.starttls()
    smtp.ehlo()
    smtp.login('gmail', 'code')
    smtp.sendmail('sender', 'reciever', msg.as_string())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM