emails: include List-Unsubscribe and X-Report-Abuse headers (#56298) #848

Merged
fpeters merged 1 commits from wip/56298-emails-header-urls into main 2023-11-24 09:13:58 +01:00
2 changed files with 26 additions and 0 deletions

View File

@ -380,3 +380,19 @@ bye,
html = emails.emails['test']['msg'].get_payload()[1].get_payload(decode=True)
assert text.count(b'\n ?????????\n') == 1
assert html.count(b'<dd>?????????</dd>') == 1
def test_email_report_headers(emails):
pub = create_temporary_pub()
pub.load_site_options()
if not pub.site_options.has_section('variables'):
pub.site_options.add_section('variables')
pub.site_options.set('variables', 'email_unsubscribe_info_url', 'http://unsub-url/')
pub.site_options.set('variables', 'email_abuse_report_url', 'http:/abuse-url/')
send_email('test', mail_body='Hello', email_rcpt='test@localhost', want_html=False)
assert emails.count() == 1
assert 'List-Unsubscribe: <http://unsub-url/>' in str(emails.emails['test']['msg'])
assert 'X-Report-Abuse: Please report abuse for this email here: http:/abuse-url/' in str(
emails.emails['test']['msg']
)

View File

@ -339,6 +339,16 @@ def email(
if not to_emails and not bcc_emails:
return
extra_headers = extra_headers or {}
for var in ('email_unsubscribe_info_url', 'portal_url'):
unsub_url = get_publisher().get_site_option(var, 'variables')
if unsub_url:
extra_headers['List-Unsubscribe'] = f'<{unsub_url}>'
break
abuse_url = get_publisher().get_site_option('email_abuse_report_url', 'variables')
if abuse_url:
extra_headers['X-Report-Abuse'] = f'Please report abuse for this email here: {abuse_url}'
email_msg_kwargs = {
'subject': subject,
'to': to_emails,