Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Make the gmail.py script more reusable from elsewhere

  • Loading branch information...
commit 42df4e7a7fe09526f10eae582b751070fd387948 1 parent 3e626f0
Toby White authored
Showing with 37 additions and 25 deletions.
  1. +3 −0  README
  2. +32 −23 gmail.py
  3. +2 −2 mail.py
View
3  README
@@ -2,3 +2,6 @@
Emails sent as RFC-2822 documents (with an accompanying signature) are decoded & sent onwards using GAE's mail-sending capabilities.
+The From address needs to be authorized
+
+Reply-To won't work for people reading the email in gmail: See http://www.google.com/support/forum/p/gmail/thread?tid=74d00d5e2605242d&hl=en
View
55 gmail.py
@@ -8,43 +8,52 @@ class MessageSendingFailure(Exception):
class Signer(object):
- def __init__(self):
- try:
- self.SECRET_KEY = os.environ['GMAIL_SECRET_KEY']
- except KeyError:
+ def __init__(self, SECRET_KEY=None):
+ if not SECRET_KEY:
try:
- self.SECRET_KEY = open('/etc/envdir/GMAIL_SECRET_KEY').readline().rstrip()
- except OSError:
- raise EnvironmentError("GMAIL_SECRET_KEY is not set.")
+ SECRET_KEY = os.environ['GMAIL_SECRET_KEY']
+ except KeyError:
+ try:
+ SECRET_KEY = open('/etc/envdir/GMAIL_SECRET_KEY').readline().rstrip()
+ except OSError:
+ raise EnvironmentError("GMAIL_SECRET_KEY is not set.")
+ self.SECRET_KEY = SECRET_KEY
def generate_signature(self, msg):
return base64.encodestring(hmac.new(self.SECRET_KEY, msg, hashlib.sha1).digest()).strip()
class Connection(object):
- def __init__(self):
+ def __init__(self, EMAIL_APPENGINE_PROXY_URL=None):
import httplib2
self.h = httplib2.Http()
- try:
- self.EMAIL_APPENGINE_PROXY_URL = os.environ['GMAIL_PROXY_URL']
- except KeyError:
- try:
- self.EMAIL_APPENGINE_PROXY_URL = open('/etc/envdir/GMAIL_PROXY_URL').readline().rstrip()
- except OSError:
- raise EnvironmentError("GMAIL_PROXY_URL is not set.")
+ if not EMAIL_APPENGINE_PROXY_URL:
+ try:
+ EMAIL_APPENGINE_PROXY_URL = os.environ['GMAIL_PROXY_URL']
+ except KeyError:
+ try:
+ EMAIL_APPENGINE_PROXY_URL = open('/etc/envdir/GMAIL_PROXY_URL').readline().rstrip()
+ except OSError:
+ raise EnvironmentError("GMAIL_PROXY_URL is not set.")
+ self.EMAIL_APPENGINE_PROXY_URL = EMAIL_APPENGINE_PROXY_URL
def make_request(self, data):
return self.h.request(self.EMAIL_APPENGINE_PROXY_URL, "POST", body=data)
-def send_mail(msg):
- values = {'msg':msg.as_string(),
- 'signature':Signer().generate_signature(msg.as_string())}
- data = urllib.urlencode([(k, v.encode('utf-8')) for k, v in values.items()])
- r, c = Connection().make_request(data)
+class GmailProxy(object):
+ def __init__(self, SECRET_KEY=None, EMAIL_APPENGINE_PROXY_URL=None):
+ self.signer = Signer(SECRET_KEY)
+ self.connection = Connection(EMAIL_APPENGINE_PROXY_URL)
+
+ 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:
- raise MessageSendingFailure(c)
+ if r.status != 204:
+ raise MessageSendingFailure(c)
if __name__ == '__main__':
@@ -65,4 +74,4 @@ def send_mail(msg):
recipient = os.environ.get('RECIPIENT')
if recipient:
msg['To'] = recipient
- send_mail(msg)
+ GmailProxy().send_mail(msg)
View
4 mail.py
@@ -76,8 +76,8 @@ def send_message(msg):
raise BadMessageError("Unauthorized message sender '%s'" % sender)
def check_signature(msg, signature):
- os.environ['GMAIL_SECRET_KEY'] = open('GMAIL_SECRET_KEY').read().strip()
- return Signer().generate_signature(msg) == signature
+ GMAIL_SECRET_KEY = open('GMAIL_SECRET_KEY').read().strip()
+ return Signer(GMAIL_SECRET_KEY).generate_signature(msg) == signature
def parse_args(request):
msg = request.get('msg')
Please sign in to comment.
Something went wrong with that request. Please try again.