django-post_office: SMTPServerDisconnected error with send_mail and Celery
Hello, i am facing issue when i want to send_mail like this:
@task(name='send_registration_mail')
def send_registration_mail():
# some stuff
mail.send(
subject=template.subject,
message=message,
html_message=rendered_content,
priority='now'
**kwargs
)
This code mostly ends failed with SMTPServerDisconnected in log ‘please run connect() first’. When i manualy requeue emails via admin and run send_queued_mail, emails are send.
dependencies:
django-post-office==3.0.3
Django==1.10.7
celery = 4.0.2
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 7
- Comments: 34 (4 by maintainers)
Commits related to this issue
- Fixes: #218 SMTPServerDisconnected error Reintroduce abandoned disconnect_after_delivery parameter for Email.dispatch. The disconnect_after_delivery parameter got deleted in https://github.com/ui/dj... — committed to weimens/django-post_office by weimens 4 years ago
- Fixes: #218 SMTPServerDisconnected error Reintroduce abandoned disconnect_after_delivery parameter for Email.dispatch. The disconnect_after_delivery parameter got deleted in https://github.com/ui/dja... — committed to weimens/django-post_office by weimens 4 years ago
- Fixes: #218 SMTPServerDisconnected error Reintroduce abandoned disconnect_after_delivery parameter for Email.dispatch. The disconnect_after_delivery parameter got deleted in https://github.com/ui/dja... — committed to weimens/django-post_office by weimens 4 years ago
- Fixes: #218 SMTPServerDisconnected error Reintroduce abandoned disconnect_after_delivery parameter for Email.dispatch. The disconnect_after_delivery parameter got deleted in https://github.com/ui/dja... — committed to weimens/django-post_office by weimens 4 years ago
Try just up Python version
Like @cberzan wrote, it’s because of connection cache and smtp timeouts. I have some dirty hack that fixes this issue for me. I’m using it with
django.core.mail.backends.smtp.EmailBackendProper solution is IMO add option to disable connection caching when using long running threads.
Seems that the Email.dispatch() method used to close the connection, but that changed in commit 03d1a37 and now the connection times out.
I changed it later, works on production without problems:
Don’t judge me 😃
I will try to find some time to make pull request with disabling connection caching based on settings. It will solve all of the problems.
Don’t use priority
NOW, especially not together with Celery. It thwards the main reason for using post_office, namely its asynchronously. With Celery, emails are sent immediately after they have been queued, but by another task.How about just close the connection after sending email with priority NOW ?
As in https://github.com/haiwen/django-post_office/commit/2312cf240363721f737b5ac8eb86ab8cb255938f
@VerosK we use
EMAIL_POST_OFFICE_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'and Microsoft Office.Current set up is using
send_mailwithout celery task and created scheduled Celery task every minute like this:with following settings:
we also have following celery task to send failed emails every 2 minutes:
I also facing to issue with
'DEFAULT_PRIORITY': 'now'some emails ends up with statusNone. Reason was probably that when you don’t have settingEMAIL_TIMEOUT, email is created but it takes to long and wsgi kill process and you end with email with statusNone.