-
Notifications
You must be signed in to change notification settings - Fork 270
/
backends.py
82 lines (68 loc) · 2.88 KB
/
backends.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from collections import OrderedDict
from email.mime.base import MIMEBase
from django.core.files.base import ContentFile
from django.core.mail.backends.base import BaseEmailBackend
from .settings import get_default_priority
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.
"""
from .mail import create
from .models import STATUS, Email
from .utils import create_attachments
from .signals import email_queued
if not email_messages:
return
default_priority = get_default_priority()
num_sent = 0
emails = []
for email_message in email_messages:
subject = email_message.subject
from_email = email_message.from_email
headers = email_message.extra_headers
if email_message.reply_to:
reply_to_header = ', '.join(str(v) for v in email_message.reply_to)
headers.setdefault('Reply-To', reply_to_header)
message = email_message.body # The plaintext message is called body
html_body = '' # The default if no html body can be found
if hasattr(email_message, 'alternatives') and len(email_message.alternatives) > 0:
for alternative in email_message.alternatives:
if alternative[1] == 'text/html':
html_body = alternative[0]
attachment_files = {}
for attachment in email_message.attachments:
if isinstance(attachment, MIMEBase):
attachment_files[attachment.get_filename()] = {
'file': ContentFile(attachment.get_payload()),
'mimetype': attachment.get_content_type(),
'headers': OrderedDict(attachment.items()),
}
else:
attachment_files[attachment[0]] = ContentFile(attachment[1])
email = create(
sender=from_email,
recipients=email_message.to,
cc=email_message.cc,
bcc=email_message.bcc,
subject=subject,
message=message,
html_message=html_body,
headers=headers,
)
if attachment_files:
attachments = create_attachments(attachment_files)
email.attachments.add(*attachments)
emails.append(email)
if default_priority == 'now':
status = email.dispatch()
if status == STATUS.sent:
num_sent += 1
if default_priority != 'now':
email_queued.send(sender=Email, emails=emails)
return num_sent