use only one connection to send multiple mails

v1-0-stable
Christian Merten 9 years ago
parent 171c747492
commit 21994c984c

@ -1,3 +1,4 @@
from django.core import mail
from django.core.mail import EmailMessage from django.core.mail import EmailMessage
@ -13,18 +14,20 @@ def send(subject, content, sender, recipients, reply_to=None,
kwargs = {"reply_to": [reply_to]} kwargs = {"reply_to": [reply_to]}
else: else:
kwargs = {} kwargs = {}
for recipient in recipients: with mail.get_connection() as connection:
email = EmailMessage(subject, content, sender, [recipient], **kwargs) for recipient in recipients:
if attachments is not None: email = EmailMessage(subject, content, sender, [recipient],
for attach in attachments: connection=connection, **kwargs)
email.attach_file(attach) if attachments is not None:
try: for attach in attachments:
email.send() email.attach_file(attach)
except Exception as e: try:
print("Error when sending mail:", e) email.send()
failed = True except Exception as e:
else: print("Error when sending mail:", e)
succeeded = True failed = True
else:
succeeded = True
return NOT_SENT if failed and not succeeded else SENT if not failed\ return NOT_SENT if failed and not succeeded else SENT if not failed\
and succeeded else PARTLY_SENT and succeeded else PARTLY_SENT

Loading…
Cancel
Save