python:如何使用TO,CC和BCC发送邮件?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python:如何使用TO,CC和BCC发送邮件?相关的知识,希望对你有一定的参考价值。
我需要用于测试目的,用各种消息填充几百个电子邮箱,并且将使用smtplib。但除此之外,我需要能够不仅向特定邮箱发送消息,还要向CC和BCC发送消息。看起来 smtplib 在发送电子邮件时不支持CC-ing和BCC。
寻找如何做CC或BCC从python脚本发送消息的建议。
(并且 - 不,我没有创建一个脚本来向我的测试环境之外的任何人发送垃圾邮件。)
电子邮件标头与smtp服务器无关。只需在发送电子邮件时将CC和BCC收件人添加到toaddrs即可。对于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 = "disturbance in sector 7"
message_text = "Three are dead in an attack in the sewers below sector 7."
message = "From: %s
" % fromaddr
+ "To: %s
" % toaddr
+ "CC: %s
" % ",".join(cc)
+ "Subject: %s
" % message_subject
+ "
"
+ 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()
关键是将收件人添加为sendmail呼叫中的电子邮件ID列表。
import smtplib
from email.mime.multipart import MIMEMultipart
me = "user63503@gmail.com"
to = "someone@gmail.com"
cc = "anotherperson@gmail.com,someone@yahoo.com"
bcc = "bccperson1@gmail.com,bccperson2@yahoo.com"
rcpt = cc.split(",") + bcc.split(",") + [to]
msg = MIMEMultipart('alternative')
msg['Subject'] = "my subject"
msg['To'] = to
msg['Cc'] = cc
msg['Bcc'] = bcc
msg.attach(my_msg_body)
server = smtplib.SMTP("localhost") # or your smtp server
server.sendmail(me, rcpt, msg.as_string())
server.quit()
不要添加密件抄送头。
见:http://mail.python.org/pipermail/email-sig/2004-September/000151.html
并且:“”注意sendmail()的第二个参数(收件人)作为列表传递。您可以在列表中包含任意数量的地址,以便依次将消息传递给每个地址。信息与消息头分开,你甚至可以通过将它们包含在方法参数中而不是消息头中来阻止某人。“”来自http://pymotw.com/2/smtplib
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
" % fromaddr
+ "To: %s
" % toaddr
+ "CC: %s
" % ",".join(cc)
# don't add this, otherwise "to and cc" receivers will know who are the bcc receivers
# + "BCC: %s
" % ",".join(bcc)
+ "Subject: %s
" % message_subject
+ "
"
+ 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()
TO,CC和BCC之间的区别仅出现在文本标题中。在SMTP级别,每个人都是收件人。
TO - 收件人的地址有一个TO:标题
CC - 有一个带有此收件人地址的CC:标题
BCC - 标题中根本没有提到此收件人,但仍然是收件人。
如果你有
TO: abc@company.com
CC: xyz@company.com
BCC: boss@company.com
你有三个收件人。电子邮件正文中的标题将仅包含TO:和CC:
你可以试试MIMEText
msg = MIMEText('text')
msg['to'] =
msg['cc'] =
然后发送msg.as_string()
https://docs.python.org/3.6/library/email.examples.html
直到我创建它才对我有用:
#created cc string
cc = ""someone@domain.com;
#added cc to header
msg['Cc'] = cc
并且在收件人[list]中添加了cc,如:
s.sendmail(me, [you,cc], msg.as_string())
以上是关于python:如何使用TO,CC和BCC发送邮件?的主要内容,如果未能解决你的问题,请参考以下文章
python使用SMTP发邮件时使用Cc(抄送)和Bcc(密送)
使用 PHP PEAR MAIL 发送多个 CC 和 BCC