forked from django-nonrel/djangoappengine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail.py
85 lines (78 loc) · 3.09 KB
/
mail.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
83
84
85
from email.MIMEBase import MIMEBase
from django.core.mail.backends.base import BaseEmailBackend
from django.core.mail import EmailMultiAlternatives
from django.core.exceptions import ImproperlyConfigured
from google.appengine.api import mail as aeemail
from google.appengine.runtime import apiproxy_errors
def _send_deferred(message, fail_silently=False):
try:
message.send()
except (aeemail.Error, apiproxy_errors.Error):
if not fail_silently:
raise
class EmailBackend(BaseEmailBackend):
can_defer = False
def send_messages(self, email_messages):
num_sent = 0
for message in email_messages:
if self._send(message):
num_sent += 1
return num_sent
def _copy_message(self, message):
"""Create and return App Engine EmailMessage class from message."""
gmsg = aeemail.EmailMessage(sender=message.from_email,
to=message.to,
subject=message.subject,
body=message.body)
if message.extra_headers.get('Reply-To', None):
gmsg.reply_to = message.extra_headers['Reply-To']
if message.cc:
gmsg.cc = list(message.cc)
if message.bcc:
gmsg.bcc = list(message.bcc)
if message.attachments:
# Must be populated with (filename, filecontents) tuples
attachments = []
for attachment in message.attachments:
if isinstance(attachment, MIMEBase):
attachments.append((attachment.get_filename(),
attachment.get_payload(decode=True)))
else:
attachments.append((attachment[0], attachment[1]))
gmsg.attachments = attachments
# Look for HTML alternative content
if isinstance(message, EmailMultiAlternatives):
for content, mimetype in message.alternatives:
if mimetype == 'text/html':
gmsg.html = content
break
return gmsg
def _send(self, message):
try:
message = self._copy_message(message)
except (ValueError, aeemail.InvalidEmailError), err:
import logging
logging.warn(err)
if not self.fail_silently:
raise
return False
if self.can_defer:
self._defer_message(message)
return True
try:
message.send()
except (aeemail.Error, apiproxy_errors.Error):
if not self.fail_silently:
raise
return False
return True
def _defer_message(self, message):
from google.appengine.ext import deferred
from django.conf import settings
queue_name = getattr(settings, 'EMAIL_QUEUE_NAME', 'default')
deferred.defer(_send_deferred,
message,
fail_silently=self.fail_silently,
_queue=queue_name)
class AsyncEmailBackend(EmailBackend):
can_defer = True