pmclanahan / django-celery-email

A Django email backend that uses a celery task for sending the email.
BSD 3-Clause "New" or "Revised" License
477 stars 110 forks source link

Celery task fails with 'too many concurrent connections' #85

Open erichhasl opened 2 years ago

erichhasl commented 2 years ago

When sending a lot of mails with django-celery-email as backend, I often get an error in my celery logs, that there were too many concurrent connections.

I have set EMAIL_BACKEND accordingly:

EMAIL_BACKEND = 'djcelery_email.backends.CeleryEmailBackend'

And I am sending emails like so:

with mail.get_connection() as connection:
    for recipient in set(recipients):
        email = EmailMessage(subject, content, sender, [recipient],
                             headers=headers,
                             connection=connection, **kwargs)
        if attachments is not None:
            for attach in attachments:
                email.attach_file(attach)
        try:
            email.send(fail_silently=True)
        except Exception as e:
            print("Error when sending mail:", e)
            failed = True
        else:
            succeeded = True

I was expecting that this opens one connection and sends all emails using this one connection. Is this a wrong assumption? How can I force the mail backend to only use one single connection?