Tri*_*ton 13 python email django mime
在我的Web应用程序中,我偶尔使用可重用的邮件程序应用程序发送电子邮件,如下所示:
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" )
Run Code Online (Sandbox Code Playgroud)
我想发送一封包含嵌入图像的电子邮件,所以我尝试在邮件客户端发送邮件,查看源代码,并将其放入我的模板(welcome.eml),但我一直无法将其渲染到正确地在邮件客户端发送时.
有没有人知道我有一个简单的方法来创建具有内联图像的mime编码邮件模板,当我发送它们时它们会正确呈现?
Jar*_*die 28
更新
非常感谢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
# 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()
Run Code Online (Sandbox Code Playgroud)
2009年的原始答复:
要发送带有嵌入式图像的电子邮件,请使用python的内置电子邮件模块来构建MIME部分.
以下应该这样做:
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")
Run Code Online (Sandbox Code Playgroud)
实际上,您可能希望将HTML与纯文本替代一起发送.在这种情况下,使用MIMEMultipart创建"相关"mimetype容器作为根.然后使用子类型"alternative"创建另一个MIMEMultipart,并将MIMEText(子类型html)和MIMEText(子类型plain)附加到替代部件.然后将图像附加到相关的根.
| 归档时间: |
|
| 查看次数: |
15102 次 |
| 最近记录: |