如何用 TO、 CC 和 BCC 发送邮件?

为了测试目的,我需要用不同的消息填充几百个电子邮箱,我打算使用 smtplib。但在其他事情中,我需要能够发送消息不仅到特定的邮箱,但 CC 和密件抄送他们以及。看起来 Smtplib在发送电子邮件时不支持 CC-ing 和 BCC-ing。

寻找关于如何从 python 脚本发送 CC 或 BCC 消息的建议。

(而且ーー不,我不会创建一个脚本来向测试环境之外的任何人发送垃圾邮件。)

229666 次浏览

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

电子邮件头对 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\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()

不要添加 bcc 头。

看这个: 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\r\n" % fromaddr
+ "To: %s\r\n" % toaddr
+ "CC: %s\r\n" % ",".join(cc)
# don't add this, otherwise "to and cc" receivers will know who are the bcc receivers
# + "BCC: %s\r\n" % ",".join(bcc)
+ "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()

关键是在 sendmail 调用中将收件人作为 电子邮件编号列表添加。

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.attach(my_msg_body)
server = smtplib.SMTP("localhost") # or your smtp server
server.sendmail(me, rcpt, msg.as_string())
server.quit()

直到我创造出:

#created cc string
cc = ""someone@domain.com;
#added cc to header
msg['Cc'] = cc

然后在收件人[列表]中添加 cc,如:

s.sendmail(me, [you,cc], msg.as_string())

从2011年11月发布的 Python 3.2开始,smtplib 有了一个新的函数 send_message,而不仅仅是 sendmail,这使得处理 To/CC/BCC 变得更加容易。从 Python 官方电子邮件示例中取出,稍作修改,我们得到:

# Import smtplib for the actual sending function
import smtplib


# Import the email modules we'll need
from email.message import EmailMessage


# Open the plain text file whose name is in textfile for reading.
with open(textfile) as fp:
# Create a text/plain message
msg = EmailMessage()
msg.set_content(fp.read())


# me == the sender's email address
# you == the recipient's email address
# them == the cc's email address
# they == the bcc's email address
msg['Subject'] = 'The contents of %s' % textfile
msg['From'] = me
msg['To'] = you
msg['Cc'] = them
msg['Bcc'] = they




# Send the message via our own SMTP server.
s = smtplib.SMTP('localhost')
s.send_message(msg)
s.quit()

使用标题工作得很好,因为 Send _ message 尊重文档中概述的 BCC:

Send _ message 不传输任何可能出现在 msg 中的 Bcc 或 Resent-Bcc 头


对于 sendmail,通常会将 CC 头添加到消息中,执行以下操作:

msg['Bcc'] = blind.email@adrress.com

或者

msg = "From: from.email@address.com" +
"To: to.email@adress.com" +
"BCC: hidden.email@address.com" +
"Subject: You've got mail!" +
"This is the message body"

问题是,sendmail 函数对所有这些头都是一样的,这意味着它们将被(可见地)发送给所有 To: 和 BCC: users,这违背了 BCC 的目的。解决方案,正如这里的许多其他答案所显示的那样,是不在标题中包含 BCC,而只在传递给 sendmail的电子邮件列表中包含 BCC。

需要注意的是,send_message需要一个 Message 对象,这意味着您需要从 email.message导入一个类,而不仅仅是将字符串传递给 sendmail

以上这些都不适合我,因为我有多个收件人都在“到”和“抄送”。所以我试了下面的方法:

recipients = ['abc@gmail.com', 'xyz@gmail.com']
cc_recipients = ['lmn@gmail.com', 'pqr@gmail.com']
MESSAGE['To'] = ", ".join(recipients)
MESSAGE['Cc'] = ", ".join(cc_recipients)

然后用“ cc _ Receiders”扩展“收件人”,并以简单的方式发送邮件

recipients.extend(cc_recipients)
server.sendmail(FROM,recipients,MESSAGE.as_string())