-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailAutomation.py
96 lines (58 loc) · 1.81 KB
/
mailAutomation.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import smtplib
import email
import pandas as pd
import numpy as np
import getpass
# In[2]:
# Function to read the contacts from a given contact file and return a
# list of names and email addresses
def get_contacts(filename):
contacts = pd.read_excel(filename)
names = contacts['Name']
emails = contacts['Email ID']
return names, emails
# In[3]:
from string import Template
# read the message template which is stored in the supplied filename
def read_template(filename):
with open(filename, 'r') as template_file:
template_file_content = template_file.read()
return Template(template_file_content)
# In[4]:
# set up the SMTP server
s = smtplib.SMTP(host='smtp.gmail.com', port=587)
s.starttls()
# input username
UserName = input("Enter Username: ")
# input password
Password = getpass.getpass("Enter Password: ")
s.login(UserName, Password)
# In[5]:
# get contact names and email ids
names, emails = get_contacts('contacts.xlsx')
# message templater̥
message_template = read_template('template.txt')
# print(names[0], emails[0])
# In[6]:
# import necessary packages
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
subject = input("Enter the Subject: ")
# For each contact, send the email:
for name, email in zip(names, emails):
msg = MIMEMultipart() # create a message
# add in the actual person name to the message template
message = message_template.substitute(PERSON_NAME=name.title())
# setup the parameters of the message
msg['From']=UserName
msg['To']=email
msg['Subject']=subject
# add in the message body
msg.attach(MIMEText(message, 'plain'))
# send the message via the server set up earlier.
s.send_message(msg)
del msg
# In[ ]: