-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmail.py
179 lines (135 loc) · 5.38 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import email, smtplib, ssl
from kimai_util import get_email_credentials
from pathlib import Path
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import logging
from datetime import datetime
def send_mail(receiver:list, subject:str, plain:str, attachments=[], html=""):
try:
_send_mail(receiver, subject, plain, attachments, html)
except Exception as e:
logging.exception(e)
def _send_mail(receiver:list, subject:str, plain:str, attachments=[], html=""):
logging.info(f"Sending mail to {receiver}")
sender, passw, server, port = get_email_credentials()
# Create a multipart message and set headers
message = MIMEMultipart()
message["From"] = sender
if type(receiver) is str:
message["To"] = receiver
else:
message["To"] = ", ".join(receiver)
message["Subject"] = subject
# Add body to email
if plain:
message.attach(MIMEText(plain, "plain"))
if html:
message.attach(MIMEText(html, "html"))
# Add attachments
for fname in attachments:
fname = Path(fname)
if not fname.is_file():
logging.error(f"Error: Attempt to attach none existing file: {fname}")
continue
part = None
with open(str(fname), "rb") as infile:
# Add file as application/octet-stream
# Email client can usually download this automatically as attachment
part = MIMEBase("application", "octet-stream")
if fname.suffix == ".xlsx":
part = MIMEBase("application", 'vnd.ms-excel')
part.set_payload(infile.read())
# Encode file in ASCII characters to send by email
encoders.encode_base64(part)
# Add header as key/value pair to attachment part
part.add_header(
"Content-Disposition",
"attachment",
filename=fname.name,
)
# Add attachment to message
message.attach(part)
# convert to string and send message
msg = message.as_string()
# Log in to server using secure context and send email
context = ssl.create_default_context()
if port == 465:
with smtplib.SMTP_SSL(server, port, context=context) as server:
server.login(sender, passw)
server.sendmail(sender, receiver, msg)
elif port == 587:
with smtplib.SMTP(server, port) as server:
server.ehlo() # Can be omitted
server.starttls(context=context)
server.ehlo() # Can be omitted
server.login(sender, passw)
server.sendmail(sender, receiver, msg)
report = f"TO: {receiver}\nATT: {[a.name for a in attachments]}\n\n{plain}\n\n{html}"
mailf = Path(__file__).resolve().parent / "mails"
ofname = f"{datetime.now().replace(microsecond=0)} {receiver}"
with open(str(mailf / ofname), "w") as outf:
outf.write(report)
def get_absolute_path(filename:str) -> Path:
thisf = Path(__file__).resolve()
filesfold = thisf.parent / "files"
filep = filesfold / filename
return filep
def encapsulate_html_with_body(contents):
return f"""\
<html>
<body>
{contents}
</body>
</html>
"""
def html_header(size, text):
return f"<h{size}>{text}</h{size}>\n"
def html_para(text):
return f"<p>{text}</p>\n"
def html_link(text, dest):
return f"<a href=\"{dest}\">{text}</a>\n"
def make_onboarding_msg(first:str, last:str, type:str, user:str, password:str) -> str:
msg = f"""
Hallo {first} {last},
herzlich willkommen im Team von leap in time!
Die Arbeitszeiterfassung funktioniert bei uns digital über die Webseite:
https://worktime.leap-in-time.de
Um bezahlt zu werden, müssen Sie dort Ihre Arbeitszeiten korrekt angeben.
Eine Kurzanleitung wie das genau funktioniert und was dabei zu beachten ist finden Sie hier:
https://wiki.leap-in-time.de/en/public/onboarding/worktime
Ihre Login-Daten:
Username: {user}
Passwort: {password}
Aus Sicherheitsgründen sollten Sie das Passwort ändern, können es aber auch einfach so weiterverwenden.
Wenn Sie das Passwort vergessen haben, können Sie sich einen Wiederherstellungslink per E-Mail auf der Startseite zusenden lassen.
Ihre Einteilung ist in der Gruppe {type}.
Viel Spass!
#####################################################
Hello {first} {last},
Welcome to the leap in time team!
We use this website to record working hours digitally:
https://worktime.leap-in-time.de
In order to get paid you have to correctly submit your working hours there.
You will find short instructions on how this works and what you need to bear in mind here:
https://wiki.leap-in-time.de/en/public/onboarding/worktime
Your login data:
Username: {user}
Password: {password}
For security reasons you should change the password, but you can also continue to use it as it is.
If you forget the password, you can have a recovery link sent to you by email on the home page.
Your allocation is in the group {type}.
Have fun!
"""
return msg
if __name__ == "__main__":
subj = "Test Email"
receiver = "[email protected]"
plain = "Hello There!"
html = html_header(2, "Hello There!") \
+ html_para("This is a test email.") \
+ html_link("Go To KIMAI", "kimai.org")
html = encapsulate_html_with_body(html)
send_mail(receiver, subj, plain, html, ["./requirements.txt", "README.md"])