在Django 1.7中发送带有html的电子邮件 [英] Sending email with html in Django 1.7

查看:438
本文介绍了在Django 1.7中发送带有html的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

send_mail()中,我们有一个新参数 - html_message 文档

In send_mail() we have one new parameter - html_message. Docs

我有 email.html 文件,我想发送我的消息的html版本。我找不到任何Django 1.7的例子。

I have email.html file and I want to send html version of my message. I can't find any example for Django 1.7.

你能告诉我一个办法吗?我需要使用 os.open()我的html文件?

Can you show me a way, how to do this? Does I need to use os.open() my html file?

谢谢!

推荐答案

render_to_string :加载模板,呈现并返回生成的字符串
html_message :如果提供了 html_message ,则使用Html消息替换默认消息。

render_to_string : which loads a template, renders it and returns the resulting string. html_message : If html_message is provided, the default message replaced with Html message.

mail / html-message.html

Hi {{ first_name }}.

    This is your {{ email }}

Thank you

views.py

def mail_function(request):
    subject = 'Test Mail'
    from = 'info@domain.com'
    to = 'to@domain.com'
    c = Context({'email': email,
                 'first_name': first_name})
    html_content = render_to_string('mail/html-message.html', c)
    txtmes = render_to_string('mail/text-message.html', c)
    send_mail(subject,
              txtmes,
              from,
              [to],
              fail_silently=False,
              html_message=html_content)

这篇关于在Django 1.7中发送带有html的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆