forked from jwlyn/anymail
-
Notifications
You must be signed in to change notification settings - Fork 3
/
web.py
150 lines (133 loc) · 4.15 KB
/
web.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import logging, json, datetime
from logging.config import fileConfig
import sqlite3,os
import mimetypes
import base64
from flask import abort
from flask import make_response
import mailparser
from flask import Flask, jsonify, redirect, url_for
from flask import request
from flask import render_template
from urllib.parse import quote
app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False # json显示中文
# fileConfig("logging.ini")
# logger = logging.getLogger()
DB = os.path.expanduser("~/mailbox/fake_mail.db")
TABLE = "fake_mail"
@app.route('/', methods=['GET'])
def index():
conn = sqlite3.connect(DB)
cur = conn.cursor()
sql = f"""
select id, email_title, email_from, email_to, dt, has_attach from {TABLE} order by dt desc limit 100
"""
cur.execute(sql)
val = cur.fetchall()
cur.close()
conn.close()
return render_template("index.html", mails=val)
@app.route('/detail/<int:id>/<type>', methods=['GET'])
def detail(id, type):
"""
:param id:
:param type:
:return:
"""
conn = sqlite3.connect(DB)
cur = conn.cursor()
sql = f"""
select id, dt, email_raw from {TABLE} where id={id}
"""
cur.execute(sql)
val = cur.fetchone()
cur.close()
conn.close()
raw_email = val[2]
mail = mailparser.parse_from_string(raw_email)
if type=='html':
mail_content = mail.text_html
else:
mail_content = mail.text_plain
from_ = mail.from_[0][1]
to_ = mail.to[0][1]
dt = val[1]
email_subject = mail.subject
return render_template("detail.html", **{"title":email_subject, "from":from_, "type":type, "to":to_, "dt":dt, "mail_content":mail_content,
"attachements":mail.attachments, "mid":id
})
@app.route('/delete/<int:id>', methods=['GET'])
def delete(id):
conn = sqlite3.connect(DB)
cur = conn.cursor()
sql = f"""
delete from {TABLE} where id = {id}
"""
cur.execute(sql)
cur.close()
conn.commit()
conn.close()
return redirect("/")
@app.route('/email/<path:email>', methods=['GET'])
def email(email):
"""
获得时间最近的一个email
:param email:
:param type:
:return:
"""
conn = sqlite3.connect(DB)
cur = conn.cursor()
sql = f"""
select id,dt,email_raw from {TABLE} where email_to=? order by dt desc limit 1;
"""
cur.execute(sql, [email])
val = cur.fetchone()
cur.close()
conn.close()
raw_email = val[2]
mail = mailparser.parse_from_string(raw_email)
result = {}
result['id'] = val[0]
result['from'] = mail.from_[0][1]
result['to_'] = mail.to[0][1]
result['title'] = mail.subject
result['dt'] = val[1]
result['html'] = mail.text_html
result['text'] = mail.text_plain
return jsonify(result)
@app.route('/attach/<int:email_id>/<file_name>', methods=['GET'])
def attach(email_id, file_name):
try:
conn = sqlite3.connect(DB)
cur = conn.cursor()
sql = f"""
select email_raw from {TABLE} where id={email_id}
"""
cur.execute(sql)
val = cur.fetchone()
cur.close()
conn.close()
raw_email = val[0]
mail = mailparser.parse_from_string(raw_email)
data = ""
charset = "utf-8"
for att in mail.attachments:
fnm =att['filename']
if fnm==file_name:
data = att['payload']
charset = "utf-8" if att['charset'] is None else att['charset']
data = base64.b64decode(data)
response = make_response(data)
mime_type = mimetypes.guess_type(file_name)[0]
response.headers['Content-Type'] = mime_type
response.headers['Content-Disposition'] = f'attachment; "filename*=UTF-8 {quote(file_name.encode("utf-8"))}'
return response
except Exception as err:
print(err)
abort(404)
def start_web(host, port):
app.run(host, port)
if __name__=="__main__":
app.run("0.0.0.0", "8888", debug=True)