Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 351
nemesisdesign Avatar asked Oct 02 '11 11:10

nemesisdesign


People also ask

How do I send multiple emails in Django?

How to send multiple mass emails django. We need to create a Tuple of messages and send them using send mass mail. In this tutorial, we create a project which sends email using Django. We fill the data in the form and send it using Django Email.

How do I run a background task in Django?

In Django Background Task, all tasks are implemented as functions (or any other callable). There are two parts to using background tasks: creating the task functions and registering them with the scheduler. setup a cron task (or long running process) to execute the tasks.


3 Answers

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.

like image 81
andreaspelme Avatar answered Sep 19 '22 09:09

andreaspelme


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

like image 44
Jesse Avatar answered Sep 22 '22 09:09

Jesse


A thread may be a possible solution. I use threads intensively in my application for haevy tasks.

# 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()
like image 38
dani herrera Avatar answered Sep 20 '22 09:09

dani herrera