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

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


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.

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_texttoaddrs = [toaddr] + cc + bccserver = smtplib.SMTP('smtp.sunnydale.k12.ca.us')server.set_debuglevel(1)server.sendmail(fromaddr, toaddrs, message)server.quit()


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

import smtplibfrom email.mime.multipart import MIMEMultipartme = "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'] = tomsg['Cc'] = ccmsg.attach(my_msg_body)server = smtplib.SMTP("localhost") # or your smtp serverserver.sendmail(me, rcpt, msg.as_string())server.quit()


As of Python 3.2, released Nov 2011, the smtplib has a new function send_message instead of just sendmail, which makes dealing with To/CC/BCC easier. Pulling from the Python official email examples, with some slight modifications, we get:

# Import smtplib for the actual sending functionimport smtplib# Import the email modules we'll needfrom 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 addressmsg['Subject'] = 'The contents of %s' % textfilemsg['From'] = memsg['To'] = youmsg['Cc'] = themmsg['Bcc'] = they# Send the message via our own SMTP server.s = smtplib.SMTP('localhost')s.send_message(msg)s.quit()

Using the headers work fine, because send_message respects BCC as outlined in the documentation:

send_message does not transmit any Bcc or Resent-Bcc headers that may appear in msg


With sendmail it was common to add the CC headers to the message, doing something such as:

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

Or

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"

The problem is, the sendmail function treats all those headers the same, meaning they'll get sent (visibly) to all To: and BCC: users, defeating the purposes of BCC. The solution, as shown in many of the other answers here, was to not include BCC in the headers, and instead only in the list of emails passed to sendmail.

The caveat is that send_message requires a Message object, meaning you'll need to import a class from email.message instead of merely passing strings into sendmail.