-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail.py
56 lines (43 loc) · 1.47 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
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import datetime
def read_credentials_from_file(path):
with open(path, "r") as f:
lines = f.readlines()[0]
creds = dict()
for line in lines.split(","):
k, v = line.split(":")
creds[k] = v
return creds
def send_mail(tx, rx, subject="", message="", path=""):
fromaddr = tx["mail"]
toaddr = rx
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = subject
body = message
msg.attach(MIMEText(body, 'plain'))
attachment = open(path, "rb")
part = MIMEBase('application', 'octate-stream')
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment", filename=path)
msg.attach(part)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, tx["password"])
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
if __name__ == '__main__':
try:
creds = read_credentials_from_file("creds.txt")
send_mail(creds, "[email protected]", "topic here!", "content here!!!\nnew line etc.",
"test.pdf")
print("--------------- !!! Mairu sent !!! ----------------")
except Exception as e:
print("excepted!", e.args)