views:

74

answers:

1

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

+1  A: 

Use:

msg.attach(msg1)
mg
Thanks mg - works fine now. Many thanks - Alan
Alan Harris-Reid
@Alan Harris-Reid: If my answer helped you, while it is trivial, you can accept it.
mg
How do I accept it? Do you mean 'vote-up'? If so, I cannot at present because vote-up requires 15 reputation, which I don't have yet. (Excuse my ignorance, but I'm new to all this.). Regards.
Alan Harris-Reid
Found it! Discovered the tick below the vote-count. Regards.
Alan Harris-Reid
Got the reputation qualification now, so you can have a vote as well!
Alan Harris-Reid