send email with bcc

Discussion in 'Python' started by Ed, Nov 30, 2012.

  1. Ed

    Ed Guest

    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 = ''
    cc = ''
    bcc = ''
    sender = ''

    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
    Ed, Nov 30, 2012
    #1
    1. Advertising

  2. Ed

    Tim Golden Guest

    On 30/11/2012 20:25, Ed wrote:
    > to = ''
    > bcc = ''


    [... 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 = ''
    >>> bcc = ''
    >>> [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
    Tim Golden, Nov 30, 2012
    #2
    1. Advertising

  3. 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+


    a.


    --
    I � UTF-8
    Ervin Hegedüs, Nov 30, 2012
    #3
  4. Ed

    Ed Guest

    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
    Ed, Nov 30, 2012
    #4
  5. On Fri, 30 Nov 2012 12:25:37 -0800 (PST), Ed <>
    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]

    ['', '', '']

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

    >>> bcc = ["", "someone.else"]
    >>> [to] + [cc] + [bcc]

    ['', '', ['', 'someone.else']]

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

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

    ['', '', '', 'someone.else']
    >>>



    --
    Wulfraed Dennis Lee Bieber AF6VN
    HTTP://wlfraed.home.netcom.com/
    Dennis Lee Bieber, Dec 1, 2012
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Erica

    Multiple bcc recipients

    Erica, Oct 6, 2003, in forum: Perl
    Replies:
    1
    Views:
    758
    Gunnar Hjalmarsson
    Oct 6, 2003
  2. Replies:
    0
    Views:
    5,110
  3. tobhamlet

    Finding BCC receipients

    tobhamlet, Nov 8, 2005, in forum: Java
    Replies:
    3
    Views:
    10,098
    Roedy Green
    Nov 9, 2005
  4. gavin

    mailto: - BCC?

    gavin, Feb 11, 2004, in forum: HTML
    Replies:
    10
    Views:
    9,908
    davethomas
    Apr 3, 2012
  5. Rolf Hemmerling
    Replies:
    3
    Views:
    746
    Roger Leigh
    Oct 31, 2003
Loading...

Share This Page