From 042fd9b28a3f9dffc4dba3f2a8dd22f5b38a0f2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Fri, 23 Jun 2017 09:33:35 +0200 Subject: [PATCH] emails: force linebreaks in email footer (#17131) --- tests/test_misc.py | 18 ++++++++++++++++++ tests/utilities.py | 1 + wcs/qommon/emails.py | 8 +++++++- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/tests/test_misc.py b/tests/test_misc.py index 4019c2b07..a757f524a 100644 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -20,6 +20,7 @@ from wcs.scripts import Script from wcs.qommon import evalutils from wcs.qommon.http_request import HTTPRequest from wcs.qommon.backoffice.listing import pagination_links +from wcs.qommon.emails import email as send_email, docutils from utilities import get_app, create_temporary_pub, clean_temporary_pub @@ -278,3 +279,20 @@ def test_pagination(): '1', '…', '8', '9', '10', '11', '12', '13', '14', '…', '50', '(101-110/500)', 'Per page: ', '10', '20', '50', '100'] assert get_texts(pagination_links(100, 20, 500)) == [ '1', '…', '3', '4', '5', '6', '7', '8', '9', '…', '25', '(101-120/500)', 'Per page: ', '10', '20', '50', '100'] + +def test_email_signature_plain(emails): + pub = create_temporary_pub() + pub.cfg['emails'] = {'footer': 'Footer\nText'} + send_email('test', mail_body='Hello', email_rcpt='test@localhost', want_html=False) + assert not emails.emails['test']['msg'].is_multipart() + assert 'Footer\nText' in emails.emails['test']['msg'].get_payload() + +@pytest.mark.skipif('docutils is None') +def test_email_signature_rst(emails): + pub = create_temporary_pub() + pub.cfg['emails'] = {'footer': 'Footer\nText'} + send_email('test', mail_body='Hello', email_rcpt='test@localhost') + assert emails.emails['test']['msg'].get_payload()[0].get_content_type() == 'text/plain' + assert emails.emails['test']['msg'].get_payload()[1].get_content_type() == 'text/html' + assert 'Footer\nText' in emails.emails['test']['msg'].get_payload()[0].get_payload() + assert '>Footer' in emails.emails['test']['msg'].get_payload()[1].get_payload() diff --git a/tests/utilities.py b/tests/utilities.py index df04a77c7..51788936d 100644 --- a/tests/utilities.py +++ b/tests/utilities.py @@ -193,6 +193,7 @@ class EmailsMocking(object): 'from': msg_from, 'to': email.header.decode_header(msg['To'])[0][0], 'payload': payload, + 'msg': msg, } self.emails[subject]['email_rcpt'] = rcpts diff --git a/wcs/qommon/emails.py b/wcs/qommon/emails.py index 33eaea299..e53452657 100644 --- a/wcs/qommon/emails.py +++ b/wcs/qommon/emails.py @@ -113,7 +113,13 @@ def email(subject, mail_body, email_rcpt, replyto = None, bcc = None, if want_html: try: if footer: - rst_mail_body = mail_body + '\n\n--------\n\n' + footer + rst_footer = footer + if not rst_footer.startswith('|') and '\n' in rst_footer: + # unless the footer text is already formatted like a block + # of lines, add pipes to give it appropriate multilines + # formatting. + rst_footer = '\n'.join(['| ' + x for x in rst_footer.splitlines()]) + rst_mail_body = mail_body + '\n\n--------\n\n' + rst_footer else: rst_mail_body = mail_body htmlmail, pub = docutils.core.publish_programmatically(