Email Attachment Problem

I want to send an email with an attachment using the following code (Python 3.1) (greatly simplified to show an 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 binding object msg1 returns the object "email.mime.text.MIMEText" in ', but when the string msg1.add_header (...) runs the result "No", then the program crashes into msg.as_string (), because no part of the attachment may not matter None. (Traceback shows that the NoneType object does not have the get_content_maintype attribute on line 118 _dispatch in generator.py, at many levels from msg.as_string ())

Does anyone know what is causing the problem? Any help would be appreciated.

Alan Harris Reed

+3
1

:

msg.attach(msg1)
+3

Source: https://habr.com/ru/post/1743413/


All Articles