diff --git a/wagtail/admin/mail.py b/wagtail/admin/mail.py index c700c1f7d2..a23f5695c1 100644 --- a/wagtail/admin/mail.py +++ b/wagtail/admin/mail.py @@ -227,35 +227,37 @@ class EmailNotificationMixin: def send_emails(self, template_set, context, recipients, **kwargs): connection = get_connection() + sent_count = 0 + try: + with OpenedConnection(connection) as open_connection: + + # Send emails + for recipient in recipients: + try: - with OpenedConnection(connection) as open_connection: + # update context with this recipient + context["user"] = recipient - # Send emails - sent_count = 0 - for recipient in recipients: - try: + # Translate text to the recipient language settings + with override(recipient.wagtail_userprofile.get_preferred_language()): + # Get email subject and content + email_subject = render_to_string(template_set['subject'], context).strip() + email_content = render_to_string(template_set['text'], context).strip() - # update context with this recipient - context["user"] = recipient + kwargs = {} + if getattr(settings, 'WAGTAILADMIN_NOTIFICATION_USE_HTML', False): + kwargs['html_message'] = render_to_string(template_set['html'], context) - # Translate text to the recipient language settings - with override(recipient.wagtail_userprofile.get_preferred_language()): - # Get email subject and content - email_subject = render_to_string(template_set['subject'], context).strip() - email_content = render_to_string(template_set['text'], context).strip() - - kwargs = {} - if getattr(settings, 'WAGTAILADMIN_NOTIFICATION_USE_HTML', False): - kwargs['html_message'] = render_to_string(template_set['html'], context) - - # Send email - send_mail(email_subject, email_content, [recipient.email], connection=open_connection, **kwargs) - sent_count += 1 - except Exception: - logger.exception( - "Failed to send notification email '%s' to %s", - email_subject, recipient.email - ) + # Send email + send_mail(email_subject, email_content, [recipient.email], connection=open_connection, **kwargs) + sent_count += 1 + except Exception: + logger.exception( + "Failed to send notification email '%s' to %s", + email_subject, recipient.email + ) + except (TimeoutError, ConnectionError): + logger.exception("Mail connection error, notification sending skipped") return sent_count == len(recipients)