This repository has been archived by the owner on Jan 22, 2023. It is now read-only.
forked from Akash-Ramjyothi/Bulk-Email-Sender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
54 lines (49 loc) · 1.79 KB
/
main.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
import pandas as pd
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
import smtplib
#Sender's email credentials
SenderAddress = "Your Email"
password = "Your Password"
#Read's excel data
e = pd.read_excel("Contacts File")
c = pd.read_excel("Coupon Code File")
#Add the coupon code column to the email excel file (for simplicity)
e = e.drop(columns='contact_name')
c = c[c['Status'] != 'Given']
e['couponcodes'] = list(c['Discount Code'])[:e.shape[0]]
#Create and send customzied email
for index, row in e.iterrows():
#Setup Multi-part message
msg = MIMEMultipart()
msg['Subject'] = "Your Subject"
#Setup Message (Customize as required)
message= """\
<html>
<body>
<!-- Sample HTML, Customize to suit your needs -->
<h1> Hey! Your Coupon code is: {} </h1>
</body>
</html>
""".format(row['couponcodes'])
#Convert the html string to the actual body
HTML_Contents = MIMEText(message, 'html')
#open and initialize the pdf file to attach
fo = open(row['certificates'] + '.pdf', 'rb')
attach = MIMEApplication(fo.read(), _subtype='pdf')
fo.close()
attach.add_header('Content-Disposition', 'attachment', filename='certificate.pdf')
#attach pdf and the email body
msg.attach(attach)
msg.attach(HTML_Contents)
msg['To'] = row['contact_email']
msg['From'] = SenderAddress
#send email
with smtplib.SMTP_SSL("smtp.gmail.com", 465) as smtp:
print('Mailing to: ' + row['contact_email'], row['couponcodes'])
smtp.login(SenderAddress, password)
smtp.send_message(msg)
df = pd.read_excel("Coupon Code File", index_col='Discount Code')
df.loc[row['couponcodes'], 'Status'] = 'Given'
df.to_excel("Coupon Code File")