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

Email headers don’t matter to the smtp server. Just add CC and BCC recipients to toaddrs when sending emails. For CC, add them to the CC header.

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()

Leave a Comment