-
Notifications
You must be signed in to change notification settings - Fork 0
/
incoming_email.py
32 lines (27 loc) · 1.12 KB
/
incoming_email.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
import logging, email
from google.appengine.ext import webapp,db
from google.appengine.ext.webapp.mail_handlers import InboundMailHandler
from google.appengine.ext.webapp.util import run_wsgi_app
class IncomingEmail(db.Model):
subject = db.StringProperty()
sender = db.StringProperty()
to = db.StringProperty()
date = db.StringProperty()
body = db.TextProperty()
original = db.TextProperty()
class LogSenderHandler(InboundMailHandler):
def receive(self, mail_message):
email = IncomingEmail(
subject = mail_message.subject,
sender = mail_message.sender,
to = mail_message.to,
date = mail_message.date,
body = str([body.decode() for type, body in mail_message.bodies('text/plain') ]),
original = mail_message.original.as_string())
email.put()
logging.info("Received an email. " + str(mail_message.original))
application = webapp.WSGIApplication([LogSenderHandler.mapping()])
def main():
run_wsgi_app(application)
if __name__ == '__main__':
main()