发送包含嵌入图像的多部分html电子邮件 [英] Sending Multipart html emails which contain embedded images

查看:220
本文介绍了发送包含嵌入图像的多部分html电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在玩python中的电子邮件模块,但我想知道如何嵌入html中包含的图像。



所以例如,如果身体像

 < img src =../ path / image.png>< ; / IMG> 

我想将 image.png 嵌入到电子邮件中, src 属性应该替换为 content-id 。有没有人知道如何做?

解决方案

这是一个我发现的例子。


配方473810:发送带有嵌入式的HTML电子邮件图像和纯文本替代



HTML是为那些
希望使用富文本发送电子邮件的方法,
布局和图形。通常情况下,
可以将图形嵌入到
中,因此收件人可以直接显示
的消息,而无需进一步
下载。



某些邮件代理不支持HTML或
,他们的用户喜欢收到纯文本
的短信。 HTML
消息的发件人应该包含一个纯文本
消息作为这些
用户的替代。



此配方发送一个简短的HTML消息
,单个嵌入图像和
替代纯文本消息。




 #发送一个HTML电子邮件,其中包含不想显示HTML的
#email客户端的嵌入图像和纯文本消息。来自email.MIMEMultipart导入MIMEMultipart


来自email.MIMEText import MIMEText
来自email.MIMEImage import MIMEImage

#定义这些一次;使用它们两次!
strFrom ='from@example.com'
strTo ='to@example.com'

#创建根消息并填写from,to和subject标题
msgRoot = MIMEMultipart('related')
msgRoot ['Subject'] ='测试消息'
msgRoot ['From'] = strFrom
msgRoot ['To'] = strTo
msgRoot.preamble ='这是MIME格式的多部分消息'

#封装消息正文的纯文本和HTML版本
#'替代'部分,所以消息代理可以决定要显示哪些。
msg替代= MIMEMultipart('alternative')
msgRoot.attach(msg替代)

msgText = MIMEText('这是替代纯文本消息')
msg替代.attach(msgText)

#我们将IMG SRC属性中的图像通过我们给它的ID
msgText = MIMEText('< b> Some< i> / i> text< / b>和图像< br>< img src =cid:image1>< br> Nifty!','html')
msgAlternative.attach(msgText)

#此示例假设图像在当前目录中
fp = open('test.jpg','rb')
msgImage = MIMEImage(fp.read() )
fp.close()

#定义上面引用的图像ID
msgImage.add_header('Content-ID','< image1>')
msgRoot.attach(msgImage)

#发送电子邮件(此示例假定需要SMTP验证)
import smtplib
smtp = smtplib.SMTP()
smtp .connect('smtp.example.com')
smtp.logi n('exampleuser','examplepass')
smtp.sendmail(strFrom,strTo,msgRoot.as_string())
smtp.quit()
pre>

I've been playing around with the email module in python but I want to be able to know how to embed images which are included in the html.

So for example if the body is something like

<img src="../path/image.png"></img>

I would like to embed image.png into the email, and the src attribute should be replaced with content-id. Does anybody know how to do this?

解决方案

Here is an example I found.

Recipe 473810: Send an HTML email with embedded image and plain text alternate:

HTML is the method of choice for those wishing to send emails with rich text, layout and graphics. Often it is desirable to embed the graphics within the message so recipients can display the message directly, without further downloads.

Some mail agents don't support HTML or their users prefer to receive plain text messages. Senders of HTML messages should include a plain text message as an alternate for these users.

This recipe sends a short HTML message with a single embedded image and an alternate plain text message.

# Send an HTML email with an embedded image and a plain text message for
# email clients that don't want to display the HTML.

from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage

# Define these once; use them twice!
strFrom = 'from@example.com'
strTo = 'to@example.com'

# Create the root message and fill in the from, to, and subject headers
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = 'test message'
msgRoot['From'] = strFrom
msgRoot['To'] = strTo
msgRoot.preamble = 'This is a multi-part message in MIME format.'

# Encapsulate the plain and HTML versions of the message body in an
# 'alternative' part, so message agents can decide which they want to display.
msgAlternative = MIMEMultipart('alternative')
msgRoot.attach(msgAlternative)

msgText = MIMEText('This is the alternative plain text message.')
msgAlternative.attach(msgText)

# We reference the image in the IMG SRC attribute by the ID we give it below
msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><br>Nifty!', 'html')
msgAlternative.attach(msgText)

# This example assumes the image is in the current directory
fp = open('test.jpg', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()

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

# Send the email (this example assumes SMTP authentication is required)
import smtplib
smtp = smtplib.SMTP()
smtp.connect('smtp.example.com')
smtp.login('exampleuser', 'examplepass')
smtp.sendmail(strFrom, strTo, msgRoot.as_string())
smtp.quit()

这篇关于发送包含嵌入图像的多部分html电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆