darragh (owner)

Revisions

gist: 211682 Download_button fork
public
Public Clone URL: git://gist.github.com/211682.git
Embed All Files: show embed
app.yaml #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
application: letsgateway
version: 1
runtime: python
api_version: 1
 
inbound_services:
- mail
 
handlers:
- url: /emails
  script: outgoing.py
  secure: always
- url: /_ah/mail/.+
  script: incoming.py
  login: admin
 
incoming.py #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from google.appengine.ext import webapp
from google.appengine.ext.webapp.mail_handlers import InboundMailHandler
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import urlfetch
import urllib
 
class MailHandler(InboundMailHandler):
  def receive(self, message):
    content_type, encoded = message.bodies(content_type='text/plain').next()
    body = encoded.decode()
    sender = message.sender
    subject = message.subject
    urlfetch.fetch("http://localhost:3000/emails/handle_incoming", urllib.urlencode({'body':body, 'sender':sender, 'subject':subject}), urlfetch.POST)
    
application = webapp.WSGIApplication([MailHandler.mapping()], debug=True)
 
def main():
  run_wsgi_app(application)
if __name__ == "__main__":
  main()
outgoing.py #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import mail
 
class OutgoingEmailHandler(webapp.RequestHandler):
  def post(self):
message = mail.EmailMessage(sender="someone@somewhere.com")
message.subject = self.request.get('subject')
message.to = self.request.get('to')
message.body = self.request.get('body')
message.send()
  
application = webapp.WSGIApplication([('/emails', OutgoingEmailHandler)], debug=True)
 
def main():
  run_wsgi_app(application)
if __name__ == "__main__":
  main()