邮件附件问题

时间:2010-04-29 20:35:37

标签: python email attachment

我想使用以下代码发送带附件的电子邮件(Python 3.1) (大大简化以显示示例)

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的_dispatch的第118行显示“'NoneType'对象没有属性'get_content_maintype'”,从msg.as_string()开始有很多级别

有谁知道问题的原因可能是什么?任何帮助将不胜感激。

艾伦哈里斯 - 里德

1 个答案:

答案 0 :(得分:3)

使用:

msg.attach(msg1)