1
0
mirror of https://github.com/django/django.git synced 2025-06-04 02:59:13 +00:00

Refs #36138 -- Cleaned up duplicate code in mail_admins()/mail_managers().

This commit is contained in:
Mike Edmunds 2025-02-14 10:31:44 -08:00 committed by Sarah Boyce
parent 62ad970c39
commit 90fc762948

View File

@ -119,19 +119,27 @@ def send_mass_mail(
return connection.send_messages(messages) return connection.send_messages(messages)
def mail_admins( def _send_server_message(
subject, message, fail_silently=False, connection=None, html_message=None *,
setting_name,
subject,
message,
html_message=None,
fail_silently=False,
connection=None,
): ):
"""Send a message to the admins, as defined by the ADMINS setting.""" recipients = getattr(settings, setting_name)
if not settings.ADMINS: if not recipients:
return return
if not all(isinstance(a, (list, tuple)) and len(a) == 2 for a in settings.ADMINS):
raise ValueError("The ADMINS setting must be a list of 2-tuples.") if not all(isinstance(a, (list, tuple)) and len(a) == 2 for a in recipients):
raise ValueError(f"The {setting_name} setting must be a list of 2-tuples.")
mail = EmailMultiAlternatives( mail = EmailMultiAlternatives(
"%s%s" % (settings.EMAIL_SUBJECT_PREFIX, subject), subject="%s%s" % (settings.EMAIL_SUBJECT_PREFIX, subject),
message, body=message,
settings.SERVER_EMAIL, from_email=settings.SERVER_EMAIL,
[a[1] for a in settings.ADMINS], to=[a[1] for a in recipients],
connection=connection, connection=connection,
) )
if html_message: if html_message:
@ -139,21 +147,29 @@ def mail_admins(
mail.send(fail_silently=fail_silently) mail.send(fail_silently=fail_silently)
def mail_admins(
subject, message, fail_silently=False, connection=None, html_message=None
):
"""Send a message to the admins, as defined by the ADMINS setting."""
_send_server_message(
setting_name="ADMINS",
subject=subject,
message=message,
html_message=html_message,
fail_silently=fail_silently,
connection=connection,
)
def mail_managers( def mail_managers(
subject, message, fail_silently=False, connection=None, html_message=None subject, message, fail_silently=False, connection=None, html_message=None
): ):
"""Send a message to the managers, as defined by the MANAGERS setting.""" """Send a message to the managers, as defined by the MANAGERS setting."""
if not settings.MANAGERS: _send_server_message(
return setting_name="MANAGERS",
if not all(isinstance(a, (list, tuple)) and len(a) == 2 for a in settings.MANAGERS): subject=subject,
raise ValueError("The MANAGERS setting must be a list of 2-tuples.") message=message,
mail = EmailMultiAlternatives( html_message=html_message,
"%s%s" % (settings.EMAIL_SUBJECT_PREFIX, subject), fail_silently=fail_silently,
message,
settings.SERVER_EMAIL,
[a[1] for a in settings.MANAGERS],
connection=connection, connection=connection,
) )
if html_message:
mail.attach_alternative(html_message, "text/html")
mail.send(fail_silently=fail_silently)