From 21994c984ca255f5b1c9f57a1ba4bb7556701db9 Mon Sep 17 00:00:00 2001 From: erichhasl Date: Tue, 14 Mar 2017 20:07:15 +0100 Subject: [PATCH] use only one connection to send multiple mails --- jdav_web/mailer/mailutils.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/jdav_web/mailer/mailutils.py b/jdav_web/mailer/mailutils.py index cab4fa6..fe9df14 100644 --- a/jdav_web/mailer/mailutils.py +++ b/jdav_web/mailer/mailutils.py @@ -1,3 +1,4 @@ +from django.core import mail from django.core.mail import EmailMessage @@ -13,18 +14,20 @@ def send(subject, content, sender, recipients, reply_to=None, kwargs = {"reply_to": [reply_to]} else: kwargs = {} - for recipient in recipients: - email = EmailMessage(subject, content, sender, [recipient], **kwargs) - if attachments is not None: - for attach in attachments: - email.attach_file(attach) - try: - email.send() - except Exception as e: - print("Error when sending mail:", e) - failed = True - else: - succeeded = True + with mail.get_connection() as connection: + for recipient in recipients: + email = EmailMessage(subject, content, sender, [recipient], + connection=connection, **kwargs) + if attachments is not None: + for attach in attachments: + email.attach_file(attach) + try: + email.send() + except Exception as e: + print("Error when sending mail:", e) + failed = True + else: + succeeded = True return NOT_SENT if failed and not succeeded else SENT if not failed\ and succeeded else PARTLY_SENT