This repository was archived by the owner on Jun 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
124 lines (88 loc) · 3.12 KB
/
main.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
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
import smtplib
import ssl
import os
from flask import Flask, abort, request
import jinja2
import config
app = Flask(__name__)
TEMPLATES = os.path.abspath("./templates")
LOGO_CID = "logo"
LOGO = "logo.png"
def format_template(template, context):
return jinja2.Environment(
loader=jinja2.FileSystemLoader(TEMPLATES)
).get_template(template).render(context)
def format_email(template, params):
html_p = os.path.expanduser(os.path.abspath(os.path.join(TEMPLATES, template + ".html")))
text_p = os.path.expanduser(os.path.abspath(os.path.join(TEMPLATES, template + ".txt")))
if not html_p.startswith(TEMPLATES) or not text_p.startswith(TEMPLATES):
return None, 400
if not os.path.exists(html_p) or not os.path.exists(text_p):
return None, 404
html = format_template(template + ".html", params)
text = format_template(template + ".txt", params)
return html, text
def build_email(html, text, from_, to, subject):
"""
Multipart(mixed)
Multipart(related)
Multipart(alternative)
Text(plain)
Text(html)
Image
"""
mixed = MIMEMultipart("mixed")
mixed["Subject"] = subject
mixed["From"] = from_
mixed["To"] = to
mixed.preamble = "This is a multi-part message in MIME format."
related = MIMEMultipart("related")
alt = MIMEMultipart("alternative")
alt.attach(MIMEText(text, "plain"))
alt.attach(MIMEText(html, "html"))
related.attach(alt)
with open(LOGO, "rb") as f:
logo = MIMEImage(f.read())
logo.add_header("Content-ID", f"<{LOGO_CID}>")
related.attach(logo)
mixed.attach(related)
return mixed.as_string()
def send_email(mime, recipient):
if config.MODE == "SSL":
context = ssl.create_default_context()
with smtplib.SMTP_SSL(*config.SMTP, context=context) as server:
server.login(*config.LOGIN)
server.sendmail(config.EMAIL, recipient, mime)
return True
elif config.MODE == "STARTTLS":
with smtplib.SMTP(*config.SMTP) as server:
server.ehlo()
server.starttls()
server.ehlo()
server.login(*config.LOGIN)
server.sendmail(config.EMAIL, recipient, mime)
return True
else:
return False
@app.route("/")
def index():
return "", 200
@app.route("/send", methods=["POST"])
def send():
if not all(i in request.form for i in ("template", "to", "subject")):
return abort(400)
context = {k: request.form[k] for k in request.form if request.form[k] and k != "template"}
context["logo_cid"] = "cid:" + LOGO_CID
email = format_email(request.form["template"], context)
if email[0] is None:
return abort(email[1])
html, txt = email
email = build_email(email[0], email[1], config.FROM_NAME, context["to"], context["subject"])
if send_email(email, context["to"]):
return "", 200
return abort(500)
if __name__ == "__main__":
app.run(debug=True)