summaryrefslogtreecommitdiffstats
path: root/roles/ask/files/backends.py
blob: 73d636849e8c4802c969ee36a37a7da01e7d9ec6 (plain)
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
from django.core.mail.backends.base import BaseEmailBackend

from .models import Email, PRIORITY, STATUS


class EmailBackend(BaseEmailBackend):

    def open(self):
        pass

    def close(self):
        pass

    def send_messages(self, email_messages):
        """
        Queue one or more EmailMessage objects and returns the number of
        email messages sent.
        """
        if not email_messages:
            return
        num_sent = 0

        for email in email_messages:
            num_sent += 1
            subject = email.subject
            from_email = email.from_email
            message = email.body

            # Check whether email has 'text/html' alternative
            alternatives = getattr(email, 'alternatives', ())
            for alternative in alternatives:
                if alternative[1] == 'text/html':
                    html_message = alternative[0]
                    break
            else:
                html_message = ''

            for recipient in email.to:
                Email.objects.create(from_email=from_email, to=recipient,
                    subject=subject, html_message=html_message,
                    message=message, status=STATUS.queued,
                    priority=PRIORITY.medium)