sim*_*sim 5 python email django
我正在尝试使用HTML和embadded图像发送电子邮件.电子邮件发送工作正常,它还将图像附加到电子邮件中,但它没有以HTML格式显示图像(内联).我在视图中使用了Content:ID,在模板中使用了cid但没有成功:(.我探索了很多表单并应用了解决方案,但是徒劳无助请帮忙!这是我的代码:
html_content = render_to_string('newsletters/newsletter_template.html', vars)
text_content = strip_tags(html_content)
to = 'myemail@gmail.com'
msg = EmailMultiAlternatives(self.subject, text_content, settings.STAFF_FROM_EMAIL, [to])
msg.attach_alternative(html_content, "text/html")
image_file = open('../media/images/banner_admin.gif', 'rb')
msg_image = MIMEImage(image_file.read())
image_file.close()
msg_image.add_header('Content-ID', '<image1>')
msg.attach(msg_image)
msg.send()
Run Code Online (Sandbox Code Playgroud)
<div style="border:2px solid #CCCCCC; width:900px; font-size:10px; padding:5px;">
<div style="margin-bottom: 10px;"><img src="cid:image1" /></div>
<div style="">{{body|linebreaks}}</div>
</div>
Run Code Online (Sandbox Code Playgroud)
如果我在这里遗漏了什么,请告诉我...
小智 0
要将 PNG 文件嵌入电子邮件正文,请尝试以下代码:
for f in ['img1.png', 'img2.png']:
fp = open(os.path.join(os.path.dirname(__file__), f), 'rb')
msg_img = MIMEImage(fp.read())
fp.close()
msg_img.add_header('Content-ID', '<{}>'.format(f))
msg.attach(msg_img)
msg.send()
Run Code Online (Sandbox Code Playgroud)
<img src="cid:img1.png">现在您可以在模板中使用。结果应该是电子邮件客户端显示嵌入在邮件中标签位置的图像<img>,而不是作为附件。
| 归档时间: |
|
| 查看次数: |
1754 次 |
| 最近记录: |