[英]django add html email template
This code is from django_messages application.Even though template is supposed to be html, i still get raw html code in email body.What needs to be done in this case to send both text version and properly working HTML version? 此代码来自django_messages应用程序。即使模板应该是html,我仍然在电子邮件正文中获取原始html代码。在这种情况下,发送文本版本和正常工作的HTML版本需要做什么?
{% load i18n %}
{% load url from future %}
{% blocktrans with recipient=message.recipient sender=message.sender %}Hello {{ recipient }},
you received a private message from {{ sender }} with
the following contents:{% endblocktrans %}
{{ message.body|safe }}
--
{% blocktrans %}Sent from {{ site_url }}{% endblocktrans %}
Above is new_message.html file 上面是new_message.html文件
Below is actual code for sending email 以下是发送电子邮件的实际代码
def new_message_email(sender, instance, signal,
subject_prefix=_(u'New Message: %(subject)s'),
template_name="messages/new_message.html",
default_protocol=None,
*args, **kwargs):
"""
This function sends an email and is called via Django's signal framework.
Optional arguments:
``template_name``: the template to use
``subject_prefix``: prefix for the email subject.
``default_protocol``: default protocol in site URL passed to template
"""
if default_protocol is None:
default_protocol = getattr(settings, 'DEFAULT_HTTP_PROTOCOL', 'http')
if 'created' in kwargs and kwargs['created']:
try:
current_domain = Site.objects.get_current().domain
subject = subject_prefix % {'subject': instance.subject}
message = render_to_string(template_name, {
'site_url': '%s://%s' % (default_protocol, current_domain),
'message': instance,
})
if instance.recipient.email != "":
send_mail(subject, message, settings.DEFAULT_FROM_EMAIL,
[instance.recipient.email,])
except Exception as e:
#print e
pass #fail silently
if 'created' in kwargs and kwargs['created']:
try:
current_domain = Site.objects.get_current().domain
subject = subject_prefix % {'subject': instance.subject}
message = render_to_string(template_name, {
'site_url': '%s://%s' % (default_protocol, current_domain),
'message': instance,
})
if instance.recipient.email != "":
send_mail(subject, message, settings.DEFAULT_FROM_EMAIL,
[instance.recipient.email,])
except Exception as e:
#print e
pass #fail silently
This is new code i tried by following example but it is not working. 这是我通过以下示例尝试的新代码,但无法正常工作。
if 'created' in kwargs and kwargs['created']:
try:
current_domain = Site.objects.get_current().domain
subject = subject_prefix % {'subject': instance.subject}
html_content = render_to_string(template_name, {
'site_url': '%s://%s' % (default_protocol, current_domain),
'message': instance,
})
if instance.recipient.email != "":
text_content = strip_tags(html_content)
msg = EmailMultiAlternatives(subject, text_content, settings.DEFAULT_FROM_EMAIL,
[instance.recipient.email,])
msg.attach_alternative(html_content, "text/html")
send_mail(msg)
except Exception as e:
#print e
pass #fail silently
In Django 1.7+, you can specify html_message
when using send_mail
. 在Django 1.7+中,可以在使用
send_mail
时指定html_message
。 The message
parameter is still required for clients that do not support html emails. 对于不支持html电子邮件的客户端,仍然需要
message
参数。
For Django 1.6 and earlier, see the instructions for sending alternative content types . 对于Django 1.6及更早版本,请参阅有关发送替代内容类型的说明 。 Instead of calling
send_mail
, you construct an email message, set the html content, then send it. 无需调用
send_mail
,而是构造电子邮件,设置html内容,然后发送。
from django.core.mail import EmailMultiAlternatives
subject, 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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.