Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > send email with bcc

Reply
Thread Tools

send email with bcc

 
 
Ed
Guest
Posts: n/a
 
      11-30-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 = '(E-Mail Removed)'
cc = '(E-Mail Removed)'
bcc = '(E-Mail Removed)'
sender = '(E-Mail 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
 
Reply With Quote
 
 
 
 
Tim Golden
Guest
Posts: n/a
 
      11-30-2012
On 30/11/2012 20:25, Ed wrote:
> to = '(E-Mail Removed)'
> bcc = '(E-Mail 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 Removed)'
>>> bcc = '(E-Mail Removed)'
>>> [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
 
Reply With Quote
 
 
 
 
Ervin Hegedüs
Guest
Posts: n/a
 
      11-30-2012
Hello,

On Fri, Nov 30, 2012 at 12:25:37PM -0800, Ed wrote:
>
> # 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+[s]


a.


--
I � UTF-8
 
Reply With Quote
 
Ed
Guest
Posts: n/a
 
      11-30-2012
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
 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      12-01-2012
On Fri, 30 Nov 2012 12:25:37 -0800 (PST), Ed <(E-Mail Removed)>
declaimed the following in gmane.comp.python.general:


> # 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...

>>> [to] + [cc] + [bcc]

['(E-Mail Removed)', '(E-Mail Removed)', '(E-Mail Removed)']

But note that this won't work if one of the three is already a list!

>>> bcc = ["(E-Mail Removed)", "someone.else"]
>>> [to] + [cc] + [bcc]

['(E-Mail Removed)', '(E-Mail Removed)', ['(E-Mail Removed)', 'someone.else']]

If you ensure that each address block starts as a list, you can...

>>> to = ["(E-Mail Removed)"]
>>> cc = ["(E-Mail Removed)"]
>>> bcc = ["(E-Mail Removed)", "someone.else"]
>>> add = []
>>> add.extend(to)
>>> add.extend(cc)
>>> add.extend(bcc)
>>> add

['(E-Mail Removed)', '(E-Mail Removed)', '(E-Mail Removed)', 'someone.else']
>>>



--
Wulfraed Dennis Lee Bieber AF6VN
http://www.velocityreviews.com/forums/(E-Mail Removed) HTTP://wlfraed.home.netcom.com/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Lycos email triggering bcc error cacoggins@gmail.com Computer Support 2 02-20-2006 08:23 AM
BCC in Thunderbird Shelly Firefox 11 10-02-2004 08:25 PM
Email Cc & Bcc Matthew Computer Support 8 11-20-2003 10:00 PM
OE sent items: How can I see the bcc? who did I send to ? lbbs Computer Support 1 10-27-2003 02:10 AM
Re: does anybody know how to send a BCC Patrick Computer Support 4 08-14-2003 04:58 AM



Advertisments