簡體   English   中英

smtplib 沒有發送 email 並且沒有出現錯誤

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

我正在嘗試通過 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)

我通過 cmd 運行它,但沒有出現或發生任何事情,我在我的 gmail 帳戶中打開了較少的安全應用程序並關閉了兩步驗證。 提前致謝

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

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

因此,代碼應如下所示:

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