From 2d1da210c25eac8b15eddda1053008d74461cdeb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Mon, 27 May 2019 13:08:15 +0200
Subject: [PATCH] emails: use global title in From header (#33437)

---
 tests/test_misc.py   | 20 ++++++++++++++++++++
 wcs/qommon/emails.py | 22 +++++++++++++---------
 2 files changed, 33 insertions(+), 9 deletions(-)

diff --git a/tests/test_misc.py b/tests/test_misc.py
index e13a662d2..f857149b9 100644
--- a/tests/test_misc.py
+++ b/tests/test_misc.py
@@ -292,6 +292,26 @@ def test_email_signature_plain(emails):
     assert not emails.emails['test']['msg'].is_multipart()
     assert 'Footer\nText' in emails.emails['test']['msg'].get_payload()
 
+def test_email_from(emails):
+    pub = create_temporary_pub()
+    send_email('test', mail_body='Hello', email_rcpt='test@localhost', want_html=False)
+    assert emails.count() == 1
+    assert emails.emails['test']['from'] == 'noreply@entrouvert.com'
+
+    pub.cfg['emails'] = {'from': 'foo@localhost'}
+    send_email('test', mail_body='Hello', email_rcpt='test@localhost', want_html=False)
+    assert emails.count() == 1
+    assert emails.emails['test']['from'] == 'foo@localhost'
+    assert emails.emails['test']['msg']['From'] == 'foo@localhost'
+
+    if not pub.site_options.has_section('variables'):
+        pub.site_options.add_section('variables')
+    pub.site_options.set('variables', 'global_title', 'HELLO')
+    send_email('test', mail_body='Hello', email_rcpt='test@localhost', want_html=False)
+    assert emails.count() == 1
+    assert emails.emails['test']['from'] == 'foo@localhost'
+    assert emails.emails['test']['msg']['From'] == '"HELLO" <foo@localhost>'
+
 @pytest.mark.skipif('docutils is None')
 def test_email_signature_rst(emails):
     pub = create_temporary_pub()
diff --git a/wcs/qommon/emails.py b/wcs/qommon/emails.py
index 223b6f092..482f9fbeb 100644
--- a/wcs/qommon/emails.py
+++ b/wcs/qommon/emails.py
@@ -254,10 +254,16 @@ def email(subject, mail_body, email_rcpt, replyto = None, bcc = None,
             msg['To'] = ', '.join(email_rcpt)
         else:
             msg['To'] = email_rcpt
-    if email_from:
-        msg['From'] = email_from
+
+    if not email_from:
+        email_from = emails_cfg.get('from', 'noreply@entrouvert.com')
+
+    sitename = get_publisher().get_site_option('global_title', 'variables')
+    if sitename:
+        msg['From'] = '"%s" <%s>' % (sitename.replace('"', ' '), email_from)
     else:
-        msg['From'] = emails_cfg.get('from', 'noreply@entrouvert.com')
+        msg['From'] = email_from
+
     if emails_cfg.get('reply_to'):
         msg['Reply-To'] = emails_cfg.get('reply_to')
     if replyto:
@@ -293,27 +299,25 @@ def email(subject, mail_body, email_rcpt, replyto = None, bcc = None,
         if get_request():
             server_name = get_request().get_server().split(':')[0]
         else:
-            server_name = msg['From'].split('@')[1]
+            server_name = email_from.split('@')[1]
         token = tokens.Token(7 * 86400)
         token.type = 'email-bounce'
         token.email_rcpts = [str(x) for x in rcpts]
         token.email_message = msg.as_string()
         token.email_type = str(email_type)
         token.store()
-        msg_from = '%s-bounces+%s@%s' % (get_publisher().APP_NAME, token.id, server_name)
-    else:
-        msg_from = msg['From']
+        email_from = '%s-bounces+%s@%s' % (get_publisher().APP_NAME, token.id, server_name)
 
     if not fire_and_forget:
         s = create_smtp_server(emails_cfg, smtp_timeout=smtp_timeout)
         try:
-            s.sendmail(msg_from, rcpts, msg.as_string())
+            s.sendmail(email_from, rcpts, msg.as_string())
         except smtplib.SMTPRecipientsRefused:
             get_logger().error('Failed to send mail to %r', rcpts)
         s.close()
     else:
         get_response().add_after_job('sending email',
-                EmailToSend(msg_from, rcpts, msg.as_string()),
+                EmailToSend(email_from, rcpts, msg.as_string()),
                 fire_and_forget = True)
 
 def create_smtp_server(emails_cfg, smtp_timeout=None):
-- 
2.20.1

