如何使用python smtplib发送电子邮件给多个收件人?

经过多次search,我找不到如何使用smtplib.sendmail发送给多个收件人。 问题是每次发送邮件邮件标题将显示包含多个地址,但实际上只有第一个收件人会收到电子邮件。

问题似乎是, email.Message模块需要与smtplib.sendmail()函数不同的东西。

简而言之,要发送给多个收件人,您应该将标题设置为由逗号分隔的电子邮件地址的string。 sendmail()参数to_addrs应该是电子邮件地址列表。

 from email.MIMEMultipart import MIMEMultipart from email.MIMEText import MIMEText import smtplib msg = MIMEMultipart() msg["Subject"] = "Example" msg["From"] = "me@example.com" msg["To"] = "malcom@example.com,reynolds@example.com,firefly@example.com" msg["Cc"] = "serenity@example.com,inara@example.com" 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() 

真的有用 ,我花了很多时间尝试多个变种。

 import smtplib from email.mime.text import MIMEText s = smtplib.SMTP('smtp.uk.xensource.com') s.set_debuglevel(1) msg = MIMEText("""body""") sender = 'me@example.com' recipients = ['john.doe@example.com', 'john.smith@example.co.uk'] msg['Subject'] = "subject line" msg['From'] = sender msg['To'] = ", ".join(recipients) s.sendmail(sender, recipients, msg.as_string()) 

msg['To']需要是一个string:

 msg['To'] = "a@b.com, b@b.com, c@b.com" 

sendmail(sender, recipients, message)需要是一个列表:

 sendmail("a@a.com", ["a@b.com", "b@b.com", "c@b.com"], "Howdy") 

您需要了解电子邮件的可见地址与交付之间的区别。

msg["To"]实际上是打印在信件上的。 它实际上没有任何作用。 除了你的电子邮件客户端,就像普通邮政官员一样,你会认为这是你要发送邮件的人。

然而实际的交付可能会有很大的不同。 因此,您可以将电子邮件(或副本)放入与完全不同的人的邮箱中。

这有很多原因。 例如转发To:头字段不会在转发时更改,但是电子邮件将被放入不同的邮箱。

smtp.sendmail命令现在负责实际的传送。 email.Message只是信件的内容,而不是交付。

在低级别的SMTP ,您需要一一给出接收者,这就是为什么地址列表(不包括名字!)是合理的API。

对于标题,它也可以包含例如名称,例如To: First Last <email@addr.tld>, Other User <other@mail.tld>因此,不推荐你的代码示例 ,因为它将无法传递这个邮件,因为只是分裂它,你仍然没有有效的地址!

它适用于我。

 import smtplib from email.mime.text import MIMEText s = smtplib.SMTP('smtp.uk.xensource.com') s.set_debuglevel(1) msg = MIMEText("""body""") sender = 'me@example.com' recipients = 'john.doe@example.com,john.smith@example.co.uk' msg['Subject'] = "subject line" msg['From'] = sender msg['To'] = recipients s.sendmail(sender, recipients.split(','), msg.as_string()) 

我想出了这个可导入的模块function。 在这个例子中它使用gmail邮件服务器。 它分成标题和消息,所以你可以清楚地看到发生了什么事情:

 import smtplib def send_alert(subject=""): to = ['email@one.com', 'email2@another_email.com', 'a3rd@email.com'] gmail_user = 'me@gmail.com' gmail_pwd = 'my_pass' smtpserver = smtplib.SMTP("smtp.gmail.com", 587) smtpserver.ehlo() smtpserver.starttls() smtpserver.ehlo smtpserver.login(gmail_user, gmail_pwd) header = 'To:' + ", ".join(to) + '\n' + 'From: ' + gmail_user + '\n' + 'Subject: ' + subject + '\n' msg = header + '\n' + subject + '\n\n' smtpserver.sendmail(gmail_user, to, msg) smtpserver.close() 

几个月前我想到了这个,并且在这里写了一篇文章 。 总结是:

如果您想使用smtplib将电子邮件发送给多个收件人,请使用email.Message.add_header('To', eachRecipientAsString)添加它们,然后在调用sendmail方法时use email.Message.get_all('To')发送消息给他们所有人。 对于抄送和密件抄送收件人也是如此。

那么, 这种asnwer方法中的方法不适合我。 我不知道,也许这是一个Python3(我使用3.4版本)或Gmail相关的问题,但经过一些尝试,为我工作的解决scheme,是线

 s.send_message(msg) 

代替

 s.sendmail(sender, recipients, msg.as_string()) 

我这样做:

 def send_mail msg['Subject'] = subject msg['From'] = sender msg['To'] = [email1@to.de,email2@to.de] msg['Cc'] = [email3@cc.de,email4@cc.de] msg['Bcc'] = [email5@bcc.de,email6@bcc.de] recips = [email1@to.de,email2@to.de,email3@cc.de,email4@cc.de,email5@bcc.de,email6@bcc.de] sendmail(sender, recips, msg.as_string()) msg['From'] = sender msg['To'] = [email1@to.de,email2@to.de] msg['Cc'] = [email3@cc.de,email4@cc.de] msg['Bcc'] = [email5@bcc.de,email6@bcc.de] recips = [email1@to.de,email2@to.de,email3@cc.de,email4@cc.de,email5@bcc.de,email6@bcc.de] sendmail(sender, recips, msg.as_string())