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' 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 # Admin setup
ADMINS = (('admin', 'christian@merten-moser.de'),) 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} headers = {'Message-ID': message_id}
else: else:
headers = {} headers = {}
with mail.get_connection() as connection:
for recipient in set(recipients): # construct mails
email = EmailMessage(subject, content, sender, [recipient], mails = []
headers=headers, for recipient in set(recipients):
connection=connection, **kwargs) email = EmailMessage(subject, content, sender, [recipient],
if attachments is not None: headers=headers, **kwargs)
for attach in attachments: if attachments is not None:
email.attach_file(attach) for attach in attachments:
try: email.attach_file(attach)
email.send(fail_silently=True) mails.append(email)
except Exception as e: try:
print("Error when sending mail:", e) # connect to smtp server
failed = True connection = mail.get_connection()
else: # send all mails with one connection
succeeded = True 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\ 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