Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add notification's emails HTML alternative + subject translation #170

Merged
merged 2 commits into from
Sep 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions docs/notifications.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
Notifications
=============

Configure notifications
-----------------------

See those settings:
* :ref:`PYBB_DISABLE_SUBSCRIPTIONS`
* :ref:`PYBB_DEFAULT_AUTOSUBSCRIBE`
* :ref:`PYBB_DISABLE_NOTIFICATIONS`
* :ref:`PYBB_USE_DJANGO_MAILER`

When notifications are sent
---------------------------

Each time a post is saved (created or updated), subcribers will receive an email.
If you configure PYBB to use django-mailer (see :ref:`PYBB_USE_DJANGO_MAILER`), emails
will be sent when your cron job will run. Else, emails will be sent when post is saved.

Overwrite emails templates
--------------------------

You can overwrite three templates:

* `pybb/mail_templates/subscription_email_subject.html`: will be used to render the subject of the email
* `pybb/mail_templates/subscription_email_body.html`: will be used to render the text version's body of the email
* `pybb/mail_templates/subscription_email_body-html.html`: will be used to render the html version's body of the email


My test user is not receiving emails ?!
---------------------------------------

Emails matching this rules are not sent: <username>@exemple.com
18 changes: 18 additions & 0 deletions docs/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,24 @@ Builtin templates will inherit this template

Default: 'base.html

.. _PYBB_TEMPLATE_MAIL_TXT:

PYBB_TEMPLATE_MAIL_TXT
.............

Builtin `txt` emails templates will inherit this template

Default: 'pybb/mail_templates/base.html

.. _PYBB_TEMPLATE_MAIL_HTML:

PYBB_TEMPLATE_MAIL_HTML
.............

Builtin `html` emails templates will inherit this template

Default: 'pybb/mail_templates/base-html.html


Markup engines
--------------
Expand Down
33 changes: 32 additions & 1 deletion pybb/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,38 @@
from django.conf import settings
from django.utils.encoding import force_text
from unidecode import unidecode

from pybb import defaults

if defaults.PYBB_USE_DJANGO_MAILER:
from mailer import send_html_mail, send_mail
else:
from django.core.mail import send_mail, get_connection
from django.core.mail.message import EmailMultiAlternatives

def send_html_mail(subject, text_msg, html_msg, sender, recipient,
fail_silently=False, auth_user=None, auth_password=None, connection=None):
"""Sends an email with HTML alternative."""
connection = connection or get_connection(username=auth_user,
password=auth_password,
fail_silently=fail_silently)
msg = EmailMultiAlternatives(subject, text_msg, sender, recipient, connection=connection)
msg.attach_alternative(html_msg, "text/html")
msg.send()


def send_mass_html_mail(emails, *args, **kwargs):
"""
Sends emails with html alternative if email item has html content.
Email item is a tuple with an optionnal html message version :
(subject, text_msg, sender, recipient, [html_msg])
"""
for email in emails:
subject, text_msg, sender, recipient = email[0:4]
html_msg = email[4] if len(email) > 4 else ''
if html_msg:
send_html_mail(subject, text_msg, html_msg, sender, recipient, *args, **kwargs)
else:
send_mail(subject, text_msg, sender, recipient, *args, **kwargs)

def get_image_field_class():
try:
Expand Down
2 changes: 2 additions & 0 deletions pybb/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ def processor(request):
context = {}
for i in (
'PYBB_TEMPLATE',
'PYBB_TEMPLATE_MAIL_HTML',
'PYBB_TEMPLATE_MAIL_TXT',
'PYBB_DEFAULT_AVATAR_URL',
'PYBB_MARKUP',
'PYBB_DEFAULT_TITLE',
Expand Down
6 changes: 6 additions & 0 deletions pybb/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ def getsetting_with_deprecation_check(all_settings, setting_name):
'PYBB_MARKUP_ENGINES_PATHS')

PYBB_TEMPLATE = getattr(settings, 'PYBB_TEMPLATE', "base.html")
PYBB_TEMPLATE_MAIL_TXT = getattr(settings,
'PYBB_TEMPLATE_MAIL_TXT',
'pybb/mail_templates/base.html')
PYBB_TEMPLATE_MAIL_HTML = getattr(settings,
'PYBB_TEMPLATE_MAIL_HTML',
'pybb/mail_templates/base-html.html')
PYBB_DEFAULT_AUTOSUBSCRIBE = getattr(settings, 'PYBB_DEFAULT_AUTOSUBSCRIBE', True)
PYBB_ENABLE_ANONYMOUS_POST = getattr(settings, 'PYBB_ENABLE_ANONYMOUS_POST', False)
PYBB_ANONYMOUS_USERNAME = getattr(settings, 'PYBB_ANONYMOUS_USERNAME', 'Anonymous')
Expand Down
Binary file modified pybb/locale/fr/LC_MESSAGES/django.mo
Binary file not shown.
70 changes: 40 additions & 30 deletions pybb/locale/fr/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ msgid ""
msgstr ""
"Project-Id-Version: pybbm\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2015-06-09 10:07-0500\n"
"PO-Revision-Date: 2015-06-09 17:24+0100\n"
"Last-Translator: DylannCordel <d.cordel@webu.coop>\n"
"POT-Creation-Date: 2015-06-14 20:00+0200\n"
"PO-Revision-Date: 2015-06-14 20:04+0100\n"
"Last-Translator: Dylann Cordel <cordel[email protected]>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"Language: \n"
"MIME-Version: 1.0\n"
Expand Down Expand Up @@ -86,7 +86,6 @@ msgid "Poll question"
msgstr "Question de sondage"

#: forms.py:62
#| msgid "Topics"
msgid "Topic slug"
msgstr "Slug du sujet"

Expand All @@ -99,7 +98,6 @@ msgstr "Vous devez spécifier une question lorsque vous ajoutez un sondage"
#: models.py:256
#: models.py:329
#: models.py:468
#: templates/pybb/mail_templates/subscription_email_body.html:4
msgid "User"
msgstr "Utilisateur"

Expand Down Expand Up @@ -236,7 +234,7 @@ msgstr "Sujet"

#: models.py:163
#: templates/pybb/forum_list.html:11
#: templates/pybb/user_topics.html:9
#: templates/pybb/user_topics.html:8
msgid "Topics"
msgstr "Sujets"

Expand All @@ -262,7 +260,7 @@ msgstr "Message"
#: templates/pybb/topic.html:28
#: templates/pybb/topic.html.py:48
#: templates/pybb/topic_list.html:10
#: templates/pybb/user_posts.html:9
#: templates/pybb/user_posts.html:8
msgid "Posts"
msgstr "Messages"

Expand Down Expand Up @@ -545,9 +543,25 @@ msgstr "Marquer tous les forums comme lus"
msgid "Mark all topics as read"
msgstr "Marquer tous les sujets comme lus"

#: templates/pybb/mail_templates/subscription_email_body-html.html:16
#: templates/pybb/mail_templates/subscription_email_body.html:4
msgid "replied in topic to which you are subscribed."
msgstr "a répondu dans un sujet auquel vous êtes abonné."
#, python-format
#| msgid "replied in topic to which you are subscribed."
msgid "User %(username)s replied in topic to which you are subscribed."
msgstr "L'utilisateur %(username)s a répondu dans un sujet auquel vous êtes abonné."

#: templates/pybb/mail_templates/subscription_email_body-html.html:19
#, python-format
msgid "Follow %(link_post_start)sthis link%(link_end)s to see the new post on the topic %(link_topic_start)s%(topic_name)s%(link_end)s."
msgstr "Suivez %(link_post_start)sce lien%(link_end)s pour voir le nouveau message du sujet %(link_topic_start)s%(topic_name)s%(link_end)s."

#: templates/pybb/mail_templates/subscription_email_body-html.html:23
#, python-format
#| msgid ""
#| "If you don't want to recive notifications on new messages in this topic "
#| "visit following link:"
msgid "If you don't want to receive notifications on new posts in this topic visit following %(link_start)s this link%(link_end)s."
msgstr "Si vous ne souhaitez pas recevoir de notifications pour les nouveaux messages de ce sujet, visitez %(link_start)sce lien%(link_end)s."

#: templates/pybb/mail_templates/subscription_email_body.html:5
msgid "Link to post:"
Expand All @@ -558,8 +572,11 @@ msgid "Link to topic:"
msgstr "Lien vers le sujet :"

#: templates/pybb/mail_templates/subscription_email_body.html:9
msgid "If you don't want to recive notifications on new messages in this topic visit following link:"
msgstr "Si vous ne souhaitez pas recevoir de notifications pour les nouveaux messages de ce sujet, visitez le lien :"
#| msgid ""
#| "If you don't want to recive notifications on new messages in this topic "
#| "visit following link:"
msgid "If you don't want to receive notifications on new messages in this topic visit following link:"
msgstr "Si vous ne souhaitez pas recevoir de notifications pour les nouveaux messages de ce sujet, visitez le lien suivant :"

#: templates/pybb/mail_templates/subscription_email_subject.html:2
msgid "New answer in topic that you subscribed."
Expand Down Expand Up @@ -588,32 +605,26 @@ msgid "Second Level Heading"
msgstr "Titre de second niveau"

#: templates/pybb/markup/markdown_widget.html:20
#| msgid "Headline"
msgid "Heading 3"
msgstr "Titre 3"

#: templates/pybb/markup/markdown_widget.html:21
#| msgid "Headline"
msgid "Heading 4"
msgstr "Titre 4"

#: templates/pybb/markup/markdown_widget.html:22
#| msgid "Headline"
msgid "Heading 5"
msgstr "Titre 5"

#: templates/pybb/markup/markdown_widget.html:23
#| msgid "Headline"
msgid "Heading 6"
msgstr "Titre 6"

#: templates/pybb/markup/markdown_widget.html:28
#| msgid "Bulleted list"
msgid "Bulleted List"
msgstr "Liste à puces"

#: templates/pybb/markup/markdown_widget.html:29
#| msgid "Numeric list"
msgid "Numeric List"
msgstr "Liste numérotée"

Expand Down Expand Up @@ -654,7 +665,7 @@ msgid "Rank"
msgstr "Classement"

#: templates/pybb/post_template.html:42
#: templates/pybb/user.html:47
#: templates/pybb/user.html:49
msgid "Edit"
msgstr "Modifier"

Expand Down Expand Up @@ -727,41 +738,41 @@ msgstr "Aller au premier message non lu"
msgid "pages"
msgstr "pages"

#: templates/pybb/user.html:16
#: templates/pybb/user.html:18
msgid "Statistics"
msgstr "Statistiques"

#: templates/pybb/user.html:19
#: templates/pybb/user.html:21
msgid "Number of topics"
msgstr "Nombre de sujets"

#: templates/pybb/user.html:25
#: templates/pybb/user.html:27
msgid "Number of posts"
msgstr "Nombre de messages"

#: templates/pybb/user.html:29
#: templates/pybb/user.html:31
msgid "Date of registration"
msgstr "Date d'enregistrement"

#: templates/pybb/user.html:37
#: templates/pybb/user.html:39
msgid "Block"
msgstr "Bloquer"

#: templates/pybb/user.html:38
#: templates/pybb/user.html:40
msgid "Block and delete all messages"
msgstr "Bloquer et supprimer tous les messages"

#: templates/pybb/user.html:42
#: templates/pybb/user.html:44
msgid "Unblock"
msgstr "Débloquer"

#: templates/pybb/user_posts.html:12
#: templates/pybb/user_posts.html.py:16
#: templates/pybb/user_posts.html:13
#: templates/pybb/user_posts.html.py:18
msgid "All posts created by"
msgstr "Tous les sujets créés par"

#: templates/pybb/user_topics.html:12
#: templates/pybb/user_topics.html.py:16
#: templates/pybb/user_topics.html:13
#: templates/pybb/user_topics.html.py:18
msgid "All topics created by"
msgstr "Tous les sujets créés par"

Expand Down Expand Up @@ -805,7 +816,6 @@ msgid "Can't get profile for anonymous user"
msgstr "Impossible d'obtenir le profil d'un utilisateur anonyme"

#: views.py:144
#| msgid "Forum categories are not created"
msgid "Forum does not exist"
msgstr "Le forum n'existe pas"

Expand Down
45 changes: 24 additions & 21 deletions pybb/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,7 @@

from pybb import defaults, util, compat

if defaults.PYBB_USE_DJANGO_MAILER:
try:
from mailer import send_mass_mail
except ImportError:
from django.core.mail import send_mass_mail
else:
from django.core.mail import send_mass_mail
from pybb.compat import send_mass_html_mail


def notify_topic_subscribers(post):
Expand All @@ -28,14 +22,20 @@ def notify_topic_subscribers(post):
delete_url = reverse('pybb:delete_subscription', args=[post.topic.id])
current_site = Site.objects.get_current()
from_email = settings.DEFAULT_FROM_EMAIL
context = {
'post': post,
'post_url': 'http://%s%s' % (current_site, post.get_absolute_url()),
'topic_url': 'http://%s%s' % (current_site, post.topic.get_absolute_url()),
'delete_url_full': 'http://%s%s' % (current_site, delete_url),

subject = render_to_string('pybb/mail_templates/subscription_email_subject.html',
{'site': current_site,
'post': post})
# Email subject *must not* contain newlines
subject = ''.join(subject.splitlines())
#backward compat only. TODO Delete those vars in next major release
#and rename delete_url_full with delete_url for consistency
'site': current_site,
'delete_url': delete_url,
}
base_tpl = 'pybb/mail_templates/subscription_email_'

mails = tuple()
mails = []
for user in topic.subscribers.exclude(pk=post.user.pk):
try:
validate_email(user.email)
Expand All @@ -48,16 +48,19 @@ def notify_topic_subscribers(post):

lang = util.get_pybb_profile(user).language or settings.LANGUAGE_CODE
translation.activate(lang)

message = render_to_string('pybb/mail_templates/subscription_email_body.html',
{'site': current_site,
'post': post,
'delete_url': delete_url,
'user': user})
mails += ((subject, message, from_email, [user.email]),)
subject = render_to_string('%ssubject.html' % base_tpl, context)
# Email subject *must not* contain newlines
subject = ''.join(subject.splitlines())
context.update({
'user': user,
'subject': subject,
})
txt_message = render_to_string('%sbody.html' % base_tpl, context)
html_message = render_to_string('%sbody-html.html' % base_tpl, context)
mails.append((subject, txt_message, from_email, [user.email], html_message))

# Send mails
send_mass_mail(mails, fail_silently=True)
send_mass_html_mail(mails, fail_silently=True)

# Reactivate previous language
translation.activate(old_lang)
10 changes: 10 additions & 0 deletions pybb/templates/pybb/mail_templates/base-html.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% load i18n pybb_tags %}
{% pybb_get_profile user=post.user as profile %}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional //EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>{{ subject|default:'Email' }}</title>
</head>
<body style="margin:0px; padding:0px;">{% block content %}{% endblock %}</body>
</html>
1 change: 1 addition & 0 deletions pybb/templates/pybb/mail_templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% block content %}{% endblock %}
Loading