-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
68 lines (55 loc) · 2.17 KB
/
main.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
import logging, email, urllib
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
class HandleEmail(InboundMailHandler):
def receive(self, message):
# Get Fields
sender = message.sender
to = message.to
date = message.date
subject = message.subject
original = str(message.original)
try :
cc = message.cc
except :
cc = ''
html_body = ''
for content_type, body in message.bodies('text/html'):
html_body = body.decode()
for plain in message.bodies('text/plain'):
plaintext_body = plain[1].decode()
# Log stuff, comment some out if its too much for you
logging.info('sender: ' + sender)
logging.info('to: ' + to)
logging.info('cc: ' + cc)
logging.info('date: ' + date)
logging.info('subject: ' + subject)
logging.info('html_body: ' + html_body)
logging.info('plaintext_body: ' + plaintext_body)
logging.info('original: ' + original)
# POST everything
url = 'http://where ever you want <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<'
form_fields = {
'sender': sender,
'to': to,
'cc': cc,
'date': date,
'subject': subject,
'html_body': html_body,
'plaintext_body': plaintext_body,
'original': original
}
form_fields = urllib.urlencode(form_fields)
result = urlfetch.fetch(url = url,
payload = form_fields,
method = urlfetch.POST,
headers = {'Content-Type': 'application/x-www-form-urlencoded'})
logging.info('POST to ' + url + ' returned: ' + str(result.status_code))
logging.info('Returned content: ' + result.content)
application = webapp.WSGIApplication([HandleEmail.mapping()], debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()