Projet

Général

Profil

0001-emails-use-global-title-in-From-header-33437.patch

Frédéric Péters, 27 mai 2019 13:31

Télécharger (3,75 ko)

Voir les différences:

Subject: [PATCH] emails: use global title in From header (#33437)

 tests/test_misc.py   | 20 ++++++++++++++++++++
 wcs/qommon/emails.py | 20 ++++++++++++--------
 2 files changed, 32 insertions(+), 8 deletions(-)
tests/test_misc.py
292 292
    assert not emails.emails['test']['msg'].is_multipart()
293 293
    assert 'Footer\nText' in emails.emails['test']['msg'].get_payload()
294 294

  
295
def test_email_from(emails):
296
    pub = create_temporary_pub()
297
    send_email('test', mail_body='Hello', email_rcpt='test@localhost', want_html=False)
298
    assert emails.count() == 1
299
    assert emails.emails['test']['from'] == 'noreply@entrouvert.com'
300

  
301
    pub.cfg['emails'] = {'from': 'foo@localhost'}
302
    send_email('test', mail_body='Hello', email_rcpt='test@localhost', want_html=False)
303
    assert emails.count() == 1
304
    assert emails.emails['test']['from'] == 'foo@localhost'
305
    assert emails.emails['test']['msg']['From'] == 'foo@localhost'
306

  
307
    if not pub.site_options.has_section('variables'):
308
        pub.site_options.add_section('variables')
309
    pub.site_options.set('variables', 'global_title', 'HELLO')
310
    send_email('test', mail_body='Hello', email_rcpt='test@localhost', want_html=False)
311
    assert emails.count() == 1
312
    assert emails.emails['test']['from'] == 'foo@localhost'
313
    assert emails.emails['test']['msg']['From'] == '"HELLO" <foo@localhost>'
314

  
295 315
@pytest.mark.skipif('docutils is None')
296 316
def test_email_signature_rst(emails):
297 317
    pub = create_temporary_pub()
wcs/qommon/emails.py
254 254
            msg['To'] = ', '.join(email_rcpt)
255 255
        else:
256 256
            msg['To'] = email_rcpt
257
    if email_from:
258
        msg['From'] = email_from
257

  
258
    if not email_from:
259
        email_from = emails_cfg.get('from', 'noreply@entrouvert.com')
260

  
261
    sitename = get_publisher().get_site_option('global_title', 'variables')
262
    if sitename:
263
        msg['From'] = '"%s" <%s>' % (sitename.replace('"', ' '), email_from)
259 264
    else:
260
        msg['From'] = emails_cfg.get('from', 'noreply@entrouvert.com')
265
        msg['From'] = email_from
266

  
261 267
    if emails_cfg.get('reply_to'):
262 268
        msg['Reply-To'] = emails_cfg.get('reply_to')
263 269
    if replyto:
......
300 306
        token.email_message = msg.as_string()
301 307
        token.email_type = str(email_type)
302 308
        token.store()
303
        msg_from = '%s-bounces+%s@%s' % (get_publisher().APP_NAME, token.id, server_name)
304
    else:
305
        msg_from = msg['From']
309
        email_from = '%s-bounces+%s@%s' % (get_publisher().APP_NAME, token.id, server_name)
306 310

  
307 311
    if not fire_and_forget:
308 312
        s = create_smtp_server(emails_cfg, smtp_timeout=smtp_timeout)
309 313
        try:
310
            s.sendmail(msg_from, rcpts, msg.as_string())
314
            s.sendmail(email_from, rcpts, msg.as_string())
311 315
        except smtplib.SMTPRecipientsRefused:
312 316
            get_logger().error('Failed to send mail to %r', rcpts)
313 317
        s.close()
314 318
    else:
315 319
        get_response().add_after_job('sending email',
316
                EmailToSend(msg_from, rcpts, msg.as_string()),
320
                EmailToSend(email_from, rcpts, msg.as_string()),
317 321
                fire_and_forget = True)
318 322

  
319 323
def create_smtp_server(emails_cfg, smtp_timeout=None):
320
-