-
Notifications
You must be signed in to change notification settings - Fork 2
/
gmail.py
72 lines (48 loc) · 1.54 KB
/
gmail.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
import smtplib
from config import config
def main():
server = connect()
contacts = get_csv_contents(config['csv_fname'])
contacts.pop(0) #get rid of headings row
for c in contacts:
(sender, recipient, content) = get_email(c)
server.sendmail(sender, recipient, content)
server.quit()
def connect():
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(config['username'],config['password'])
return server
def get_email(contact):
#contact format:
#contact = [company, name, email]
sender = config['username']+'@gmail.com'
company = contact[0]
recip_name = contact[1]
recipient = contact[2]
msg = get_email_msg(recip_name, config['sender_name'], company)
headers = ["From: " + sender,
"Subject: " + config['email_subject'],
"To: " + recipient]
headers = "\r\n".join(headers)
content = headers + "\r\n\r\n" + msg
return (sender, recipient, content)
def get_csv_contents(fname):
contents = []
file = open(fname, 'r')
for line in file:
contact = line.split(',')
contents.append(contact)
file.close()
return contents
def get_email_msg(recipient, sender, company):
file = open(config['template_fname'], 'r')
msg = file.read()
file.close()
#replace fields
msg = msg.replace('__RECIPIENT__', recipient)
msg = msg.replace('__SENDER_NAME__', sender)
msg = msg.replace('__COMPANY__', company)
return msg
if __name__ == "__main__":
main()