-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsending_mails.py
65 lines (55 loc) · 1.96 KB
/
sending_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
import smtplib
import pandas as pd
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import re
import config
def send_email(subject, msg, to_):
global mail_count
#Setup the MIME
message = MIMEMultipart("alternative")
message['From'] = config.EMAIL_ADDRESS
message['To'] = to_
message['Subject'] = subject
# attach the body with the msg instance
message.attach(MIMEText(msg, 'html'))
msg = message.as_string()
try:
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(config.EMAIL_ADDRESS, config.PASSWORD)
server.sendmail(config.EMAIL_ADDRESS, to_, msg)
server.quit()
mail_count = mail_count + 1
print(mail_count,". Success: Email sent to ", to_)
except:
print("Email failed to send.")
print("Total", mail_count," Mail Sent!!")
###---------------------------MAIN--------------------------------------
# Here, the given .xlsx file name is participants. stored at the same folder
data = pd.read_excel('YOUR_MAILLIST.xlsx')
name_list = data["Name"].tolist() #NAME HEADER
mailid_list = data["MailId"].tolist() #MAIL-ID HEADER
email_pattern= re.compile("^.+@.+\..+$")
mail_count = 0
for i,mail in enumerate(mailid_list):
author = name_list[i]
fname = author.split(' ')[0]
#-----------------------------------------------------------------------------
# CUSTOM MESSAGE (HTML/PLAIN)
msg = '''Dear '''+ author +''',<br><br>
I hope that this email finds you well.<br><br>
If you have already received this message, please ignore it. <br><br>
Thanks & Regards,<br>
NAME OF THE SENDER<br>
AFFILIATION OF THE SENDER<br>
'''
subject = "SUBJECT OF THE MAIL"
try:
if( email_pattern.search(mail)):
send_email(subject, alumni_msg, mail)
except:
print("Mail id is not correct.", mail)
print("Total", mail_count," Mail Sent!!")