python:如何用TO,CC和BCC发邮件? [英] python: how to send mail with TO, CC and BCC?

查看:389
本文介绍了python:如何用TO,CC和BCC发邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要测试目的,用各种信息填充几百个电子邮箱,并将使用smtplib。但除其他事项外,我还需要能够发送消息,不仅可以发送到特定邮箱,还可以发送CC和BCC。它不像 smtplib 在发送电子邮件时支持CC-ing和BCC。 / p>

寻找建议如何从python脚本中执行CC或BCC发送消息。



(和 - 不,我不会在我的测试环境之外创建一个脚本来垃圾邮件。)

解决方案

电子邮件头对于smtp服务器。发送电子邮件时,只需将CC和BCC收件人添加到收件人。对于CC,将它们添加到CC头。

  toaddr ='buffy@sunnydale.k12.ca.us'
cc = ['alexander@sunydale.k12.ca.us','willow@sunnydale.k12.ca.us']
bcc = ['chairman@slayerscouncil.uk']
fromaddr =' giles@sunnydale.k12.ca.us'
message_subject =第7区的干扰
message_text =在第7区下方的下水道中有3人死亡。
message =From:%s\r\\\
%fromaddr
+至:%s\r\\\
%toaddr
+CC:%s\\ \\ r \\ n%,。join(cc)
+主题:%s\r\\\
%message_subject
+\r\\\

+ message_text
toaddrs = [toaddr] + cc + bcc
server = smtplib.SMTP('smtp.sunnydale.k12.ca.us')
server.set_debuglevel(1)
server.sendmail(fromaddr,toaddrs,message)
server.quit()


I need for testing purposes to populate few hundred email boxes with various messages, and was going to use smtplib for that. But among other things I need to be able to send messages not only TO specific mailboxes, but CC and BCC them as well. It does not look like smtplib supports CC-ing and BCC-ing while sending emails.

Looking for suggestions how to do CC or BCC sending messages from the python script.

(And — no, I'm not creating a script to spam anyone outside of my testing environment.)

解决方案

Email headers don't matter to the smtp server. Just add the CC and BCC recipients to the toaddrs when you send your email. For CC, add them to the CC header.

toaddr = 'buffy@sunnydale.k12.ca.us'
cc = ['alexander@sunydale.k12.ca.us','willow@sunnydale.k12.ca.us']
bcc = ['chairman@slayerscouncil.uk']
fromaddr = 'giles@sunnydale.k12.ca.us'
message_subject = "disturbance in sector 7"
message_text = "Three are dead in an attack in the sewers below sector 7."
message = "From: %s\r\n" % fromaddr
        + "To: %s\r\n" % toaddr
        + "CC: %s\r\n" % ",".join(cc)
        + "Subject: %s\r\n" % message_subject
        + "\r\n" 
        + message_text
toaddrs = [toaddr] + cc + bcc
server = smtplib.SMTP('smtp.sunnydale.k12.ca.us')
server.set_debuglevel(1)
server.sendmail(fromaddr, toaddrs, message)
server.quit()

这篇关于python:如何用TO,CC和BCC发邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆