您正在费力地在中构造有效的MIME消息
msg,然后放弃它并发送一个简单的字符串
email_message。
您可能应该先了解正确的MIME结构是什么样子。多部分消息本身根本没有内容,如果需要文本部分,则必须添加文本部分。
以下是对脚本的编辑,其中添加了缺少的部分。我没有尝试发送结果消息。
from email.mime.multipart import MIMEMultipartfrom email.mime.text import MIMEText # Addedfrom email.mime.image import MIMEImageattachment = 'bob.jpg'msg = MIMEMultipart()msg["To"] = tomsg["From"] = frommsg["Subject"] = subjectmsgText = MIMEText('<b>%s</b><br><img src="cid:%s"><br>' % (body, attachment), 'html') msg.attach(msgText) # Added, and edited the previous linefp = open(attachment, 'rb') img = MIMEImage(fp.read())fp.close()img.add_header('Content-ID', '<{}>'.format(attachment))msg.attach(img)print msg.as_string()exit(0)


