我正在尝试发送带有嵌入式gif图像的多部分/相关html电子邮件.此电子邮件是使用Oracle PL/SQL生成的.我的尝试失败了,图像显示为红色X(在Outlook 2007和雅虎邮件中)
我已经发送了一段时间的HTML电子邮件,但我现在要求在电子邮件中使用几个gif图像.我可以将它们存储在我们的一个Web服务器上并链接到它们,但许多用户的电子邮件客户端不会自动显示它们,并且需要更改设置或手动为每个电子邮件下载它们.
所以,我的想法是嵌入图像.我的问题是:
片段:
MIME-Version: 1.0
To: me@gmail.com
BCC: me@yahoo.com
From: email@yahoo.com
Subject: Test
Reply-To: email@yahoo.com
Content-Type: multipart/related; boundary="a1b2c3d4e3f2g1"
--a1b2c3d4e3f2g1
content-type: text/html;
<html>
<head><title>My title</title></head>
<body>
<div style="font-size:11pt;font-family:Calibri;">
<p><IMG SRC="cid:my_logo" alt="Logo"></p>
... more html here ...
</div></body></html>
--a1b2c3d4e3f2g1
Content-Type: image/gif;
Content-ID:<my_logo>
Content-Transfer-Encoding: base64
Content-Disposition: inline
[base64 image data here]
--a1b2c3d4e3f2g1--
Run Code Online (Sandbox Code Playgroud)
非常感谢.
顺便说一句:是的,我已经验证了base64数据是正确的,因为我可以将图像嵌入到html本身(使用相同的算法用于创建标题数据)并在Firefox/IE中查看图像.
我还应该注意,这不是针对垃圾邮件,而是将电子邮件发送给每天都在期待的特定客户.内容是数据驱动的,而不是广告.
Mail multipart/alternative vs multipart/mixed的答案表明附件应该是multipart/alternative
消息的对等,如:
我想发送带有一些内嵌图像和纯文本替代品的html部分的电子邮件.各个部分的首选MIME布局是什么?在示例代码和其他问题中出现了几个选项,但哪些选项在实践中效果最好?我倾向于这样:
这样,图像显然是为了渲染html部分.一个完整的例子是:
From: Rich Example <rich-example@example.org>
To: A Recipient <recipient@example.org>
Subject: An example of email with images and a plain alternative
MIME-Version: 1.0
Content-Type: multipart/alternative; boundary="outer-boundary"
This is a MIME-encoded message. If you are seeing this, your mail
reader is old.
--outer-boundary
Content-Type: text/plain; charset=us-ascii
This message might make you :) or it might make you :( …
Run Code Online (Sandbox Code Playgroud) 使用内联附件和非内联附件创建HTML电子邮件的正确方法是什么?
另外,请告诉我仅使用内联附件和仅使用非内联附件的内容类型.
到现在为止我这样做了:
MIME-Version: 1.0
[some more headers]
Content-type: multipart/mixed;
boundary="myboundary"
--myboundary
Content-Type: text/html; charset=ISO-8859-15
Content-Transfer-Encoding: 7bit
[html with img cid:my_image]
--myboundary
Content-Type: image/png; name="my_image.png"
Content-Transfer-Encoding: base64
Content-ID: <my_image>
Content-Disposition: inline; filename="my_image.png"
[base64 image data]
--myboundary
Content-type: application/pdf; name="my_pdf.pdf"
Content-length: 1150
Content-Transfer-Encoding: base64
Content-ID: <my_pdf.pdf>
Content-Disposition: attachment; filename="my_pdf.pdf"
[base64 pdf data]
--myboundary--
Run Code Online (Sandbox Code Playgroud)
邮件看起来很好看.但是我注意到Thunderbird没有显示我的内嵌图像,而是显示了2个附件(我的图像和我的PDF).所以我做了一些调试,发现内联图像应该通过发送Content-Type: multipart/related
.
所以我改变了Content-Type: multipart/mixed
对Content-Type: multipart/related
和Thunderbird显示它正确的:图像是HTML和一个附件所示,PDF所示.
我不确定这是否是正确的解决方案,虽然它似乎有效.是否正确使用multipart/related
(如果我有内联和非内联附件,如果我只有内联附件,如果我只有非内联附件)?
或者是使用一个类型边界相关的拆分内联附件和另一个类型混合边界以拆分非内联附件的正确方法?
我希望你能为我提供样品
嗨我正在使用以下2个功能来动态创建和发送PDF文件.我究竟做错了什么.(导出pdf函数在浏览器中显示时可正常工作.)
我得到的错误是"TypeError:'ContentFile'对象不支持索引".我做错了什么?几乎所有这些代码都来自一些博客,所以我不知道它是如何工作的.但如果你不理解某些事情,请评论.我会回复.
if request.method=="POST":
form = ReportSendMailForm(request.POST)
if form.is_valid():
email = form.cleaned_data['mail']
message = form.cleaned_data['message']
print email
print message
result = export_pdf(request,id)
file_to_be_sent= ContentFile(result)
#print file_to_be_sent
if result:
#try :
subject, from_email, to = request.user.username+' has sent a report for review',' Dtz Team', email
html_content = render_to_string('email/report.html',{'username':request.user,'messgae':message})
msg = EmailMultiAlternatives(subject,'', from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.attach("Report.pdf", file_to_be_sent, "application/pdf")
msg.send()
messages.success(request,'Please check your email id '+email+' for further instructions.')
return HttpResponse('success')
#except:
# pass
messages.error(request,'Error occured in the pdf.')
return …
Run Code Online (Sandbox Code Playgroud)