nemesifier
nemesifier

Reputation: 8529

Python/Django: sending emails in the background

Imagine a situation in which a user performs an action on a website and admins are notified. Imagine there are 20 admins to notify. By using normal methods for sending emails with Django the user will have to wait until all the emails are sent before being able to proceed.

How can I send all the emails in a separate process so the user doesn't have to wait? Is it possible?

Upvotes: 19

Views: 15189

Answers (3)

dani herrera
dani herrera

Reputation: 51665

If we are talking about to send only 20 mails time by time, a thread may be a possible solution. For expensive background tasks use Celery.

This is a sample using thread:

# This Python file uses the following encoding: utf-8

#threading
from threading import Thread

...

class afegeixThread(Thread):
    
    def __init__ (self,usuari, parameter=None):
        Thread.__init__(self)
        self.parameter = parameter
        ...
      
    def run(self):        
        errors = []
        try:
             if self.paramenter:
                   ....
        except Exception, e:                
             ...
...

n = afegeixThread( 'p1' )
n.start()

Upvotes: 5

Jesse
Jesse

Reputation: 373

Another option is django-mailer. It queues up mail in a database table and then you use a cron job to send them.

https://github.com/pinax/django-mailer

Upvotes: 7

andreaspelme
andreaspelme

Reputation: 3310

Use celery as a task queue and django-celery-email which is an Django e-mail backend that dispatches e-mail sending to a celery task.

Upvotes: 25

Related Questions