django asynchronous send mail

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
from django.core.mail import send_mail as core_send_mail
from django.core.mail import EmailMultiAlternatives
import threading

class EmailThread(threading.Thread):
    def __init__(self, subject, body, from_email, recipient_list, fail_silently, html):
        self.subject = subject
        self.body = body
        self.recipient_list = recipient_list
        self.from_email = from_email
        self.fail_silently = fail_silently
        self.html = html
        threading.Thread.__init__(self)

    def run (self):
        msg = EmailMultiAlternatives(self.subject, self.body, self.from_email, self.recipient_list)
        if self.html:
            msg.attach_alternative(self.html, "text/html")
        msg.send(self.fail_silently)

def send_mail(subject, body, from_email, recipient_list, fail_silently=False, html=None, *args, **kwargs):
    EmailThread(subject, body, from_email, recipient_list, fail_silently, html).start()

More like this

  1. Email queue in DB by fish2000 3 years, 1 month ago
  2. Better debugging mail server by yourcelf 2 years, 3 months ago
  3. Execute a signal once by johnnoone 4 years ago
  4. send_html_mail by amitu 4 years, 7 months ago
  5. send_mail wrapper with DEBUG email trapping by udfalkso 3 years, 3 months ago

Comments

pigletto (on April 13, 2010):

Using threads might cause side effects, see comments at this post: http://iraniweb.com/blog/?p=95

For asynchronous sending of email messages it might be better to use django-mailer.

#

(Forgotten your password?)