创建一个使用python / django发送的图像的MIME电子邮件模板 [英] creating a MIME email template with images to send with python / django

查看:216
本文介绍了创建一个使用python / django发送的图像的MIME电子邮件模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网络应用程序中,我偶尔会使用这样一个可重复使用的邮件应用程序发送电子邮件:

  user  -  self.user 
subject =(我的主题)
from =me@mydomain.com
message = render_to_string(welcomeEmail / welcome.eml,{
user:user,
})
send_mail(subject,message,from,[email],priority =high)

我想发送带有嵌入图像的电子邮件,所以我尝试在邮件客户端中查看邮件,查看源代码,并将其放入我的模板(welcome.eml),但我已经有没有人能够在邮件客户端发送文件时正确呈现。



有没有人知道一个简单的方法来创建带有内联图像的MIME编码邮件模板,

解决方案

更新



非常感谢 Saqib Ali 在我回复近5年之后复活这个老问题。



我当时给出的说明不再工作。我怀疑Django在这段时间里有所改进,这意味着 send_mail()强制执行纯文本。无论您在内容中放置什么,它都将以纯文本格式递送。



最近的 Django文档解释说, send_mail()真的只是一个方便用于创建 django.core.mail.EmailMessage 类的实例,然后在该实例上调用 send() EmailMessage 有关于body参数的注释,这说明了我们在2014年看到的结果:


body:正文。这应该是一个纯文本消息。



...稍后文档...



由默认情况下,EmailMessage中的body参数的MIME类型为text / plain。这是一个很好的做法,一个人离开。


公平(我承认我没有花时间调查为什么2009年的指示工作 - 我在2009年做了测试 - 或者改变了)。 Django确实提供了文档,一个 django.core.mail.EmailMultiAlternatives 类,以便更轻松地发送相同消息的纯文本和HTML表示。



这个问题的情况略有不同。我们不是追求一种替代方法,而是将相关的部件添加到其中一种替代方案。在HTML版本中(如果您有或忽略了纯文本版本无关紧要),我们要嵌入图像数据部分。不是HTML体内引用的内容的替代视图,而是相关的内容。



仍然可以发送嵌入的图像,但是我没有看到直接使用 send_mail 的方法。现在是时候省略方便的功能,并直接实例化一个 EmailMessage



以上是上一个例子的更新:

  from django。 core.mail import EmailMessage 
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

#加载要发送的图像为字节
img_data = open('logo.jpg','rb')。read()

#创建一个相关消息容器, HTML
#消息和图像。这些是相关的(而不是替代)
#,因为它们是不同的,HTML消息的独特部分,
#不可替代(html对纯文本)的相同内容的视图。
html_part = MIMEMultipart(_subtype ='related')

#使用HTML创建正文。注意,由于它是内联的,所以图像是
#,引用URL cid:myimage ...你应该注意使
#myimage唯一
body = MIMEText(' < p> Hello< img src =cid:myimage/>< / p>',_subtype ='html')
html_part.attach(body)

#现在为图像创建MIME容器
img = MIMEImage(img_data,'jpeg')
img.add_header('Content-Id','< myimage>')#尖括号很重要
img.add_header(Content-Disposition,inline,filename =myimage)#David Hess推荐此编辑
html_part.attach(img)

#配置和发送一个EmailMessage
#注意,我们传递了无身体(第二个参数)。您可以传递纯文本
#为此消息创建一个替代部分
msg = EmailMessage('Subject Line',None,'foo@bar.com',['bar@foo.com'] )
msg.attach(html_part)#附加原始MIMEBase后代。这是EmailMessage的一种公共方法
msg.send()






2009年的原始回复:



要发送带有嵌入图像的电子邮件,请使用python的内置电子邮件模块要建立MIME部分。



以下应该做到:

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

#加载您要发送的图像在字节
img_data = open('logo.jpg','rb')。read()

#创建一个相关消息容器,将容纳HTML
#消息和图像
msg = MIMEMultipart(_subtype ='related')

#使用HTML创建主体。注意,由于它是内联的,所以图像是
#,引用URL cid:myimage ...你应该注意使
#myimage唯一
body = MIMEText(' < p> Hello< img src =cid:myimage/>< / p>',_subtype ='html')
msg.attach(body)

#现在为图像创建MIME容器
img = MIMEImage(img_data,'jpeg')
img.add_header('Content-Id','< myimage>')#尖括号很重要
msg.attach(img)

send_mail(subject,msg.as_string(),from,[to],priority =high)
/ pre>

在现实中,你可能想要发送HTML和一个纯文本的替代方法。在这种情况下,请使用MIMEMultipart创建相关mimetype容器作为根。然后创建另一个具有子类型alternative的MIMEMultipart,并将MIMEText(子类型html)和MIMEText(子类型plain)附加到替代部分。然后将图像附加到相关根。


In my web application I send emails occasionally using a reusable mailer application like this:

user - self.user
subject = ("My subject")
from = "me@mydomain.com"
message = render_to_string("welcomeEmail/welcome.eml", { 
                "user" : user,
                })
send_mail(subject, message, from, [email], priority="high" )

I want to send an email with embedded images in it, so I tried making the mail in a mail client, viewing the source, and putting it into my template (welcome.eml), but I've been unable to get it to render correctly in mail clients when its sent.

Does anyone know of an easy way for me to create mime encoded mail templates with inline images that will render correctly when I send them?

解决方案

Update

Many thanks to Saqib Ali for resurrecting this old question nearly 5 years after my reply.

The instructions I gave at the time no longer work. I suspect there have been some improvements to Django in the intervening years which mean that send_mail() enforces plain text. No matter what you put in the content, it will always be delivered as plain text.

The most recent Django documentation explains that send_mail() is really just a convenience for creating an instance of the django.core.mail.EmailMessage class, and then calling send() on that instance. EmailMessage has this note for the body parameter, which explains the results we're seeing now in 2014:

body: The body text. This should be a plain text message.

... somewhat later in the docs ...

By default, the MIME type of the body parameter in an EmailMessage is "text/plain". It is good practice to leave this alone.

Fair enough (I confess I haven't taken the time to investigate why the 2009 instructions worked - I did test them back in 2009 - or when it changed). Django does provide, and document, a django.core.mail.EmailMultiAlternatives class to make it easier for sending a plain text and HTML representation of the same message.

The case in this question is slightly different. We're not seeking to append an alternative per se, but to append related parts to one of the alternatives. Within the HTML version (and it doesn't matter if you have or omit the plain text version), we want to embed an image data part. Not an alternative view of the content, but related content that is referenced in the HTML body.

Sending an embedded image is still possible, but I don't see a straightforward way to do it using send_mail. It's time to dispense with the convenience function and to instantiate an EmailMessage directly.

Here's an update to the previous example:

from django.core.mail import EmailMessage
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# Load the image you want to send as bytes
img_data = open('logo.jpg', 'rb').read()

# Create a "related" message container that will hold the HTML 
# message and the image. These are "related" (not "alternative")
# because they are different, unique parts of the HTML message,
# not alternative (html vs. plain text) views of the same content.
html_part = MIMEMultipart(_subtype='related')

# Create the body with HTML. Note that the image, since it is inline, is 
# referenced with the URL cid:myimage... you should take care to make
# "myimage" unique
body = MIMEText('<p>Hello <img src="cid:myimage" /></p>', _subtype='html')
html_part.attach(body)

# Now create the MIME container for the image
img = MIMEImage(img_data, 'jpeg')
img.add_header('Content-Id', '<myimage>')  # angle brackets are important
img.add_header("Content-Disposition", "inline", filename="myimage") # David Hess recommended this edit
html_part.attach(img)

# Configure and send an EmailMessage
# Note we are passing None for the body (the 2nd parameter). You could pass plain text
# to create an alternative part for this message
msg = EmailMessage('Subject Line', None, 'foo@bar.com', ['bar@foo.com'])
msg.attach(html_part) # Attach the raw MIMEBase descendant. This is a public method on EmailMessage
msg.send()


Original reply from 2009:

To send an e-mail with embedded images, use python's built-in email module to build up the MIME parts.

The following should do it:

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

# Load the image you want to send at bytes
img_data = open('logo.jpg', 'rb').read()

# Create a "related" message container that will hold the HTML 
# message and the image
msg = MIMEMultipart(_subtype='related')

# Create the body with HTML. Note that the image, since it is inline, is 
# referenced with the URL cid:myimage... you should take care to make
# "myimage" unique
body = MIMEText('<p>Hello <img src="cid:myimage" /></p>', _subtype='html')
msg.attach(body)

# Now create the MIME container for the image
img = MIMEImage(img_data, 'jpeg')
img.add_header('Content-Id', '<myimage>')  # angle brackets are important
msg.attach(img)

send_mail(subject, msg.as_string(), from, [to], priority="high")

In reality, you'll probably want to send the HTML along with a plain-text alternative. In that case, use MIMEMultipart to create the "related" mimetype container as the root. Then create another MIMEMultipart with the subtype "alternative", and attach both a MIMEText (subtype html) and a MIMEText (subtype plain) to the alternative part. Then attach the image to the related root.

这篇关于创建一个使用python / django发送的图像的MIME电子邮件模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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