Python 在电子邮件中嵌入图片

Python 在电子邮件中嵌入图片,python,email,python-3.x,Python,Email,Python 3.x,我目前有一个程序,可以从列表中随机选择报价并通过电子邮件发送给他们。我现在也在尝试在电子邮件中嵌入一个图像。我遇到了一个问题,我可以附加电子邮件,但我的报价不再工作。我在网上进行了研究,但这些解决方案对我不起作用。请注意,我使用的是Python 3.2.2 任何指导都将不胜感激 import smtplib from email.mime.multipart import MIMEMultipart from email.mime.image import MIMEImage attachme

我目前有一个程序,可以从列表中随机选择报价并通过电子邮件发送给他们。我现在也在尝试在电子邮件中嵌入一个图像。我遇到了一个问题,我可以附加电子邮件,但我的报价不再工作。我在网上进行了研究,但这些解决方案对我不起作用。请注意,我使用的是Python 3.2.2

任何指导都将不胜感激

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage

attachment = 'bob.jpg' 

msg = MIMEMultipart()
msg["To"] = to_addr
msg["From"] = from_addr
msg["Subject"] = subject_header

#msgText = MIMEText(<b>%s</b><br><img src="cid:bob.jpg"><br>, 'html') % body

fp = open(attachment, 'rb')
img = MIMEImage(fp.read())
fp.close()

msg.attach(img)

#email_message = '%s\n%s\n%s' % (subject_header, body, img)
email_message = '%s\n%s' % (subject_header, body)

emailRezi = smtplib.SMTP(mail_server, mail_server_port)
emailRezi.set_debuglevel(1)
emailRezi.login(mail_username, mail_password)
emailRezi.sendmail(from_addr, to_addr, email_message)
#emailRezi.sendmail(from_addr, to_addr, msg.as_string())
emailRezi.quit()

从上面的代码中可以看出,我尝试了不同的方法来引用

,您正在经历巨大的痛苦来构建一个有效的msg MIME消息,然后放弃它并发送一个简单的字符串email\u消息

您可能应该从理解正确的MIME结构开始。多部分消息本身没有任何内容,如果需要文本部分,则必须添加文本部分

下面是对脚本的编辑,添加了缺少的部分。我没有尝试发送结果消息

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText  # Added
from email.mime.image import MIMEImage

attachment = 'bob.jpg'

msg = MIMEMultipart()
msg["To"] = to
msg["From"] = from
msg["Subject"] = subject

msgText = MIMEText('<b>%s</b><br><img src="cid:%s"><br>' % (body, attachment), 'html')  
msg.attach(msgText)   # Added, and edited the previous line

fp = 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)

我已经编辑了将图像附加到邮件正文和HTML模板上

import smtplib
from email import encoders
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage

strFrom = 'zzzzzz@gmail.com'
strTo = 'xxxxx@gmail.com'

# Create the root message 

msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = 'test message'
msgRoot['From'] = strFrom
msgRoot['To'] = strTo
msgRoot['Cc'] =cc
msgRoot.preamble = 'Multi-part message in MIME format.'

msgAlternative = MIMEMultipart('alternative')
msgRoot.attach(msgAlternative)

msgText = MIMEText('Alternative plain text message.')
msgAlternative.attach(msgText)

msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><br>KPI-DATA!', 'html')
msgAlternative.attach(msgText)

#Attach Image 
fp = open('test.png', 'rb') #Read image 
msgImage = MIMEImage(fp.read())
fp.close()

# Define the image's ID as referenced above
msgImage.add_header('Content-ID', '<image1>')
msgRoot.attach(msgImage)

import smtplib
smtp = smtplib.SMTP()
smtp.connect('smtp.gmail.com') #SMTp Server Details
smtp.login('exampleuser', 'examplepass') #Username and Password of Account
smtp.sendmail(strFrom, strTo, msgRoot.as_string())
smtp.quit()

由于现在电子邮件几乎总是以HTML格式显示,包括图像并不是问题,但最终用户是否会看到它是另一回事,因为除非另有规定,否则它们几乎总是被阻止。请发布您正在使用的代码。可能重复的代码。谢谢你的帮助。电子邮件发送时,邮件正文中的图片为红色X框。我应该添加我的修复程序。除了从tripleee添加的内容外,我还将img放在imgur上并引用了url。如果您真的想附加图像,正确的修复方法是将正确的文件名添加到附件的元数据中。有一个更完整的例子。谢谢你的链接。我认为从URL引用图像的好处在于我们决定从哪个系统运行这个程序的灵活性。再次感谢您的帮助。需要是:img.add_header'Content-ID',formatattachmentHey,这很有效!但是,您使用的是compat32 API。我不知道如何使用更现代的EmailMessage界面来做同样的事情,你知道怎么做吗?这很有魅力。多谢各位@Sachin@musbur,到现在为止,我还没有探索过同样的方法。