python:如何使用TO,CC和BCC发送邮件?

时间:2022-08-01 18:14:20

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.

我需要用于测试目的,用各种消息填充几百个电子邮箱,并且将使用smtplib。但除此之外,我需要能够不仅向特定邮箱发送消息,还要向CC和BCC发送消息。在发送电子邮件时,它看起来不像smtplib支持CC-ing和BCC。

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

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

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

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

6 个解决方案

#1


116  

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.

电子邮件标头与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()

#2


15  

The distinction between TO, CC and BCC occurs only in the text headers. At the SMTP level, everybody is a recipient.

TO,CC和BCC之间的区别仅出现在文本标题中。在SMTP级别,每个人都是收件人。

TO - There is a TO: header with this recipient's address

TO - 收件人的地址有一个TO:标题

CC - There is a CC: header with this recipient's address

CC - 有一个带有此收件人地址的CC:标题

BCC - This recipient isn't mentioned in the headers at all, but is still a recipient.

BCC - 标题中根本没有提到此收件人,但仍然是收件人。

If you have

如果你有

TO: abc@company.com
CC: xyz@company.com
BCC: boss@company.com

You have three recipients. The headers in the email body will include only the TO: and CC:

你有三个收件人。电子邮件正文中的标题将仅包含TO:和CC:

#3


14  

You can try MIMEText

你可以试试MIMEText

msg = MIMEText('text')
msg['to'] = 
msg['cc'] = 

then send msg.as_string()

然后发送msg.as_string()

http://docs.python.org/library/email-examples.html

http://docs.python.org/library/email-examples.html

#4


11  

Don't add the bcc header.

不要添加密件抄送头。

See this: http://mail.python.org/pipermail/email-sig/2004-September/000151.html

见:http://mail.python.org/pipermail/email-sig/2004-September/000151.html

And this: """Notice that the second argument to sendmail(), the recipients, is passed as a list. You can include any number of addresses in the list to have the message delivered to each of them in turn. Since the envelope information is separate from the message headers, you can even BCC someone by including them in the method argument but not in the message header.""" from http://pymotw.com/2/smtplib

并且:“”注意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()

#5


11  

Key thing is to add the recipients as a list of email ids in your sendmail call.

关键是将收件人添加为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()

#6


3  

It did not worked for me until i created:

直到我创建它才对我有用:

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

and than added cc in recipient [list] like:

并且在收件人[list]中添加了cc,如:

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

#1


116  

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.

电子邮件标头与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()

#2


15  

The distinction between TO, CC and BCC occurs only in the text headers. At the SMTP level, everybody is a recipient.

TO,CC和BCC之间的区别仅出现在文本标题中。在SMTP级别,每个人都是收件人。

TO - There is a TO: header with this recipient's address

TO - 收件人的地址有一个TO:标题

CC - There is a CC: header with this recipient's address

CC - 有一个带有此收件人地址的CC:标题

BCC - This recipient isn't mentioned in the headers at all, but is still a recipient.

BCC - 标题中根本没有提到此收件人,但仍然是收件人。

If you have

如果你有

TO: abc@company.com
CC: xyz@company.com
BCC: boss@company.com

You have three recipients. The headers in the email body will include only the TO: and CC:

你有三个收件人。电子邮件正文中的标题将仅包含TO:和CC:

#3


14  

You can try MIMEText

你可以试试MIMEText

msg = MIMEText('text')
msg['to'] = 
msg['cc'] = 

then send msg.as_string()

然后发送msg.as_string()

http://docs.python.org/library/email-examples.html

http://docs.python.org/library/email-examples.html

#4


11  

Don't add the bcc header.

不要添加密件抄送头。

See this: http://mail.python.org/pipermail/email-sig/2004-September/000151.html

见:http://mail.python.org/pipermail/email-sig/2004-September/000151.html

And this: """Notice that the second argument to sendmail(), the recipients, is passed as a list. You can include any number of addresses in the list to have the message delivered to each of them in turn. Since the envelope information is separate from the message headers, you can even BCC someone by including them in the method argument but not in the message header.""" from http://pymotw.com/2/smtplib

并且:“”注意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()

#5


11  

Key thing is to add the recipients as a list of email ids in your sendmail call.

关键是将收件人添加为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()

#6


3  

It did not worked for me until i created:

直到我创建它才对我有用:

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

and than added cc in recipient [list] like:

并且在收件人[list]中添加了cc,如:

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