Answers:
电子邮件标头与smtp服务器无关。发送电子邮件时,只需将抄送和密件抄送收件人添加到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()
关键是将收件人添加为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.attach(my_msg_body)
server = smtplib.SMTP("localhost") # or your smtp server
server.sendmail(me, rcpt, msg.as_string())
server.quit()
msg['BCC']
-可以显示您的隐藏发件人,并且对是否将邮件发送给他们没有影响(执行此操作的参数sendmail
)。
不要添加密件抄送头。
看到这个:http : //mail.python.org/pipermail/email-sig/2004-September/000151.html
并且此:“”“注意,sendmail()的第二个参数(收件人)作为一个列表传递。您可以在列表中包括任意数量的地址,以将消息依次传递给它们。信息与消息头是分开的,您甚至可以通过在方法参数中包括消息而不是消息头来将其密送给BCC。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()
从2011年11月发布的Python 3.2开始,smtplib具有一个新功能,send_message
而不是just 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不传输可能在味精中显示的任何密件抄送或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:用户,这违背了BCC的目的。如此处其他许多答案所示,解决方案是在标题中不包含密件抄送,而仅在传递给的电子邮件列表中包含BCC sendmail
。
需要注意的是,它send_message
需要一个Message对象,这意味着您需要从中导入一个类,email.message
而不仅仅是将字符串传递到中sendmail
。
您可以尝试MIMEText
msg = MIMEText('text')
msg['to'] =
msg['cc'] =
然后发送msg.as_string()
在我创建之前,它对我不起作用:
#created cc string
cc = ""someone@domain.com;
#added cc to header
msg['Cc'] = cc
并且在收件人[列表]中添加了抄送,例如:
s.sendmail(me, [you,cc], msg.as_string())
由于我在“收件人”和“抄送”中都有多个收件人,因此上述所有内容都不适合我。所以我尝试如下:
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_recipients”扩展“收件人”并以平凡的方式发送邮件
recipients.extend(cc_recipients)
server.sendmail(FROM,recipients,MESSAGE.as_string())