Permalink
Browse files

Add the Django example backend.

  • Loading branch information...
1 parent 42df4e7 commit e0de5ffa1cc4c12cb32cc2ce1a0cfb632e23770a Toby White committed Jul 7, 2010
Showing with 23 additions and 2 deletions.
  1. +20 −0 django_gmail_backend.py
  2. +3 −2 gmail.py
View
@@ -0,0 +1,20 @@
+from django.conf import settings
+from django.core.mail.backends.base import BaseEmailBackend
+
+from gmail import GmailProxy, MessageSendingFailure
+
+
+class GmailBackend(BaseEmailBackend):
+ def __init__(self, fail_silently=False):
+ self.gmail_proxy = GmailProxy(settings.SECRET_KEY, settings.EMAIL_APPENGINE_PROXY_URL, fail_silently)
+ super(GmailBackend, self).__init__(fail_silently)
+
+ def send_messages(self, messages):
+ n = 0
+ for message in messages:
+ try:
+ self.gmail_proxy.send_mail(message.message())
+ n += 1
+ except MessageSendingFailure:
+ pass
+ return n
View
@@ -42,17 +42,18 @@ def make_request(self, data):
class GmailProxy(object):
- def __init__(self, SECRET_KEY=None, EMAIL_APPENGINE_PROXY_URL=None):
+ def __init__(self, SECRET_KEY=None, EMAIL_APPENGINE_PROXY_URL=None, fail_silently=False):
self.signer = Signer(SECRET_KEY)
self.connection = Connection(EMAIL_APPENGINE_PROXY_URL)
+ self.fail_silently = fail_silently
def send_mail(self, msg):
values = {'msg':msg.as_string(),
'signature':self.signer.generate_signature(msg.as_string())}
data = urllib.urlencode([(k, v.encode('utf-8')) for k, v in values.items()])
r, c = self.connection.make_request(data)
- if r.status != 204:
+ if r.status != 204 and not self.fail_silently:
raise MessageSendingFailure(c)

0 comments on commit e0de5ff

Please sign in to comment.