Created
August 30, 2010 19:39
-
-
Save ghinch/557918 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Copyright (c) 2010, Greg Hinch | |
All rights reserved. | |
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: | |
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. | |
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
This is an example of a custom Django email backend for use with Mail Engine. | |
Usage: | |
1) Deploy an instance of Mail Engine on App Engine | |
2) Configure your Django settings file with the following settings: | |
- MAIL_ENGINE_PRIVATE_KEY : The private key you configured in Mail Engine for authentication | |
- MAIL_ENGINE_URL : The domain where your Mail Engine is deployed. Will likely be something like "engineappname.appspot.com", unless you use a custom domain. | |
3) In the settings import below, change "MY_APP" to the name of your Django project | |
4) Place this file in your Django project and point to it as the EMAIL_BACKEND in your settings file. For example, if the location is "myproject/mail_engine/backend.py", then your settings would need to include "EMAIL_BACKEND = 'myproject.mail_engine.backend.EmailBackend'" | |
""" | |
import hashlib | |
import httplib | |
import urllib | |
import threading | |
from django.core.mail.backends.base import BaseEmailBackend | |
from MYAPP.settings import MAIL_ENGINE_PRIVATE_KEY, MAIL_ENGINE_URL | |
class EmailBackend (BaseEmailBackend): | |
def __init__(self, host=None, port=None, username=None, password=None, | |
use_tls=None, fail_silently=False, **kwargs): | |
super(EmailBackend, self).__init__(fail_silently=fail_silently) | |
self.connection = None | |
self._lock = threading.RLock() | |
def open(self): | |
if self.connection: | |
return False | |
try: | |
self.connection = httplib.HTTPConnection(MAIL_ENGINE_URL) | |
except: | |
if not self.fail_silently: | |
raise | |
def close(self): | |
try: | |
try: | |
self.connection.close() | |
except: | |
if self.fail_silently: | |
return | |
raise | |
finally: | |
self.connection = None | |
def send_messages(self, email_messages): | |
if not email_messages: | |
return | |
self._lock.acquire() | |
try: | |
new_conn_created = self.open() | |
if not self.connection: | |
return | |
num_sent = 0 | |
for message in email_messages: | |
sent = self._send(message) | |
if sent: | |
num_sent += 1 | |
if new_conn_created: | |
self.close() | |
finally: | |
self._lock.release() | |
return num_sent | |
def _send(self, email_message): | |
if not email_message.recipients(): | |
return False | |
try: | |
params = { | |
'sender' : str(email_message.sender), | |
'body' : str(email_message.body), | |
'recipients' : str(','.join(email_message.recipients())), | |
'subject' : str(email_message.subject) | |
} | |
token = hashlib.sha1(urllib.urlencode(params) + MAIL_ENGINE_PRIVATE_KEY).hexdigest() | |
headers = { | |
"Content-type": "application/x-www-form-urlencoded", | |
"Accept": "text/plain", | |
"Mail-Engine-Auth-Token" : token | |
} | |
self.connection.request('POST', '/post', urllib.urlencode(params), headers) | |
response = self.connection.getresponse() | |
except: | |
if not self.fail_silently: | |
raise | |
return False | |
return True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment