-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
56 lines (44 loc) · 1.48 KB
/
main.py
File metadata and controls
56 lines (44 loc) · 1.48 KB
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 pandas as pd
import smtplib
from email.message import EmailMessage
def format_mail(template, keywords, recipient_row) -> str:
for key in keywords:
to_replace = f'[{key}]'
new_value = str(recipient_row[str(key)])
template = template.replace(to_replace, new_value)
return template
def send_mail(email) -> bool:
import config
try:
with smtplib.SMTP(host='smtp.gmail.com', port=587) as smtp:
smtp.ehlo()
smtp.starttls()
smtp.login(config.email,config.password)
smtp.send_message(email)
except smtplib.SMTPException:
return False
return True
if __name__ == '__main__':
template = ''
template_path = 'mail.txt'
with open(template_path) as f:
template = f.read()
recipients = []
recipients_path = 'recipients.csv'
df = pd.read_csv(recipients_path, delimiter=';')
keywords = df.columns.values.tolist()
for _, recipient_row in df.iterrows():
info = recipient_row.to_dict()
mail_text = format_mail(
template,
keywords,
info
)
email = EmailMessage()
email['from'] = info['from']
email['to'] = info['receiver_email']
email['subject'] = info['subject']
email.set_content(mail_text)
if not send_mail(email):
print(f'[Error with] {info["receiver_email"]}')
print('Done!')