我需要測試的目的是用各種消息來填充幾百個電子郵箱,並且會爲此使用smtplib。但除此之外,我需要能夠不僅發送郵件到特定的郵箱,而且CC和BCC也是如此。它看起來不像 smtplib在發送電子郵件時支持CC-ing和BCC-ing。python:如何用TO,CC和BCC發送郵件?
尋找建議如何執行CC或BCC從python腳本發送消息。
(和 - 不,我不是在我的測試環境之外創建一個腳本,以垃圾郵件的人。)
我需要測試的目的是用各種消息來填充幾百個電子郵箱,並且會爲此使用smtplib。但除此之外,我需要能夠不僅發送郵件到特定的郵箱,而且CC和BCC也是如此。它看起來不像 smtplib在發送電子郵件時支持CC-ing和BCC-ing。python:如何用TO,CC和BCC發送郵件?
尋找建議如何執行CC或BCC從python腳本發送消息。
(和 - 不,我不是在我的測試環境之外創建一個腳本,以垃圾郵件的人。)
電子郵件標題別無論對於SMTP服務器。發送電子郵件時,只需將CC和BCC收件人添加到toaddrs即可。對於CC,將它們添加到CC標頭。
toaddr = '[email protected]'
cc = ['[email protected]','[email protected]']
bcc = ['[email protected]']
fromaddr = '[email protected]'
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()
TO,CC之間的區別和BCC只有在文本標題出現。在SMTP級別,每個人都是收件人。
TO - 有一個TO:本收件人的地址
CC頭 - 有一個CC:本收件人的地址
BCC頭 - 這方未在標題中提到的所有,但仍是收件人。
如果你有
TO: [email protected]
CC: [email protected]
BCC: [email protected]
你有三個收件人。在電子郵件正文的標題將只包括TO:和CC:
您可以嘗試MimeText用於
msg = MIMEText('text')
msg['to'] =
msg['cc'] =
然後發送msg.as_string()
請勿添加密件抄送標題。
看到這個:http://mail.python.org/pipermail/email-sig/2004-September/000151.html
而且這樣的:「」「注意,第二個參數sendmail的(),收件人,作爲一個列表被傳遞您可以在列表中的任何數量的地址有。消息通過包括他們在法的說法,但沒有在郵件標題中依次傳遞給每個人。由於包絡信息從郵件頭分開的,你甚至可以BCC的人。「」」從http://pymotw.com/2/smtplib
toaddr = '[email protected]'
cc = ['[email protected]','[email protected]']
bcc = ['[email protected]']
fromaddr = '[email protected]'
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()
豎起大拇指:D – Chris 2017-06-28 08:48:13
關鍵是將收件人添加爲電子郵件ID列表在你的sendmail調用中。
import smtplib
from email.mime.multipart import MIMEMultipart
me = "[email protected]"
to = "[email protected]"
cc = "[email protected],[email protected]"
bcc = "[email protected],[email protected]"
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()
在這個問題中還要求提供密件抄送字段。 – 2015-07-15 18:05:15
您也可以以類似的方式添加密送。我更新了片段。 – helios 2015-07-18 19:01:31
離開'msg ['BCC']' - 它揭示了你隱藏的發件人,並且不會影響郵件是否發送給他們(sendmail的參數就是這樣做的)。 – 2017-10-10 14:31:03
,直到我創造它沒有爲我工作:
#created cc string
cc = ""[email protected];
#added cc to header
msg['Cc'] = cc
,比收件人[名單]新增CC這樣的:
s.sendmail(me, [you,cc], msg.as_string())
smtplib.SMTP不會發送地址列表。至少不是python 2.7.2 – LostMohican 2012-04-05 08:06:20
在這種情況下,RFC 2822中提到的BCC頭沒有任何意義。 – 2012-04-21 09:34:59
@ABentSpoon冒號在'主題'後面缺少。 – user891260 2012-06-25 08:04:13