Email attachment problem

Discussion in 'Python' started by Alan Harris-Reid, Apr 29, 2010.

  1. Hi there,

    I want to send an email with an attachment using the following code
    (running under Python 3.1, greatly simplified to show example)

    from email.mime.multipart import MIMEMultipart
    from email.mime.text import MIMEText

    msg = MIMEMultipart()
    msg['From'] = from_addr
    msg['To'] = to_addr
    msg['Subject'] = subject
    msg.attach(MIMEText(body))

    fp = open(att_file)
    att_msg = MIMEText(fp.read())
    attachment = att_msg.add_header('Content-Disposition', 'attachment',
    filename=att_file)
    msg.attach(attachment)

    # set string to be sent as 3rd parameter to smptlib.SMTP.sendmail()
    send_string = msg.as_string()


    The attachment object msg1 returns 'email.mime.text.MIMEText' object at
    <address>', but when the att_msg.add_header(...) line runs the result is
    None, hence the program falls-over in msg.as_string() because no part of
    the attachment can have a None value. (Traceback shows "'NoneType'
    object has no attribute 'get_content_maintype'" in line 118 of _dispatch
    in generator.py, many levels down from msg.as_string())

    Has anyone any idea what the cause of the problem might be? Any help
    would be appreciated.

    Alan Harris-Reid
     
    Alan Harris-Reid, Apr 29, 2010
    #1
    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. Alan Harris-Reid

    Binary file email attachment problem

    Alan Harris-Reid, May 10, 2010, in forum: Python
    Replies:
    0
    Views:
    397
    Alan Harris-Reid
    May 10, 2010
  2. Shawn

    attachment email script problem

    Shawn, Feb 6, 2004, in forum: Perl Misc
    Replies:
    0
    Views:
    117
    Shawn
    Feb 6, 2004
  3. ecureuil
    Replies:
    0
    Views:
    329
    ecureuil
    May 28, 2006
  4. ecureuil
    Replies:
    0
    Views:
    323
    ecureuil
    Jun 11, 2006
  5. M.O.B. i L.

    Problem with email attachment, 1 becomes 10

    M.O.B. i L., May 5, 2008, in forum: Perl Misc
    Replies:
    3
    Views:
    139
    M.O.B. i L.
    May 8, 2008
Loading...

Share This Page