send email with bcc

E

Ed

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 = '(e-mail address removed)'
cc = '(e-mail address removed)'
bcc = '(e-mail address removed)'
sender = '(e-mail address removed)'

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
 
T

Tim Golden

to = '(e-mail address removed)'
bcc = '(e-mail address removed)'

[... snippage ...]
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.
to = '(e-mail address removed)'
bcc = '(e-mail address removed)'
[to] + bcc
Traceback (most recent call last):

</dump>

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

TJG
 
E

Ervin Hegedüs

Hello,

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

didn't you forgot to attach the reason of the error, I mean what
is the Exception type?
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+


a.
 
E

Ed

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
 
D

Dennis Lee Bieber

# Start the server:
smtp.ehlo()
Technically, the server is always running -- this just connects you
(as a CLIENT) to that running server.
# Send the email
smtp.sendmail(sender, [to] + bcc, msg.as_string())

I notice that you left OUT the CC addresses.
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())

Pardon --- you failed to include the ERROR, you've only supplied the
traceback identifying which line failed.

However, presuming the error is

TypeError: can only concatenate list (not "str") to list

then the solution is obvious... DON'T try to use "+" with a string on
one side and a list on the other...
['(e-mail address removed)', '(e-mail address removed)', '(e-mail address removed)']

But note that this won't work if one of the three is already a list!
bcc = ["(e-mail address removed)", "someone.else"]
[to] + [cc] + [bcc]
['(e-mail address removed)', '(e-mail address removed)', ['(e-mail address removed)', 'someone.else']]

If you ensure that each address block starts as a list, you can...
to = ["(e-mail address removed)"]
cc = ["(e-mail address removed)"]
bcc = ["(e-mail address removed)", "someone.else"]
add = []
add.extend(to)
add.extend(cc)
add.extend(bcc)
add ['(e-mail address removed)', '(e-mail address removed)', '(e-mail address removed)', 'someone.else']
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top