经过大量搜索后,我无法找到如何使用 smtplib.sendmail 发送给多个收件人。问题是每次发送邮件时,邮件标头都会显示包含多个地址,但实际上只有第一个收件人会收到电子邮件。
问题似乎在于email.Message http://docs.python.org/library/email.message.html#email.message.Message模块期望的东西与smtplib.sendmail() http://docs.python.org/library/smtplib.html#smtplib.SMTP.sendmail功能。
简而言之,要发送给多个收件人,您应该将标头设置为一串以逗号分隔的电子邮件地址。这sendmail()
范围to_addrs
但是应该是电子邮件地址列表。
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
import smtplib
msg = MIMEMultipart()
msg["Subject"] = "Example"
msg["From"] = "[email protected] /cdn-cgi/l/email-protection"
msg["To"] = "[email protected] /cdn-cgi/l/email-protection,[email protected] /cdn-cgi/l/email-protection,[email protected] /cdn-cgi/l/email-protection"
msg["Cc"] = "[email protected] /cdn-cgi/l/email-protection,[email protected] /cdn-cgi/l/email-protection"
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()
This 真的有效,我花了很多时间尝试多种变体。
import smtplib
from email.mime.text import MIMEText
s = smtplib.SMTP('smtp.uk.xensource.com')
s.set_debuglevel(1)
msg = MIMEText("""body""")
sender = '[email protected] /cdn-cgi/l/email-protection'
recipients = ['[email protected] /cdn-cgi/l/email-protection', '[email protected] /cdn-cgi/l/email-protection']
msg['Subject'] = "subject line"
msg['From'] = sender
msg['To'] = ", ".join(recipients)
s.sendmail(sender, recipients, msg.as_string())
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)