Python/Django:在后台发送电子邮件

时间:2022-11-15 13:13:56

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.

想象一下这样一种情况:一个用户在一个网站上执行一个操作并通知管理员。假设有20个管理员需要通知。通过使用正常的方法,用Django发送电子邮件,用户必须等到所有的电子邮件发送后才能继续。

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

我如何能在一个单独的过程中发送所有的邮件,这样用户就不用等待了?是可能的吗?

3 个解决方案

#1


22  

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.

使用芹菜作为任务队列和Django -celery-email,这是Django的电子邮件后端,用于向芹菜任务发送电子邮件。

#2


6  

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

另一个选择是django-mailer。它将邮件排队到数据库表中,然后使用cron作业发送它们。

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

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

#3


4  

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

一个线程可能是一个可能的解决方案。我在应用程序中对haevy任务使用了大量的线程。

# 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()

#1


22  

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.

使用芹菜作为任务队列和Django -celery-email,这是Django的电子邮件后端,用于向芹菜任务发送电子邮件。

#2


6  

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

另一个选择是django-mailer。它将邮件排队到数据库表中,然后使用cron作业发送它们。

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

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

#3


4  

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

一个线程可能是一个可能的解决方案。我在应用程序中对haevy任务使用了大量的线程。

# 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()