Skip to content

Commit

Permalink
SPSERV-21 Added functionality to show a specific name for the sender …
Browse files Browse the repository at this point in the history
…in emails
  • Loading branch information
Rosanne Zeiler authored and Kareeeeem committed Sep 13, 2017
1 parent b29b8c2 commit 82e0f94
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
18 changes: 13 additions & 5 deletions spynl/main/mail.py
@@ -1,7 +1,7 @@
"""Define functions to send emails."""

"""Define functions to send emails."""

import os
import re

from pyramid.renderers import render
from pyramid_mailer import get_mailer
Expand Down Expand Up @@ -43,7 +43,7 @@

def _sendmail(request, recipient, subject, plain_body, html_body=None,
sender=None, attachments=None, fail_silently=True, mailer=None,
reply_to=None):
reply_to=None, sender_name=None):
"""
Send a mail using the pyramid mailer. This function also makes sure
that if Spynl is not in a production environment, mail is sent to a dummy
Expand All @@ -56,6 +56,7 @@ def _sendmail(request, recipient, subject, plain_body, html_body=None,
:param string html_body: html content of mail
:param string sender: senders address (optional,
default: no_reply@<spynl.domain>)
:param string sender_name: sender name)
:param [Attachment] attachments: attachments (optional)
:param bool fail_silently: keep quiet when connection errors happen
:param pyramid_mailer.Mailer mailer: mailer object (optional)
Expand Down Expand Up @@ -90,6 +91,11 @@ def _sendmail(request, recipient, subject, plain_body, html_body=None,
extra_headers = {}
if reply_to:
extra_headers['Reply-To'] = reply_to
if sender_name:
# sanitize sender name to make sure we do not get bad headers or
# unverified email errors.
sender_name = re.sub('[\n<>@]', ' ', sender_name).strip()
extra_headers['From'] = '{} <{}>'.format(sender_name, sender)
message = Message(sender=sender, recipients=recipients, subject=subject,
body=plain_body, html=html_body,
extra_headers=extra_headers)
Expand Down Expand Up @@ -117,7 +123,8 @@ def _sendmail(request, recipient, subject, plain_body, html_body=None,
def send_template_email(request, recipient, template_string=None,
template_file=None, replacements=None,
subject='', fail_silently=True, mailer=None,
attachments=None, sender=None, reply_to=None):
attachments=None, sender=None, reply_to=None,
sender_name=None):
"""
Send email using a html template.
Expand Down Expand Up @@ -195,4 +202,5 @@ def send_template_email(request, recipient, template_string=None,

return _sendmail(request, recipient, subject, text_body, html_body,
fail_silently=fail_silently, mailer=mailer,
attachments=attachments, sender=sender, reply_to=reply_to)
attachments=attachments, sender=sender, reply_to=reply_to,
sender_name=sender_name)
12 changes: 12 additions & 0 deletions spynl/tests/test_mail.py
Expand Up @@ -79,6 +79,18 @@ def test_missing_recipient(dummy_request, mailer):
assert email.body == "Hey Nic! It's me, Nic."


def test_sender_name(dummy_request, mailer):
"""
test that the sender_name gets added and that any problematic characters are
removed.
"""
assert sendmail(dummy_request, 'nicolas@softwear', 'Nic Test',
"Hey Nic! It's me, Nic.", sender_name='<bla@bla.com\n bla>',
mailer=mailer)
email = mailer.outbox[0]
assert email.extra_headers == {'From': 'bla bla.com bla <info@spynl.com>'}


def test_custom_html_template_mail(dummy_request, mailer, template):
"""The custom template is correctly used."""
with open(template[0], 'w') as fob:
Expand Down

0 comments on commit 82e0f94

Please sign in to comment.