用于分离消息内容和传输方法的django库
django-herald的Python项目详细描述
Django先驱报
一个Django消息传递库,其功能是:
- 基于类的声明和注册表方法,如django admin
- 每封邮件支持多种传输方法(电子邮件、短信、时差等)
- 基于浏览器的消息预览
- 维护消息发送尝试的历史记录,并可以查看这些消息
- 禁用每个用户的通知
python/django支持
我们试图使Herald支持Django的所有版本,而Django支持+介于两者之间的所有版本。
对于python,herald支持上述django版本支持的所有python版本。
因此,从Herald v0.2开始,我们支持Django 1.11、2.0、2.1和2.2,以及Python 2.7、3.4、3.5、3.6和3.7。
如果我有django x和pythonx,我需要什么版本的herald?
如果django/python版本组合在表中有一个--
,则不保证它受支持。
安装
pip安装django herald
将
herald
和django.contrib.sites
添加到已安装的应用程序中
添加先驱报的网址:
ifsettings.DEBUG:urlpatterns=[url(r'^herald/',include('herald.urls')),]+urlpatterns
< > >- 在任何django应用程序中创建
notifications.py
文件。这就是您的通知类所在的位置。添加这样的类: < > > 使用此文件结构创建用于呈现电子邮件的模板:
templates/ herald/ text/ welcome_email.txt html/ welcome_email.html
导航到
/herald/
,测试电子邮件的外观在代码中的任何需要的地方发送电子邮件:
WelcomeEmail(user).send()
在django admin中查看已发送的电子邮件,甚至可以重新发送。
< > >发件人的电子邮件地址
主题
:(str
,默认值:)电子邮件主题收件人电子邮件:(
列表[str]
,默认值:无
)要发送到的电子邮件字符串列表bcc
:(list[str]
,默认值:none
)要作为bcc发送的电子邮件字符串列表cc
:(list[str]
,默认值:none
)要作为cc发送的电子邮件字符串列表头
:(dict
,默认值:none
)要传递给emailmultialternatives
对象的额外头回复
:(列表[str]
,默认值:无
)作为回复电子邮件发送的电子邮件字符串列表附件
:(列表
)附件列表。有关详细信息,请参见下面的"电子邮件附件"- 由文件名、原始附件数据和mimetype组成的元组。附件数据由你来决定。像这样: < > >
一个mimebase对象。请参阅Django文档中电子邮件对象/附件下的附件文档。
一个django
文件
对象。 < > >
用法
fromheraldimportregistryfromherald.baseimportEmailNotificationclassWelcomeEmail(EmailNotification):# extend from EmailNotification for emailstemplate_name='welcome_email'# name of template, without extensionsubject='Welcome'# subject of emaildef__init__(self,user):# optionally customize the initializationself.context={'user':user}# set context for the template renderingself.to_emails=[user.email]# set list of emails to send to@staticmethoddefget_demo_args():# define a static method to return list of args needed to initialize class for testingfromusers.modelsimportUserreturn[User.objects.order_by('?')[0]]registry.register(WelcomeEmail)# finally, register your notification class# Alternatively, a class decorator can be used to register the notification:@registry.register_decorator()classWelcomeEmail(EmailNotification):...
电子邮件选项
可以在电子邮件通知类上设置以下选项。例如:
class WelcomeEmail(EmailNotification):
cc = ['test@example.com']
自动删除旧通知
Herald可以在发送新通知时自动删除旧通知。
要启用此功能,请将herald\u notification\u retention\u时间设置设置为timedelta实例。
例如:
HERALD_NOTIFICATION_RETENTION_TIME = timedelta(weeks=8)
每次发送新通知时,将删除所有早于8周的通知。
手动删除旧通知
delnotifs
命令可用于清除通知历史记录。
默认用法将删除今天发送的所有内容:
python manage.py delnotifs
但是,您也可以为start
或end
日期传递参数。结束
是截止日期,但不包括该日期。
python manage.py delnotifs --start='2016-01-01' --end='2016-01-10'
异步电子邮件发送
如果你给很多人发送的电子邮件略有不同,可能需要相当长的一段时间才能处理。默认情况下,django将同步处理这一切。对于异步支持,我们推荐django芹菜电子邮件。它非常易于设置和集成:https://github.com/pmclanahan/django-celerie email
herald.contrib.auth
django内置了发送密码重置电子邮件的支持。如果要使用herald发送这些电子邮件,可以使用herald.contrib.auth中的通知类。
首先,将herald.contrib.auth
添加到已安装的应用程序中(除了
herald
)。
其次,使用heraldpasswordresetform
代替django内置的passwordresetform
。这一步完全取决于您的项目结构,但它实际上只涉及以某种方式更改密码重置视图上的窗体类:
# you may simply just need to override the password reset url like so:url(r'^password_reset/$',password_reset,name='password_reset',{'password_reset_form':HeraldPasswordResetForm}),# of if you are using something like django-authtools:url(r'^password_reset/$',PasswordResetView.as_view(form_class=HeraldPasswordResetForm),name='password_reset'),# or you may have a customized version of the password reset view:classMyPasswordResetView(FormView):form_class=HeraldPasswordResetForm# change the form class here# or, you may have a custom password reset form already. In that case, you will want to extend from the HeraldPasswordResetForm:classMyPasswordResetForm(HeraldPasswordResetForm):...# alternatively, you could even just send the notification wherever you wish, seperate from the form:PasswordResetEmail(some_user).send()
第三,您可能需要自定义电子邮件的模板。默认情况下,herald将使用django为电子邮件的html和文本版本提供的registration/password_reset_email.html
。但是您可以简单地覆盖herald/html/password_reset.html
和/或herald/text/password_reset.txt
以满足您的需要。
用户禁用通知
如果要禁用每个用户的某些通知,请将记录添加到usernotification表中,然后 将通知添加到禁用的通知多对多表。
例如:
ifsettings.DEBUG:urlpatterns=[url(r'^herald/',include('herald.urls')),]+urlpatterns0
默认情况下,可以禁用通知。您可以在通知类中设置can disable=false,系统将 使用此默认值填充数据库。通知类还可以通过在 继承的通知类。像这样:
ifsettings.DEBUG:urlpatterns=[url(r'^herald/',include('herald.urls')),]+urlpatterns1
电子邮件附件
要发送附件,请将附件列表分配给emailnotification实例的attachments属性,或重写get_attachments()方法。
列表中的每个附件都可以是以下附件之一:
ifsettings.DEBUG:urlpatterns=[url(r'^herald/',include('herald.urls')),]+urlpatterns2
内联附件
有时你想直接在电子邮件内容中嵌入一个图像。使用mimeimage将内容id头分配给mimeimage,如下所示:
ifsettings.DEBUG:urlpatterns=[url(r'^herald/',include('herald.urls')),]+urlpatterns3
您可以使用如下内容ID(CID)在HTML电子邮件模板中引用这些图像:
ifsettings.DEBUG:urlpatterns=[url(r'^herald/',include('herald.urls')),]+urlpatterns4
当然,您需要将"图像文件"添加到上面示例中的模板上下文中。您还可以使用文件操作来完成此操作。在本例中,我们重写了电子邮件通知的get_attachments方法。
ifsettings.DEBUG:urlpatterns=[url(r'^herald/',include('herald.urls')),]+urlpatterns5
在模板中,您可以这样引用它,而不需要向上下文中添加任何内容:
ifsettings.DEBUG:urlpatterns=[url(r'^herald/',include('herald.urls')),]+urlpatterns6
其他mime附件
您也可以将任何mimebase对象作为常规附件附加,但必须添加内容处置头,否则将无法访问它们:
ifsettings.DEBUG:urlpatterns=[url(r'^herald/',include('herald.urls')),]+urlpatterns7
附件会导致数据库变得很大,因此您应该确保运行管理命令以清除数据库中的旧邮件。
运行测试
ifsettings.DEBUG:urlpatterns=[url(r'^herald/',include('herald.urls')),]+urlpatterns8