Email attachment problem

Alan Harris-Reid aharrisreid at
Thu Apr 29 22:06:59 CEST 2010

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

    fp = open(att_file)
    att_msg = MIMEText(
    attachment = att_msg.add_header('Content-Disposition', '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, 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

More information about the Python-list mailing list