Login

email backend which use sendmail subprocess

Author:
pawnhearts
Posted:
January 13, 2010
Language:
Python
Version:
1.1
Score:
4 (after 4 ratings)

email backend which use sendmail subprocess

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""sendmail email backend class."""

import threading

from django.conf import settings
from django.core.mail.backends.base import BaseEmailBackend
from subprocess import Popen,PIPE

class EmailBackend(BaseEmailBackend):
    def __init__(self, fail_silently=False, **kwargs):
        super(EmailBackend, self).__init__(fail_silently=fail_silently)
        self._lock = threading.RLock()

    def open(self):
        return True

    def close(self):
        pass

    def send_messages(self, email_messages):
        """
        Sends one or more EmailMessage objects and returns the number of email
        messages sent.
        """
        if not email_messages:
            return
        self._lock.acquire()
        try:
            num_sent = 0
            for message in email_messages:
                sent = self._send(message)
                if sent:
                    num_sent += 1
        finally:
            self._lock.release()
        return num_sent

    def _send(self, email_message):
        """A helper method that does the actual sending."""
        if not email_message.recipients():
            return False
        try:
            ps = Popen(["sendmail"]+list(email_message.recipients()), \
                       stdin=PIPE)
            ps.stdin.write(email_message.message().as_string())
            ps.stdin.flush()
            ps.stdin.close()
            return not ps.wait()
        except:
            if not self.fail_silently:
                raise
            return False
        return True

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 3 months ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 1 week ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 4 weeks ago
  5. Help text hyperlinks by sa2812 11 months, 3 weeks ago

Comments

sridharvana (on October 26, 2010):

Hi..

I am getting the following error- [Errno 2] No such file or directory in the the following line... 43. ps = Popen(["sendmail"]+list(email_message.recipients()), \ 44. stdin=PIPE)

Any suggestions please

#

leopd (on August 4, 2011):

If your system has 'mail' instead of sendmail, this works at line 43 just inside the try:

        args = ['mail']+list(email_message.recipients())
        args += ['-s',email_message.subject]
        ps = Popen(args, stdin=PIPE)

#

apollo13 (on March 26, 2012):

Btw the threading.lock is just pointless for this backend -- I think this code was copied from the SmtpBackend which requires this lock.

#

blueyed (on January 11, 2014):

You might be interested in my pull request to get this merged into the django-sendmail-backend package, and/or the package in general.

#

khaledelansari (on February 3, 2016):

I copied the code to my project and I'm getting the following : 'Module' object is not callable btw I'm using django 1.9

#

Please login first before commenting.