Django电子邮件为HTML [英] Django email message as HTML
问题描述
我有一个电子邮件模板,用于发送各种电子邮件。我不想保留多个电子邮件HTML模板,因此处理此问题的最佳方法是自定义消息内容。像这样:
I have an email template that I use to send emails of different kinds. I'd rather not keep multiple email HTML templates, so the best way to handle this is to customize the message contents. Like so:
def email_form(request):
html_message = loader.render_to_string(
'register/email-template.html',
{
'hero': 'email_hero.png',
'message': 'We\'ll be contacting you shortly! If you have any questions, you can contact us at <a href="#">meow@something.com</a>',
'from_email': 'lala@lala.com',
}
)
email_subject = 'Thank you for your beeswax!'
to_list = 'johndoe@whatever.com'
send_mail(email_subject, 'message', 'from_email', [to_list], fail_silently=False, html_message=html_message)
return
但是,发送电子邮件时, html代码不起作用。该消息将按原样显示,并带有尖括号和所有内容。我有办法强迫它呈现为HTML标签吗?
When the email is sent however, the html codes don't work. The message appears as it is exactly, angled brackets and all. Is there a way for me to force it to render as HTML tags?
推荐答案
解决了它。不是很优雅,但是确实可以。如果有人好奇,则应按以下方式实现放置在电子邮件模板中的变量:
Solved it. Not very elegant, but it does work. In case anyone's curious, the variable placed in the email template should be implemented as so:
{{ your_variable|safe|escape }}
然后就可以了!谢谢大家!
Then it works! Thanks guys!
这篇关于Django电子邮件为HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!