diff --git a/cotisations/utils.py b/cotisations/utils.py index 5da8eee0..c7ca6187 100644 --- a/cotisations/utils.py +++ b/cotisations/utils.py @@ -23,9 +23,7 @@ import os from django.template.loader import get_template from django.core.mail import EmailMessage -from django.utils.translation import ugettext_lazy as _ -from django.contrib import messages -from smtplib import SMTPException +from re2o.mail_utils import send_mail_object from .tex import create_pdf from preferences.models import AssoOption, GeneralOption, CotisationsOption, Mandate @@ -46,20 +44,6 @@ def find_payment_method(payment): return None -def send_mail(mail, request): - """Wrapper for Django's EmailMessage.send which handles errors""" - try: - mail.send() - except (SMTPException, ConnectionError) as e: - if request: - messages.error( - request, - _("Failed to send email: %(error)s.") % { - "error": e, - }, - ) - - def send_mail_invoice(invoice, request=None): """Creates the pdf of the invoice and sends it by email to the client""" purchases_info = [] @@ -108,7 +92,7 @@ def send_mail_invoice(invoice, request=None): attachments=[("invoice.pdf", pdf, "application/pdf")], ) - send_mail(mail, request) + send_mail_object(mail, request) def send_mail_voucher(invoice, request=None): @@ -145,4 +129,4 @@ def send_mail_voucher(invoice, request=None): attachments=[("voucher.pdf", pdf, "application/pdf")], ) - send_mail(mail, request) + send_mail_object(mail, request) diff --git a/re2o/mail_utils.py b/re2o/mail_utils.py index 86d446fb..72dbac81 100644 --- a/re2o/mail_utils.py +++ b/re2o/mail_utils.py @@ -43,3 +43,17 @@ def send_mail(request, *args, **kwargs): "error": e, }, ) + + +def send_mail_object(mail, request): + """Wrapper for Django's EmailMessage.send which handles errors""" + try: + mail.send() + except (SMTPException, ConnectionError) as e: + if request: + messages.error( + request, + _("Failed to send email: %(error)s.") % { + "error": e, + }, + )