Skip to content
This repository
Newer
Older
100644 47 lines (39 sloc) 1.516 kb
bcf4ea93 » tghw
2012-11-07 Add gitattributes
1 import logging
2 import settings
3 from textwrap import dedent
4 from google.appengine.ext import webapp
5 from google.appengine.api import mail
6 from google.appengine.ext.webapp.mail_handlers import InboundMailHandler
7
8 class ReceiveEmail(InboundMailHandler):
9 def receive(self,message):
10 headers = dedent("""\
11 From: %s
12 To: %s
13 Subject: %s
14 """ % (
15 message.sender,
16 message.to,
17 message.subject,
18 ))
19
20 log_msg = headers + 'Body:\n'
21 for content_type, body in message.bodies():
22 log_msg += '%s\n%s\n-------------------------------\n' % (content_type, body.decode())
23
24 if settings.log_all_incoming:
25 logging.info(log_msg)
26
27 email_subject = '%s (from %s)' % (message.subject, message.sender)
28
29 email_body = headers
30 for content_type, body in message.bodies('text/plain'):
31 email_body += body.decode()
32
33 email_html = '<p>' + headers.replace('\n', '<br />') + '</p>'
34 for content_type, body in message.bodies('text/html'):
35 email_html += body.decode()
36
37 mail.send_mail(
38 sender = settings.incoming_sender_email,
39 reply_to = message.sender,
40 to = settings.forward_mail_to,
41 subject = email_subject,
42 body = email_body,
43 html = email_html,
44 )
45 return 'OK'
46
47 application = webapp.WSGIApplication([ReceiveEmail.mapping()], debug=True)
Something went wrong with that request. Please try again.