使用Django创建电子邮件模板

我想使用Django模板发送HTML电子邮件,如下所示:

<html>
<车身>
你好<strong>{{username}}&lt/strong>
您的帐户已激活。
<img src=”https://stackoverflow.com/questions/2809547/mysite.com/logo.gif“/>
&lt/车身>

我找不到关于send\u mail的任何信息,django mailer只发送HTML模板,没有动态数据

如何使用Django的模板引擎生成电子邮件

在文档中,要发送HTML电子邮件,您需要使用其他内容类型,如:

django.core.mail导入电子邮件的


主题,从电子邮件发送至='你好','[email protected]', '[email protected]'
text_content='这是一条重要的消息。'
html_content='<p>这是一个<strong>重要&lt/strong>信息。&lt/p>'
msg=emailmultialternations(主题、文本内容、发件人/电子邮件[收件人])
msg.附加可选内容(html内容,“文本/html”)
msg.send()

您可能需要两个电子邮件模板—一个类似于此的纯文本模板,存储在您的templates目录下的email.txt

你好{{username}}-您的帐户已激活。

还有一个HTMLy,存储在email.html下:

你好<strong>{{username}}&lt/strong>-您的帐户已激活。

然后,您可以通过使用get\u template使用这两个模板发送电子邮件,如下所示:

django.core.mail导入电子邮件的


从django.template.loader导入get_模板
从django.template导入上下文
明文=获取模板('email.txt')
htmly=get_模板('email.html')
d=上下文({'username':username})
主题,从电子邮件发送至='你好','[email protected]', '[email protected]'
text\u content=纯文本。呈现(d)
html\u content=htmly.render(d)
msg=emailmultialternations(主题、文本内容、发件人/电子邮件[收件人])
msg.附加可选内容(html内容,“文本/html”)
msg.send()

发表评论