栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Django创建电子邮件模板

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Django创建电子邮件模板

从docs,要发送HTML电子邮件,你想使用其他内容类型,如下所示:

from django.core.mail import EmailMultiAlternativessubject, from_email, to = 'hello', 'from@example.com', 'to@example.com'text_content = 'This is an important message.'html_content = '<p>This is an <strong>important</strong> message.</p>'msg = EmailMultiAlternatives(subject, text_content, from_email, [to])msg.attach_alternative(html_content, "text/html")msg.send()

你可能需要两个用于电子邮件的模板-一个看起来像这样的纯文本模板,存储在你的模板目录下

email.txt

Hello {{ username }} - your account is activated.

还有一个HTMLy,存放在以下位置

email.html

Hello <strong>{{ username }}</strong> - your account is activated.

然后,你可以使用来使用这两个模板发送电子邮件get_template,如下所示:

from django.core.mail import EmailMultiAlternativesfrom django.template.loader import get_templatefrom django.template import Contextplaintext = get_template('email.txt')htmly     = get_template('email.html')d = Context({ 'username': username })subject, from_email, to = 'hello', 'from@example.com', 'to@example.com'text_content = plaintext.render(d)html_content = htmly.render(d)msg = EmailMultiAlternatives(subject, text_content, from_email, [to])msg.attach_alternative(html_content, "text/html")msg.send()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/407740.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号