fix using too many connections

v1-0-stable
Christian Merten 4 years ago
parent 0c44cd20c1
commit e9af13c9d8

@ -176,6 +176,13 @@ EMAIL_USE_TLS = True if deployed else False
EMAIL_BACKEND = 'djcelery_email.backends.CeleryEmailBackend'
# Celery Email Setup
CELERY_EMAIL_TASK_CONFIG = {
'rate_limit' : '1/m' # * CELERY_EMAIL_CHUNK_SIZE (default: 10)
}
# Admin setup
ADMINS = (('admin', 'christian@merten-moser.de'),)

@ -20,21 +20,27 @@ def send(subject, content, sender, recipients, message_id=None, reply_to=None,
headers = {'Message-ID': message_id}
else:
headers = {}
with mail.get_connection() as connection:
# construct mails
mails = []
for recipient in set(recipients):
email = EmailMessage(subject, content, sender, [recipient],
headers=headers,
connection=connection, **kwargs)
headers=headers, **kwargs)
if attachments is not None:
for attach in attachments:
email.attach_file(attach)
mails.append(email)
try:
email.send(fail_silently=True)
# connect to smtp server
connection = mail.get_connection()
# send all mails with one connection
connection.send_messages(mails)
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

Loading…
Cancel
Save