-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail.py
157 lines (132 loc) · 5.31 KB
/
mail.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
151
152
153
154
155
156
157
from email.mime.multipart import MIMEMultipart
import smtplib, json
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email import encoders
from jinja2 import Environment, select_autoescape, FileSystemLoader
from datetime import date
from qr import generateQRCode
from user import User
def current_semester() -> str:
today = date.today()
msg = str(today.year)
if today.month < 6:
msg += "S"
else:
msg += "W"
return msg
class MailSender:
def __init__(self, config_file: str) -> None:
with open(config_file) as mail_data:
self.config = json.load(mail_data)
self.env = Environment(
loader=FileSystemLoader("."),
autoescape=select_autoescape(["html", "xml"]),
)
def send_mail(self, msg: MIMEMultipart, email: str):
msg.add_header("reply-to", "[email protected]")
with smtplib.SMTP(self.config["domain"], self.config["port"]) as server:
server.starttls()
server.ehlo()
server.login(self.config["username"], self.config["password"])
server.sendmail(self.config["username"], [email], msg.as_string())
print("Successfully sent email.")
def send_plain_email(self, subject: str, message: str, email: str):
msg = MIMEMultipart()
msg.attach(MIMEText(message, "plain"))
msg["Subject"] = subject
msg["From"] = self.config["username"]
msg["To"] = email
self.send_mail(msg, email)
def send_reminder_email(self, user: User, amount: int, days_not_payed: int) -> None:
templates_config = self.config["reminder_template"]
template = None
for key in sorted(templates_config.keys(), reverse=True):
if int(key) <= days_not_payed:
template = templates_config[key]
break
if template is None:
print(
"Did not send email for",
user.given_name,
user.family_name,
"because",
days_not_payed,
"days did not match a template.",
)
return
template = self.env.get_template(template)
signature = self.env.get_template(self.config["signature_template"])
sig_body = signature.render()
body = template.render(
user=user, amount=amount, days_not_payed=days_not_payed, signature=sig_body
)
msg = MIMEMultipart()
msg.attach(MIMEText(body, "html"))
msg[
"Subject"
] = "Beitragserinnerung TU Space Team - Membershipfee reminder TU Space Team"
msg["From"] = self.config["username"]
msg["To"] = user.recovery_email
image = MIMEImage(
generateQRCode(
recipient=self.config["banking_account_name"],
iban=self.config["iban"],
amount=amount,
currency=self.config["currency"],
purpose=f"MGB {user.given_name} {user.family_name}",
)
)
image.add_header("Content-Disposition", "attachment", filename="qrcode.png")
msg.attach(image)
self.send_mail(msg, user.recovery_email)
def send_welcome_mail(self, user: User) -> None:
template = self.env.get_template(self.config["welcome_template"])
msg = MIMEMultipart()
body = template.render(user=user, current_semester=current_semester())
msg.attach(MIMEText(body, "html"))
# binary_pdf = open(self.config["pdf"], "rb")
# payload = MIMEBase("application", "octate-stream", Name=self.config["pdf"])
# payload.set_payload((binary_pdf).read())
# TODO: Doesn't work in all email clients
# enconding the binary into base64
# encoders.encode_base64(payload)
# add header with pdf name
# payload.add_header(
# "Content-Decomposition", "attachment", filename=self.config["pdf"]
# )
# msg.attach(payload)
msg["Subject"] = "Willkommen im TU Space Team - Welcome to TU Space Team"
msg["From"] = self.config["username"]
msg["To"] = user.recovery_email
self.send_mail(msg, user.recovery_email)
if __name__ == "__main__":
# sending test email
mail_handler = MailSender("mail.json")
user = User(
email="[email protected]",
recovery_email="[email protected]",
given_name="Paul",
family_name="Höller",
password="",
)
user2 = User(
email="[email protected]",
recovery_email="[email protected]",
given_name="Alicia",
family_name="Wollendorfer",
password="",
)
user3 = User(
email="[email protected]",
recovery_email="[email protected]",
given_name="Patrick",
family_name="Kappl",
password="",
)
mail_handler.send_welcome_mail(user=user)
# mail_handler.send_reminder_email(user=user2, amount=25, days_not_payed=31)
# mail_handler.send_reminder_email(user=user2, amount=25, days_not_payed=61)
# mail_handler.send_reminder_email(user=user2, amount=25, days_not_payed=91)
# mail_handler.send_plain_email("Test Email", "Test email to see if smtp is correctly set up", "[email protected]")