-
Notifications
You must be signed in to change notification settings - Fork 2
/
send_mails.py
78 lines (65 loc) · 2.24 KB
/
send_mails.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
# Built-In Libraries/Modules/Packages
import smtplib
import ssl
# Third Party Libraries/Modules/Packages
import pandas as pd
from email.mime.text import MIMEText
from email.utils import formataddr
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
def read_template(filename) -> str:
"""
Reads a file completely and returns it as a string.
"""
filehandle = open(filename, 'r', encoding='utf-8')
content = filehandle.read()
filehandle.close()
return content
def file_object(file_name):
try:
with open(file_name, "rb") as attachment:
part = MIMEBase("application", "octet-stream")
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header(
"Content-Disposition",
f"attachment; filename= {file_name}",
)
return part
except Exception as e:
print(f'Oh no! We didnt found the attachment!n{e}')
exit()
mail_subject = "Your subject"
sender_email = "your mail"
sender_name = "Your name"
password = "haahahha"
template_file = "template.html"
csv_file = "details.csv"
e = pd.read_csv(csv_file)
receiver_emails = e['EmailAddress'].values
receiver_names = e["Name"].values
for receiver_email, receiver_name in zip(receiver_emails, receiver_names):
print("Sending to " + receiver_name)
msg = MIMEMultipart()
msg['Subject'] = mail_subject
msg['From'] = formataddr((sender_name, sender_email))
msg['To'] = formataddr((receiver_name, receiver_email))
mail_content = read_template(template_file)
mail_content = mail_content.replace('{mail_receiver}', receiver_name)
msg.attach(MIMEText(mail_content, 'html'))
# filename = "filename.pdf"
# msg.attach(file_object(filename))
try:
server = smtplib.SMTP("smtp.gmail.com", 587)
context = ssl.create_default_context()
server.starttls(context=context)
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, msg.as_string())
print('Email sent!')
except Exception as e:
print(f'Oh no! Something bad happened!n{e}')
break
finally:
print('Closing the server...')
server.quit()