使用django模板系统发送电子邮件
django-mail-templated的Python项目详细描述
使用django模板系统发送电子邮件
这是标准EmailMessage类的一个小包装 send_mail()函数,提供创建电子邮件的简单方法 使用Django template system。 只需传递template_name和context作为第一个参数,然后使用 正常。
功能
- 内置OOP、接吻和灵活性。很小很简单,但是 但我希望是全功能的。
- 扩展并模拟内置django的EmailMessage和 send_mail()。尽可能兼容。
- 完全支持django模板系统,包括模板继承 (感谢bradwittington关于这个问题的注释)。
- 支持任何可能的模板引擎和加载程序。
- 支持序列化(感谢arjandepoter)。
- 全套测试。
- 用django 1.4-1.9测试。
- 与Python3兼容。
快速启动
运行:
pip install django-mail-templated
并在您的设置文件中注册应用程序:
INSTALLED_APPS=(...'mail_templated')
创建模板:
{%extends"mail_templated/base.tpl"%}{%blocksubject%} Hello {{user.name}}{%endblock%}{%blockbody%}{{user.name}}, this is a plain text message. {%endblock%}{%blockhtml%}{{user.name}}, this is an <strong>html</strong> message. {%endblock%}
发送消息:
frommail_templatedimportsend_mailsend_mail('email/hello.tpl',{'user':user},from_email,[user.email])
高级用法:
frommail_templatedimportEmailMessagemessage=EmailMessage('email/hello.tpl',{'user':user},from_email,to=[user.email])# TODO: Add more useful commands here.message.send()
更多有用的信息和示例请参见http://django-mail-templated.readthedocs.org