send email with bcc

Ed akegb3 at gmail.com
Fri Nov 30 21:25:37 CET 2012


Hi,

I have found many different suggestions on "how to" code python to send a bcc email. None of which have worked in my environment - yet. Following is what I have at this time, which is erroring. Most of the code I've found will successfully send the message to the TO & CC recipients, but not the BCC. Any help is very much appreciated. Thx, Ed <Python novice>.

<snip>
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from smtplib import SMTP

to = 'ed at domain.gov'
cc = 'ed at gmailmail.com'
bcc = 'ed at domain.net'
sender = 'edsboss at domain.gov'

msg = MIMEMultipart()
msg['To'] = to
msg['Cc'] = cc
msg['Subject'] = 'Monthly Report'
msg['From'] = sender

smtp = SMTP("smtp.server.com")
# Start the server:
smtp.ehlo()

# Send the email
smtp.sendmail(sender, [to] + bcc, msg.as_string())

The above generates the following error:
Traceback (most recent call last):
  File "/opt/batch/ebtest/example4.py", line 46, in <module>
    smtp.sendmail(sender, [to] + bcc, msg.as_string())

Other iterations of the code have worked without error, but do not send mail to the BCC recipient.

Thanks in advance for any assistance!
~Ed



More information about the Python-list mailing list