我想发送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的模板引擎生成电子邮件?
当前回答
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>
其他回答
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>
我写了一个代码片段,允许您发送使用存储在数据库中的模板渲染的电子邮件。一个例子:
EmailTemplate.send('expense_notification_to_admin', {
# context object that email template will be rendered with
'expense': expense_request,
})
我做了django-template -email来解决这个问题,灵感来自于这个解决方案(在某些时候,需要从使用django模板切换到使用mailchimp等一组模板来处理事务,为我自己的项目创建模板邮件)。虽然这仍然是一个正在进行的工作,但对于上面的例子,你可以这样做:
from templated_email import send_templated_mail
send_templated_mail(
'email',
'from@example.com',
['to@example.com'],
{ 'username':username }
)
在settings.py中添加以下内容(以完成示例):
TEMPLATED_EMAIL_DJANGO_SUBJECTS = {'email':'hello',}
它会自动在普通的django模板dirs/loaders中分别为plain部分和html部分寻找名为templated_email/email.txt和templated_email/email.html的模板(如果找不到其中至少一个会报错)。
如果您想要为您的邮件设置动态电子邮件模板,请将电子邮件内容保存在数据库表中。 这是我保存为HTML代码在database =
<p>Hello.. {{ first_name }} {{ last_name }}. <br> This is an <strong>important</strong> {{ message }}
<br> <b> By Admin.</b>
<p style='color:red'> Good Day </p>
在你看来:
from django.core.mail import EmailMultiAlternatives
from django.template.loader import get_template
def dynamic_email(request):
application_obj = AppDetails.objects.get(id=1)
subject = 'First Interview Call'
email = request.user.email
to_email = application_obj.email
message = application_obj.message
text_content = 'This is an important message.'
d = {'first_name': application_obj.first_name,'message':message}
htmly = FirstInterviewCall.objects.get(id=1).html_content #this is what i have saved previously in database which i have to send as Email template as mentioned above HTML code
open("partner/templates/first_interview.html", "w").close() # this is the path of my file partner is the app, Here i am clearing the file content. If file not found it will create one on given path.
text_file = open("partner/templates/first_interview.html", "w") # opening my file
text_file.write(htmly) #putting HTML content in file which i saved in DB
text_file.close() #file close
htmly = get_template('first_interview.html')
html_content = htmly.render(d)
msg = EmailMultiAlternatives(subject, text_content, email, [to_email])
msg.attach_alternative(html_content, "text/html")
msg.send()
这将发送您保存在Db中的动态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