面向Django的模板/事务电子邮件抽象
项目描述
- 信息:
一个面向Django的模板电子邮件发送类
- 测试:
概述
django-templated-email旨在发送模板电子邮件,适用于使用事务邮件发送器(支持MailchimpSTS和PostageApp),但默认情况下使用一个后端类,该类使用Django的模板系统和Django的核心邮件功能。该库支持模板继承,添加抄送和暗送收件人,可配置模板命名和位置,并可在后端/提供者之间轻松切换。
send_templated_email方法可以被视为电子邮件的render_to_response快捷方式。
开始 - 安装
安装
pip install django-templated-email
您可以将以下内容添加到settings.py文件中(但默认情况下即可工作)
TEMPLATED_EMAIL_BACKEND = 'templated_email.backends.vanilla_django.TemplateBackend' # You can use a shortcut version TEMPLATED_EMAIL_BACKEND = 'templated_email.backends.vanilla_django' # You can also use a class directly from templated_email.backends.vanilla_django import TemplateBackend TEMPLATED_EMAIL_BACKEND = TemplateBackend
发送模板电子邮件
使用vanilla_django TemplateBackend后端进行示例使用
使用Python发送邮件
from templated_email import send_templated_mail send_templated_mail( template_name='welcome', from_email='from@example.com', recipient_list=['to@example.com'], context={ 'username':request.user.username, 'full_name':request.user.get_full_name(), 'signup_date':request.user.date_joined }, # Optional: # cc=['cc@example.com'], # bcc=['bcc@example.com'], # headers={'My-Custom-Header':'Custom Value'}, # template_prefix="my_emails/", # template_suffix="email", )
如果您想更精细地控制发送邮件,可以使用get_templated_email,它将返回一个Django EmailMessage对象,并使用vanilla_django后端进行准备。
from templated_email import get_templated_mail get_templated_mail( template_name='welcome', from_email='from@example.com', to=['to@example.com'], context={ 'username':request.user.username, 'full_name':request.user.get_full_name(), 'signup_date':request.user.date_joined }, # Optional: # cc=['cc@example.com'], # bcc=['bcc@example.com'], # headers={'My-Custom-Header':'Custom Value'}, # template_prefix="my_emails/", # template_suffix="email", )
您还可以使用 cc 和 bcc 收件人,格式为 cc=[‘example@example.com’]。某些后端有其他可以覆盖的参数,请参阅以下内容。
您的模板
需要将 templated_email/ 目录设置为模板目录。
后端将查找 my_app/templates/templated_email/welcome.email
{% block subject %}My subject for {{username}}{% endblock %} {% block plain %} Hi {{full_name}}, You just signed up for my website, using: username: {{username}} join date: {{signup_date}} Thanks, you rock! {% endblock %}
如果您想在邮件中包含HTML部分,只需使用‘html’块即可。
{% block html %} <p>Hi {{full_name}},</p> <p>You just signed up for my website, using: <dl> <dt>username</dt><dd>{{username}}</dd> <dt>join date</dt><dd>{{signup_date}}</dd> </dl> </p> <p>Thanks, you rock!</p> {% endblock %}
纯文本部分也可以通过 html2text 从HTML计算得出。如果您未指定纯文本块且已安装 html2text 包,则纯文本部分将自动从HTML部分计算得出。您可以在 settings.py 中禁用此行为。
TEMPLATED_EMAIL_AUTO_PLAIN = False
您可以使用以下变量在 settings.py 中全局覆盖模板目录和文件扩展名:
TEMPLATED_EMAIL_TEMPLATE_DIR = 'templated_email/' #use '' for top level template dir, ensure there is a trailing slash TEMPLATED_EMAIL_FILE_EXTENSION = 'email'
对于 vanilla_django 和 mailchimp_sts 后端,您可以为每次调用 send_templated_mail 设置 template_prefix 和 template_suffix 的值(或使用更不兼容后端的 template_dir / file_extension),如果您希望将一组模板存储在不同的目录中。请记住包括尾随斜杠。
有关模板继承的注意/警告
模板继承支持非常基础(在模板中使用 {% extends … %})。如果您使用 {{block.super}},会遇到问题,并导致邮件部分为空白。
遗留行为
库的 0.2.x 版本会在 django 模板目录/加载器中查找 templated_email/welcome.txt
Hey {{full_name}}, You just signed up for my website, using: username: {{username}} join date: {{signup_date}} Thanks, you rock!
它将使用 templated_email/welcome.html 作为邮件的HTML部分,让您可以使其更加美观。
未来计划
请参阅 https://github.com/bradwhittington/django-templated-email/issues?state=open
在第三方应用程序中使用 django_templated_email
如果您想在可重用应用程序中处理邮件,请注意以下内容:
您对 send_templated_mail 的调用应设置 template_dir 的值,这样您就可以将您应用程序特定的模板副本存储在您的应用程序中(尽管如果将它们存储在 <your app>/templates/templated_email 中,加载器将找到您的电子邮件模板,如果 TEMPLATED_EMAIL_TEMPLATE_DIR 未被覆盖)
如果您(应该)设置了 template_dir 的值,请记住包括尾随斜杠,例如 ‘my_app_email/’
部署的应用程序可能使用不同的后端,该后端不使用 django 模板后端,因此请在您的 README 中提醒开发者,如果他们已经使用不同的后端使用 django_templated_email,他们将需要确保他们的电子邮件提供商可以发送所有模板(理想情况下,在方便的位置列出它们)
有关特定后端的说明
使用 vanilla_django
这是默认后端,因此不需要特殊配置,并且可以直接使用。默认情况下,它假设以下设置(如果您想覆盖它们)
TEMPLATED_EMAIL_TEMPLATE_DIR = 'templated_email/' #Use '' for top level template dir TEMPLATED_EMAIL_FILE_EXTENSION = 'email'
为了遗留目的,您可以在设置文件中指定电子邮件主题(但,首选方法是使用模板中的 {% block subject %})
TEMPLATED_EMAIL_DJANGO_SUBJECTS = { 'welcome':'Welcome to my website', }
此外,您可以调用 send_templated_mail 并可选地覆盖以下参数
template_prefix='your_template_dir/' # Override where the method looks for email templates (alternatively, use template_dir) template_suffix='email' # Override the file extension of the email templates (alternatively, use file_extension) cc=['fubar@example.com'] # Set a CC on the mail bcc=['fubar@example.com'] # Set a BCC on the mail template_dir='your_template_dir/' # Override where the method looks for email templates connection=your_connection # Takes a django mail backend connection, created using **django.core.mail.get_connection** auth_user='username' # Override the user that the django mail backend uses, per **django.core.mail.send_mail** auth_password='password' # Override the password that the django mail backend uses, per **django.core.mail.send_mail**
使用 PostageApp
要使用 PostageApp(《http://postageapp.com》)发送方法,您需要安装 python-postageapp
pip install -e git://github.com/bradwhittington/python-postageapp.git#egg=postageapp
并将以下内容添加到您的 settings.py 中
TEMPLATED_EMAIL_BACKEND = 'templated_email.backends.postageapp_backend.TemplateBackend' POSTAGEAPP_API_KEY = 'yourapikey' #If you are already using django-postageapp: EMAIL_POSTAGEAPP_API_KEY = POSTAGEAPP_API_KEY
使用 MAILCHIMP STS
要使用 MailChimp STS 发送方法,您需要安装 mailsnake(请注意,在 main mailsnake 支持 STS 之前,您需要使用我的分支)
pip install -e git://github.com/nitinhayaran/greatape.git#egg=greatape
并将以下内容添加到您的 settings.py 中
TEMPLATED_EMAIL_BACKEND = 'templated_email.backends.mailchimp_sts.TemplateBackend' MAILCHIMP_API_KEY = 'yourapikey' # For the django back-end specifically TEMPLATED_EMAIL_MAILCHIMP = { 'welcome':{ 'subject':'Welcome to my website', 'track_opens':True, 'track_clicks':False, 'tags':['my','little','pony'], } }
Mailchimp STS 发送者使用与 VanillaDjango 后端相同的模板处理器,因此您可以全局覆盖以下设置
TEMPLATED_EMAIL_TEMPLATE_DIR = 'templated_email/' #use '' for top level template dir TEMPLATED_EMAIL_FILE_EXTENSION = 'email'
在调用 send_templated_mail 时,您还可以覆盖 template_dir 变量
项目详情
下载文件
下载您平台上的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。
源分布
vinta-django-templated-email-0.5.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | acdc19838bf49b168e1e11f482812ebe0e1860b7ce730af359bce3a0e2114cb0 |
|
MD5 | 79a314a86249ed201cf08440c868b0d9 |
|
BLAKE2b-256 | 9e531cf7ebe13350a728be6b0a545ba0f8fa513f014f593d4f2344e6e8f27d1d |