Python: how to send mail with TO, CC and BCC?

I needed for testing to fill several hundred email accounts with various messages and was going to use smtplib for this. But, among other things, I need to be able to send messages not only to special mailboxes, but also CC and BCC. This is not like smtplib supports CC-ing and BCC-ing when sending emails.

Look for suggestions on how to make CC or BCC messages from a python script message.

(And - no, I am not creating a script to spam anyone outside my test environment.)

+68
python email testing
09 Oct '09 at 22:29
source share
6 answers

Email headers do not matter for the smtp server. Just add CC and BCC recipients to toddrs when sending email. For CC, add them to the CC header.

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() 
+101
09 Oct '09 at 22:52
source share

You can try MIMEText

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

then send the message msg.as_string ()

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

+15
Oct 09 '09 at 22:42
source share

The distinction between TO, CC, and BCC only occurs in the text headers. At the SMTP level, all recipients.

TO - TO header: with recipient address

CC - CC header: with destination address

BCC - This recipient is not mentioned at all in the headers, but is still the recipient.

If you

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

You have three recipients. Headings in the body of the letter will include only TO: and CC:

+14
Oct 09 '09 at 22:41
source share

Do not add a bcc header.

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

And this: "" Note that the second argument to sendmail (), the recipients, is passed as a list. You can include any number of addresses in the list so that a message is sent to each of them in turn. Since envelope information is separate from message headers, you can even associate it with an object in a method argument, but not in a message header. "From 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() 
+10
Apr 14 '15 at 12:17
source share

The key is to add recipients to the list of email IDs in your sendmail call.

 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() 
+8
Apr 29 '15 at 6:57
source share

This did not work for me until I created:

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

and then cc is added to the receiver [list], for example:

 s.sendmail(me, [you,cc], msg.as_string()) 
+1
Jan 13 '17 at 9:47 on
source share



All Articles