emails: add backend to add headers (#50753)

This commit is contained in:
Frédéric Péters 2021-02-02 09:59:12 +01:00
parent d121f41a7c
commit 341c4d1e3a
3 changed files with 44 additions and 1 deletions

35
hobo/emails/backend.py Normal file
View File

@ -0,0 +1,35 @@
# hobo - portal to configure and deploy applications
# Copyright (C) 2015-2021 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from django.conf import settings
import django.core.mail.backends.smtp
class EmailBackend(django.core.mail.backends.smtp.EmailBackend):
def _send(self, email_message):
try:
url = settings.TEMPLATE_VARS['email_unsubscribe_info_url']
if url:
email_message.extra_headers['List-Unsubscribe'] = '<%s>' % url
except (KeyError, TypeError):
pass
try:
url = settings.TEMPLATE_VARS['email_abuse_report_url']
if url:
email_message.extra_headers['X-Report-Abuse'] = 'Please report abuse for this email here: %s' % url
except (KeyError, TypeError):
pass
return super()._send(email_message)

View File

@ -36,6 +36,11 @@ class EmailsForm(forms.Form):
help_text=_('Custom prefix for emails subject (defaults to global title)'))
email_signature = forms.CharField(label=_('Signature'), required=False,
widget=forms.Textarea)
email_unsubscribe_info_url = forms.CharField(
label=_('URL with informations about emails sent by the platform'), required=False,
help_text=_('It should contain details such as what emails are sent and how not to receive them.'))
email_abuse_report_url = forms.CharField(label=_('URL for email abuse reports'), required=False,
help_text=_('It should contain a form to submit email abuse reports'))
def __init__(self, *args, **kwargs):
super(EmailsForm, self).__init__(*args, **kwargs)

View File

@ -24,7 +24,10 @@ from .forms import EmailsForm
class HomeView(VariablesFormMixin, TemplateView):
template_name = 'hobo/emails_home.html'
variables = ['default_from_email', 'email_sender_name', 'global_email_prefix', 'email_signature']
variables = ['default_from_email', 'email_sender_name',
'global_email_prefix', 'email_signature',
'email_unsubscribe_info_url',
'email_abuse_report_url']
form_class = EmailsForm
success_message = _('Emails settings have been updated. '
'It will take a few seconds to be effective.')