用于分离消息内容和传输方法的django库
mindbogglr-django-herald的Python项目详细描述
django-herald-msg91
设置差异:
安装
pip安装mindbogglr django herald
设置
在django设置文件中,添加以下设置:
MSG91_TRANSACTIONAL_SENDER_ID = 'SOCKET'
MSG91_AUTHKEY = '228352HGNg5dF65b57672G'
将"socket"替换为用户文本消息收件箱中所需的任何6个Alphebet系列。
将"228352HGNG5DF65B57672G"替换为来自msg91帐户的authkey
注意:它将显示为bt-socket或类似字符…前两个字母表示文本消息提供程序和区域,不可控制。用法
fromheraldimportregistryfromherald.text_notificationimportMsg91TextNotification@registry.register_decorator()classInvitationMessage(Msg91TextNotification):template_name='invitation_message'# name of template, without extensiondef__init__(self,to_number,invited_by_user,user_full_name,invitation_url):# set context for the template renderingself.context={'invited_by_user':invited_by_user,'user_full_name':user_full_name,'invitation_url':invitation_url}self.to_number=to_number
原始自述文件
安装
pip安装django herald
- 添加
herald
和django.contrib.sites
到已安装的应用程序 - 添加先驱报的网址:
ifsettings.DEBUG:urlpatterns=[url(r'^herald/',include('herald.urls')),]+urlpatterns
用法
- 在任何django应用程序中创建
notifications.py
文件。这就是您的通知类所在的位置。添加这样的类:
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):...
使用此文件结构创建用于呈现电子邮件的模板:
templates/ herald/ text/ welcome_email.txt html/ welcome_email.html
导航到
/herald/
,测试电子邮件的外观在代码中的任何需要的地方发送电子邮件:
WelcomeEmail(user).send()
在django admin中查看已发送的电子邮件,甚至可以重新发送。
删除旧通知
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表中,然后 将通知添加到禁用的通知多对多表。
例如:
MSG91_TRANSACTIONAL_SENDER_ID = 'SOCKET'
MSG91_AUTHKEY = '228352HGNg5dF65b57672G'
0
默认情况下,可以禁用通知。您可以在通知类中设置can disable=false,系统将 使用此默认值填充数据库。通知类还可以通过在 继承的通知类。像这样:
MSG91_TRANSACTIONAL_SENDER_ID = 'SOCKET'
MSG91_AUTHKEY = '228352HGNg5dF65b57672G'
1
电子邮件附件
要发送附件,请将附件列表分配给emailnotification实例的attachments属性,或重写get_attachments()方法。
列表中的每个附件都可以是以下附件之一:
- 由文件名、原始附件数据和mimetype组成的元组。附件数据由你来决定。像这样:
MSG91_TRANSACTIONAL_SENDER_ID = 'SOCKET'
MSG91_AUTHKEY = '228352HGNg5dF65b57672G'
2
一个mimebase对象。请参阅Django文档中电子邮件对象/附件下的附件文档。
一个django
文件
对象。
内联附件
有时你想直接在电子邮件内容中嵌入一个图像。使用mimeimage将内容id头分配给mimeimage,如下所示:
MSG91_TRANSACTIONAL_SENDER_ID = 'SOCKET'
MSG91_AUTHKEY = '228352HGNg5dF65b57672G'
3
您可以使用如下内容ID(CID)在HTML电子邮件模板中引用这些图像:
MSG91_TRANSACTIONAL_SENDER_ID = 'SOCKET'
MSG91_AUTHKEY = '228352HGNg5dF65b57672G'
4
您当然需要将"图像文件"添加到模板cont上面例子中的ext。您还可以使用文件操作来完成此操作。在本例中,我们重写了电子邮件通知的get_attachments方法。
MSG91_TRANSACTIONAL_SENDER_ID = 'SOCKET'
MSG91_AUTHKEY = '228352HGNg5dF65b57672G'
5
在模板中,您可以这样引用它,而不需要向上下文中添加任何内容:
MSG91_TRANSACTIONAL_SENDER_ID = 'SOCKET'
MSG91_AUTHKEY = '228352HGNg5dF65b57672G'
6
其他mime附件
您也可以将任何mimebase对象作为常规附件附加,但必须添加内容处置头,否则将无法访问它们:
MSG91_TRANSACTIONAL_SENDER_ID = 'SOCKET'
MSG91_AUTHKEY = '228352HGNg5dF65b57672G'
7
附件会导致数据库变得很大,因此您应该确保运行管理命令以清除数据库中的旧邮件。
运行测试
MSG91_TRANSACTIONAL_SENDER_ID = 'SOCKET'
MSG91_AUTHKEY = '228352HGNg5dF65b57672G'
8