Discussion:
send email with bcc
Ed
2012-11-30 20:25:37 UTC
Permalink
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
Tim Golden
2012-11-30 20:40:27 UTC
Permalink
Post by Ed
to = 'ed at domain.gov'
bcc = 'ed at domain.net'
[... snippage ...]
Post by Ed
smtp.sendmail(sender, [to] + bcc, msg.as_string())
Well, you crucially don't show us the rest of the traceback. But I
imagine you'd have got something like this:

<dump>

ActivePython 2.7.2.5 (ActiveState Software Inc.) based on
Python 2.7.2 (default, Jun 24 2011, 12:21:10) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Post by Ed
Post by Ed
to = 'ed at domain.gov'
bcc = 'ed at domain.net'
[to] + bcc
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "str") to list
</dump>

which suggests, fairly clearly, that you're trying to concatenate a
string and a list.

TJG
Ervin Hegedüs
2012-11-30 21:03:29 UTC
Permalink
Hello,
Post by Ed
# Send the email
smtp.sendmail(sender, [to] + bcc, msg.as_string())
File "/opt/batch/ebtest/example4.py", line 46, in <module>
smtp.sendmail(sender, [to] + bcc, msg.as_string())
didn't you forgot to attach the reason of the error, I mean what
is the Exception type?
Post by Ed
Other iterations of the code have worked without error, but do not send mail to the BCC recipient.
you could't concatenate a list and a string at this way.

You can do that one of these:

l = ['foo']
s = 'bar'

l.append(s)

or

n = l+[s]


a.
--
I ? UTF-8
Ed
2012-11-30 22:35:14 UTC
Permalink
Oops. Sorry 'bout that. I somehow didn't grab the entire traceback info. Well, turns out one if not more of my BCC attempts is working. Just thought to check my spam filter of the BCC email address and there are quite a few messages in there. I've got it working now.

Thanks to you both for the quick responses!!
~Ed

Continue reading on narkive:
Loading...