Skip to content

Conversation

@MrYoda
Copy link

@MrYoda MrYoda commented Apr 13, 2016

Hi there!
Until recent times, I used Mandrill to send emails in Django projects. When I started migrating to Sparkpost I found there is no implemented way in python-sparkpost to send emails asynchronously using Celery. For example, I used djrillcelery pypi package for it when I used Mandrill, and it worked well.
I implemented this feature and there is my pull request. How you could use it:

Set up settings.py:

# New email backend
EMAIL_BACKEND = 'sparkpost.django.email_backend.SparkPostCeleryEmailBackend'

# Also you must include sparkpost in `INSTALLED_APPS`, it needs for celery tasks:
INSTALLED_APPS = (
    ...
    'sparkpost.django',
    ...
)

And now you can send email using usual way:

from django.core.mail import send_mail

r = send_mail('Subject', 'Hello from celery', 'sender@example.com', ['to1@example.com', 'to2@example.com', ])

r is Celery AsyncResult object returned by send_messages task
r.result is AsyncResult object for chord of send_message tasks
r.result.result is integer number of successful emails returned by send_summary task, it will be non-zero when all tasks will be completed and when r.result.successful() will be True

You can wait for final result, but in absolutely most cases I'm not waiting for results of email sending.

I installed my fork for one of my projects, and it works. I could not write tests for it, I don't know pytest.

@richleland
Copy link
Contributor

Thanks for the PR @MrYoda! Can you add tests and docs for this please?

@petersmoeller
Copy link

@MrYoda Great work!
Another solution is using django-celery-email - which works with any backend :)
https://pypi.python.org/pypi/django-celery-email/1.1.4

@richleland
Copy link
Contributor

Oh thanks for mentioning that @petersmoeller - that's a more maintainable solution IMO. Does that work for your use case @MrYoda?

@MrYoda
Copy link
Author

MrYoda commented Apr 20, 2016

Thank you @petersmoeller ! I had used django-celery-email before I started use djrill, but I didn't know about CELERY_EMAIL_BACKEND setting! :)
It works, and yes, @richleland, I think that's a more maintainable solution

@richleland
Copy link
Contributor

Awesome thanks all - I'll add some documentation to point people to that lib.

@richleland richleland closed this Apr 20, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants