forked from cdhigh/KindleEar
-
Notifications
You must be signed in to change notification settings - Fork 1
/
handlemail.py
82 lines (72 loc) · 3.11 KB
/
handlemail.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
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
将发到[email protected]的邮件正文转成附件发往管理员的kindle邮箱。
"""
from email.Header import decode_header
from email.utils import parseaddr, collapse_rfc2231_value
from bs4 import BeautifulSoup
import webapp2
from google.appengine.ext.webapp.mail_handlers import InboundMailHandler
from main import KeUser, WhiteList, BaseHandler
def decode_subject(subject):
if subject[0:2] == '=?' and subject[-2:] == '?=':
subject = u''.join(unicode(s, c or 'us-ascii') for s, c in decode_header(subject))
else:
subject = unicode(collapse_rfc2231_value(subject))
return subject
class HandleMail(InboundMailHandler):
def receive(self, message):
sender = parseaddr(message.sender)[1]
if not WhiteList.all().filter('mail = ', '*').get() and \
not WhiteList.all().filter('mail = ', sender).get():
self.response.out.write("Spam mail!")
default_log.warn('Spam mail from : %s' % sender)
return
if hasattr(message, 'subject'):
subject = decode_subject(message.subject)
else:
subject = u"NoSubject"
admin = KeUser.all().filter('name = ', 'admin').get()
if not admin or not admin.kindle_email:
self.response.out.write('No admin account or no email configured!')
return
txt_bodies = message.bodies('text/plain')
html_bodies = message.bodies('text/html')
allBodies = [body.decode() for ctype, body in html_bodies]
if len(allBodies) == 0:
allBodies = [body.decode() for ctype, body in txt_bodies]
bodies = u''.join(allBodies)
if not bodies:
return
allBodies = [u"""<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>%s</title></head><body>%s</body></html>""" %(subject,bodies)]
soup = BeautifulSoup(allBodies[0], 'lxml')
h = soup.find('head')
if not h:
h = soup.new_tag('head')
soup.html.insert(0, h)
t = soup.find('title')
if not t:
t = soup.new_tag('title')
t.string = subject
soup.html.head.insert(0, t)
m = soup.find('meta', attrs={"http-equiv":"Content-Type"})
if not m:
m = soup.new_tag('meta', content="text/html; charset=utf-8")
m["http-equiv"] = "Content-Type"
else:
m['content'] = "text/html; charset=utf-8"
if len(allBodies) > 1:
for o in allBodies[1:]:
so = BeautifulSoup(o, 'lxml')
b = so.find('body')
if not b:
continue
for c in b.contents:
soup.body.append(c)
html = unicode(soup).encode("utf-8")
BaseHandler.SendToKindle('admin', admin.kindle_email, subject[:15], 'html',
html, admin.timezone, False)
self.response.out.write('Done')
appmail = webapp2.WSGIApplication([HandleMail.mapping()], debug=True)