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. Login with email or username by zeeg 5 years, 8 months ago
  2. Email backend to BCC all emails by maikhoepfel 6 months ago
  3. Email or username authentication with masquerading by petrilli 4 years, 10 months ago
  4. Decorator to execute a method only once by atodorov-otb 4 months, 1 week ago
  5. Multipart Templated Email by stephen_mcd 4 years, 1 month 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.

#

(Forgotten your password?)