我想发送html电子邮件,使用Django模板,像这样:
<html>
<body>
hello <strong>{{username}}</strong>
your account activated.
<img src="mysite.com/logo.gif" />
</body>
我找不到任何关于send_mail的东西,django-mailer只发送HTML模板,没有动态数据。
如何使用Django的模板引擎生成电子邮件?
我想发送html电子邮件,使用Django模板,像这样:
<html>
<body>
hello <strong>{{username}}</strong>
your account activated.
<img src="mysite.com/logo.gif" />
</body>
我找不到任何关于send_mail的东西,django-mailer只发送HTML模板,没有动态数据。
如何使用Django的模板引擎生成电子邮件?
当前回答
Django Mail Templated是一个功能丰富的Django应用程序,可以用Django模板系统发送电子邮件。
安装:
pip install django-mail-templated
配置:
INSTALLED_APPS = (
...
'mail_templated'
)
模板:
{% block subject %}
Hello {{ user.name }}
{% endblock %}
{% block body %}
{{ user.name }}, this is the plain text part.
{% endblock %}
Python:
from mail_templated import send_mail
send_mail('email/hello.tpl', {'user': user}, from_email, [user.email])
更多信息:https://github.com/artemrizhov/django-mail-templated
其他回答
使用EmailMultiAlternatives和render_to_string来使用两个可供选择的模板(一个是纯文本,一个是html):
from django.core.mail import EmailMultiAlternatives
from django.template import Context
from django.template.loader import render_to_string
c = Context({'username': username})
text_content = render_to_string('mail/email.txt', c)
html_content = render_to_string('mail/email.html', c)
email = EmailMultiAlternatives('Subject', text_content)
email.attach_alternative(html_content, "text/html")
email.to = ['to@example.com']
email.send()
send_emai()不适合我,所以我在django docs中使用EmailMessage。
我有两个版本的答案:
只提供html电子邮件版本 与纯文本电子邮件和html电子邮件版本
from django.template.loader import render_to_string
from django.core.mail import EmailMessage
# import file with html content
html_version = 'path/to/html_version.html'
html_message = render_to_string(html_version, { 'context': context, })
message = EmailMessage(subject, html_message, from_email, [to_email])
message.content_subtype = 'html' # this is required because there is no plain text email version
message.send()
如果你想包含一个纯文本版本的电子邮件,修改如下:
from django.template.loader import render_to_string
from django.core.mail import EmailMultiAlternatives # <= EmailMultiAlternatives instead of EmailMessage
plain_version = 'path/to/plain_version.html' # import plain version. No html content
html_version = 'path/to/html_version.html' # import html version. Has html content
plain_message = render_to_string(plain_version, { 'context': context, })
html_message = render_to_string(html_version, { 'context': context, })
message = EmailMultiAlternatives(subject, plain_message, from_email, [to_email])
message.attach_alternative(html_message, "text/html") # attach html version
message.send()
我的普通和html版本是这样的: plain_version.html:
Plain text {{ context }}
html_version.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
...
</head>
<body>
<table align="center" border="0" cellpadding="0" cellspacing="0" width="320" style="border: none; border-collapse: collapse; font-family: Arial, sans-serif; font-size: 14px; line-height: 1.5;">
...
{{ context }}
...
</table>
</body>
</html>
我知道这是一个老问题,但我也知道有些人就像我一样,总是在寻找最新的答案,因为旧的答案如果不更新有时会有过时的信息。
现在是2020年1月,我使用的是Django 2.2.6和Python 3.7
注意:我使用DJANGO REST框架,下面发送电子邮件的代码是在我的views.py的模型视图集中
所以在看了很多不错的答案之后,我就这么做了。
from django.template.loader import render_to_string
from django.core.mail import EmailMultiAlternatives
def send_receipt_to_email(self, request):
emailSubject = "Subject"
emailOfSender = "email@domain.com"
emailOfRecipient = 'xyz@domain.com'
context = ({"name": "Gilbert"}) #Note I used a normal tuple instead of Context({"username": "Gilbert"}) because Context is deprecated. When I used Context, I got an error > TypeError: context must be a dict rather than Context
text_content = render_to_string('receipt_email.txt', context, request=request)
html_content = render_to_string('receipt_email.html', context, request=request)
try:
#I used EmailMultiAlternatives because I wanted to send both text and html
emailMessage = EmailMultiAlternatives(subject=emailSubject, body=text_content, from_email=emailOfSender, to=[emailOfRecipient,], reply_to=[emailOfSender,])
emailMessage.attach_alternative(html_content, "text/html")
emailMessage.send(fail_silently=False)
except SMTPException as e:
print('There was an error sending an email: ', e)
error = {'message': ",".join(e.args) if len(e.args) > 0 else 'Unknown Error'}
raise serializers.ValidationError(error)
重要!那么render_to_string如何获得receipt_email.txt和receipt_email.html? 在我的settings.py中,我有TEMPLATES,下面是它的外观
注意DIRS,这里有一行os。path。join(BASE_DIR, 'templates', 'email_templates') 这一行使我的模板可访问。在我的project_dir中,我有一个文件夹叫做templates,还有一个子目录叫做email_templates,就像这个project_dir->templates->email_templates。我的模板receipt_email.txt和receipt_email.html在email_templates子目录下。
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates'), os.path.join(BASE_DIR, 'templates', 'email_templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
我再加上,我的receit_email。txt是这样的;
Dear {{name}},
Here is the text version of the email from template
我的receipt_email。html是这样的;
Dear {{name}},
<h1>Now here is the html version of the email from the template</h1>
在Django 1.7的send_email方法中加入了html_message参数。
html_message:如果提供了html_message,生成的电子邮件将是 多部分/可选的电子邮件,以消息作为文本/纯内容 Type和html_message作为文本/html内容类型。
所以你可以:
from django.core.mail import send_mail
from django.template.loader import render_to_string
msg_plain = render_to_string('templates/email.txt', {'some_params': some_params})
msg_html = render_to_string('templates/email.html', {'some_params': some_params})
send_mail(
'email title',
msg_plain,
'some@sender.com',
['some@receiver.com'],
html_message=msg_html,
)
示例....中有一个错误如果你像写的那样使用它,会出现以下错误:
< type '异常。异常' >:'dict'对象没有属性'render_context'
您将需要添加以下导入:
from django.template import Context
并将字典改为:
d = Context({ 'username': username })
看到http://docs.djangoproject.com/en/1.2/ref/templates/api/ rendering-a-context