我想使用以下代码(Python 3.1)发送带有附件的电子邮件(大大简化以示例为例)。
附件对象msg1返回'email.mime.text.MIMEText'对象,但是当msg1.add_header(...)行运行时,结果为None,因此程序在msg.as_string()中崩溃,因为附件的任何部分都不能有None值。(Traceback显示在generator.py的第118行的_dispatch中,从msg.as_string()许多级别之下,“'NoneType'对象没有属性'get_content_maintype'”)
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()
附件对象msg1返回'email.mime.text.MIMEText'对象,但是当msg1.add_header(...)行运行时,结果为None,因此程序在msg.as_string()中崩溃,因为附件的任何部分都不能有None值。(Traceback显示在generator.py的第118行的_dispatch中,从msg.as_string()许多级别之下,“'NoneType'对象没有属性'get_content_maintype'”)
有人知道问题的原因吗?任何帮助将不胜感激。
Alan Harris-Reid