如何使用 python smtplib 向多個收件人發送電子郵件? - How to send email to multiple recipients using python smtplib?

問題:

After much searching I couldn't find out how to use smtplib.sendmail to send to multiple recipients.經過大量搜索,我無法找到如何使用 smtplib.sendmail 發送給多個收件人。 The problem was every time the mail would be sent the mail headers would appear to contain multiple addresses, but in fact only the first recipient would receive the email.問題是每次發送郵件時,郵件標題似乎都包含多個地址,但實際上只有第一個收件人會收到電子郵件。

The problem seems to be that the email.Message module expects something different than the smtplib.sendmail() function.問題似乎是email.Message模塊期望與smtplib.sendmail()函數不同的東西。

In short, to send to multiple recipients you should set the header to be a string of comma delimited email addresses.簡而言之,要發送給多個收件人,您應該將標題設置爲一串逗號分隔的電子郵件地址。 The sendmail() parameter to_addrs however should be a list of email addresses.但是, sendmail()參數to_addrs應該是電子郵件地址列表。

from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
import smtplib

msg = MIMEMultipart()
msg["Subject"] = "Example"
msg["From"] = "[email protected]"
msg["To"] = "[email protected],[email protected],[email protected]"
msg["Cc"] = "[email protected],[email protected]"
body = MIMEText("example email body")
msg.attach(body)
smtp = smtplib.SMTP("mailhost.example.com", 25)
smtp.sendmail(msg["From"], msg["To"].split(",") + msg["Cc"].split(","), msg.as_string())
smtp.quit()

解決方案:

參考一: https://stackoom.com/question/b9sb
參考二: How to send email to multiple recipients using python smtplib?
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章