-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_email.py
60 lines (48 loc) · 1.68 KB
/
send_email.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
import mimetypes
import smtplib
from email.message import EmailMessage
import configparser
from pathlib import Path
def guess_type(file):
print("file: ", file)
ctype, encoding = mimetypes.guess_type(file)
print("with types: ", ctype)
if ctype is None or encoding is not None:
# No guess could be made, or the file is encoded (compressed), so
# use a generic bag-of-bits type.
ctype = "application/octet-stream"
maintype, subtype = ctype.split("/", 1)
return (maintype, subtype)
config = configparser.ConfigParser()
config.read("./secrets/config.toml")
smtp_host = str(config["server"]["host"])
smtp_port = int(config["server"]["port"])
username = str(config["credentials"]["user"])
password = str(config["credentials"]["pass"])
directory = "./certs"
smtp = smtplib.SMTP(smtp_host, smtp_port)
smtp.starttls() # for using port 587
smtp.login(username, password)
files = Path(directory).glob("*")
for file in files:
to_user = file.name.removesuffix(".png")
msg = EmailMessage()
msg["Subject"] = "Certyfikat uczestnictwa Integralia 2023"
msg["From"] = username
msg["To"] = to_user
html_content_path = Path("./text/text.html")
html_types = guess_type(html_content_path)
# Set email content
with html_content_path.open("rb") as textb:
msg.set_content(textb.read(), maintype=html_types[0], subtype=html_types[1])
print(to_user)
maintype, subtype = guess_type(file)
with file.open("rb") as fb:
msg.add_attachment(
fb.read(),
maintype=maintype,
subtype=subtype,
filename="Certyfikat Integralia 2023.png",
)
smtp.send_message(msg)
smtp.quit()