Email attachment problem

Posted by Alan Harris-Reid on Stack Overflow See other posts from Stack Overflow or by Alan Harris-Reid
Published on 2010-04-29T20:35:36Z Indexed on 2010/04/29 20:37 UTC
Read the original article Hit count: 382

Filed under:
|
|

Hi there,

I want to send an email with an attachment using the following code (Python 3.1) (greatly simplified to show the 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)
msg1 = MIMEText(fp.read())
attachment = msg1.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 ', but when the msg1.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

© Stack Overflow or respective owner

Related posts about python

Related posts about email