notifications: move email recipients from To to Bcc (#81860)
gitea/chrono/pipeline/head This commit looks good Details

This commit is contained in:
Yann Weber 2024-01-17 10:40:03 +01:00 committed by Yann Weber
parent 917c918422
commit 3bfa450f97
2 changed files with 29 additions and 5 deletions

View File

@ -18,7 +18,7 @@ import copy
from urllib.parse import urljoin
from django.conf import settings
from django.core.mail import send_mail
from django.core.mail import EmailMultiAlternatives
from django.core.management.base import BaseCommand
from django.db.transaction import atomic
from django.template.loader import render_to_string
@ -72,4 +72,12 @@ class Command(BaseCommand):
with atomic():
setattr(event, status + '_notification_timestamp', timestamp)
event.save()
send_mail(subject, body, settings.DEFAULT_FROM_EMAIL, recipients, html_message=html_body)
mail_msg = EmailMultiAlternatives(
subject=subject,
body=body,
from_email=settings.DEFAULT_FROM_EMAIL,
to=[settings.DEFAULT_FROM_EMAIL],
bcc=recipients,
)
mail_msg.attach_alternative(html_body, 'text/html')
mail_msg.send()

View File

@ -1829,7 +1829,11 @@ def test_agenda_notifications_role_email(mocked_role, emails_to_members, emails,
call_command('send_email_notifications')
assert len(mailoutbox) == expected_email_count
if mailoutbox:
assert mailoutbox[0].recipients() == expected_recipients
assert mailoutbox[0].bcc == expected_recipients
for addr in expected_recipients:
assert addr not in mailoutbox[0].to
assert addr in mailoutbox[0].recipients()
assert len(mailoutbox[0].recipients()) == len(expected_recipients) + 1
assert mailoutbox[0].subject == 'Alert: event "Hop" is almost full (90%)'
assert 'manage/agendas/%s/events/%s/' % (agenda.id, event.id) in mailoutbox[0].body
assert 'manage/agendas/%s/events/%s/' % (agenda.id, event.id) in mailoutbox[0].alternatives[0][0]
@ -1855,7 +1859,11 @@ def test_agenda_notifications_email_list(mailoutbox):
call_command('send_email_notifications')
assert len(mailoutbox) == 1
assert mailoutbox[0].recipients() == recipients
assert mailoutbox[0].bcc == recipients
for addr in recipients:
assert addr not in mailoutbox[0].to
assert addr in mailoutbox[0].recipients()
assert len(mailoutbox[0].recipients()) == len(recipients) + 1
assert mailoutbox[0].subject == 'Alert: event "Hop" is full'
assert (
'view it here: https://example.com/manage/agendas/%s/events/%s/'
@ -1866,6 +1874,10 @@ def test_agenda_notifications_email_list(mailoutbox):
in mailoutbox[0].body
)
html, mime = mailoutbox[0].alternatives[0]
assert mime == 'text/html'
assert '<a href="https://example.com/manage/agendas/%s/events/%s/">' % (agenda.pk, event.pk) in html
# no new email on subsequent run
call_command('send_email_notifications')
assert len(mailoutbox) == 1
@ -1885,7 +1897,11 @@ def test_agenda_notifications_cancelled(mailoutbox):
call_command('send_email_notifications')
assert len(mailoutbox) == 1
assert mailoutbox[0].recipients() == recipients
assert mailoutbox[0].bcc == recipients
for addr in recipients:
assert addr not in mailoutbox[0].to
assert addr in mailoutbox[0].recipients()
assert len(mailoutbox[0].recipients()) == len(recipients) + 1
assert mailoutbox[0].subject == 'Alert: event "Hop" is cancelled'
# no new email on subsequent run