Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #34108 > unrolled thread

send email with bcc

Started byEd <akegb3@gmail.com>
First post2012-11-30 12:25 -0800
Last post2012-12-01 14:41 -0500
Articles 5 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  send email with bcc Ed <akegb3@gmail.com> - 2012-11-30 12:25 -0800
    Re: send email with bcc Tim Golden <mail@timgolden.me.uk> - 2012-11-30 20:40 +0000
    Re: send email with bcc Ervin Hegedüs <airween@gmail.com> - 2012-11-30 22:03 +0100
    Re: send email with bcc Ed <akegb3@gmail.com> - 2012-11-30 14:35 -0800
    Re: send email with bcc Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-12-01 14:41 -0500

#34108 — send email with bcc

FromEd <akegb3@gmail.com>
Date2012-11-30 12:25 -0800
Subjectsend email with bcc
Message-ID<9fedf732-2a8e-4445-bdce-b88d53f4540c@googlegroups.com>
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@domain.gov'
cc = 'ed@gmailmail.com'
bcc = 'ed@domain.net'
sender = 'edsboss@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

[toc] | [next] | [standalone]


#34109

FromTim Golden <mail@timgolden.me.uk>
Date2012-11-30 20:40 +0000
Message-ID<mailman.393.1354308034.29569.python-list@python.org>
In reply to#34108
On 30/11/2012 20:25, Ed wrote:
> to = 'ed@domain.gov'
> bcc = 'ed@domain.net'

[... 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 = 'ed@domain.gov'
 >>> bcc = 'ed@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

[toc] | [prev] | [next] | [standalone]


#34110

FromErvin Hegedüs <airween@gmail.com>
Date2012-11-30 22:03 +0100
Message-ID<mailman.394.1354309583.29569.python-list@python.org>
In reply to#34108
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

[toc] | [prev] | [next] | [standalone]


#34112

FromEd <akegb3@gmail.com>
Date2012-11-30 14:35 -0800
Message-ID<323db439-f559-49f3-a890-59a911f586ac@googlegroups.com>
In reply to#34108
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

[toc] | [prev] | [next] | [standalone]


#34128

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2012-12-01 14:41 -0500
Message-ID<mailman.405.1354390881.29569.python-list@python.org>
In reply to#34108
On Fri, 30 Nov 2012 12:25:37 -0800 (PST), Ed <akegb3@gmail.com>
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]
['ed@domain.gov', 'ed@gmailmail.com', 'ed@domain.net']

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

>>> bcc = ["ed@domain.net", "someone.else"]
>>> [to] + [cc] + [bcc]
['ed@domain.gov', 'ed@gmailmail.com', ['ed@domain.net', 'someone.else']]

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

>>> to = ["ed@domain.gov"]
>>> cc = ["ed@gmailmail.com"]
>>> bcc = ["ed@domain.net", "someone.else"]
>>> add = []
>>> add.extend(to)
>>> add.extend(cc)
>>> add.extend(bcc)
>>> add
['ed@domain.gov', 'ed@gmailmail.com', 'ed@domain.net', 'someone.else']
>>> 


-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
        wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web