-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinboundmail.py
74 lines (57 loc) · 2.07 KB
/
inboundmail.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
69
70
71
72
73
import logging
import webapp2
from models import AdminEmail
from google.appengine.api import mail
from google.appengine.ext.webapp.mail_handlers import InboundMailHandler
def forwardEmail():
message = mail.EmailMessage(
sender='busybici <[email protected]>',
subject='busybici update',
to=to,
body=body
)
message.send()
class LogSenderHandler(InboundMailHandler):
def receive(self, mail_message):
logging.info("Received a message from: " + mail_message.sender)
original_sender = 'Original sender: '+mail_message.sender+'\n'
logging.info('Subject: '+mail_message.subject)
forward_subject = 'new message from busybici user: '+mail_message.subject
logging.info('To: '+mail_message.to)
original_to = 'Original to: '+mail_message.to+'\n'
try:
logging.info('Cc: '+mail_message.cc)
original_cc = 'Original cc: '+mail_message.cc+'\n'
except:
original_cc=''
forward_body_contents = []
forward_body_contents.append(original_sender)
forward_body_contents.append(original_to)
forward_body_contents.append(original_cc)
forward_body_contents.append('\n')
forward_html_contents = []
forward_html_contents.append(original_sender)
forward_html_contents.append(original_to)
forward_html_contents.append(original_cc)
forward_html_contents.append('\n')
for content_type, body in mail_message.bodies('text/plain'):
decoded_body = body.decode()
logging.info(decoded_body)
forward_body_contents.append(decoded_body)
for content_type, body in mail_message.bodies('text/html'):
decoded_body = body.decode()
logging.info(decoded_body)
forward_html_contents.append(decoded_body)
forward_body = ''.join(forward_body_contents)
forward_html = ''.join(forward_html_contents)
admin_entity = AdminEmail.query().get()
forward_to = admin_entity.email
message = mail.EmailMessage(
sender='busybici carrier <[email protected]>',
subject=forward_subject,
to=forward_to,
body=forward_body,
html=forward_html,
)
message.send()
app = webapp2.WSGIApplication([LogSenderHandler.mapping()], debug=True)