问题:
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?