-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
76 lines (55 loc) · 2.11 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import smtplib, ssl
from verseAPI import *
from string import Template
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
#TODO: SETUP EMAIL, FINISH BUILDING EMAIL BODY, SEND TEST EMAIL
SENDER_ADDRESS = '26438dc37f3f01' #Mailtrap testing credentials - NOT STATIC
PASSWORD = '3693b2d82a2421' #Mailtrap testing credentials - NOT STATIC
def get_contacts(filename):
"""
Return two lists names, emails containing names and email addresses
read from a file specified by filename.
"""
names = []
emails = []
with open(filename, mode='r', encoding='utf-8') as contacts_file:
for a_contact in contacts_file:
names.append(a_contact.split()[0])
emails.append(a_contact.split()[1])
return names, emails
def read_template(filename):
"""
Returns a Template object comprising the contents of the
file specified by filename.
"""
with open(filename, 'r', encoding='utf-8') as template_file:
template_file_content = template_file.read()
return Template(template_file_content)
def main():
names, emails = get_contacts('email.txt') # read contacts
message_template = api_response
# set up the SMTP server
s = smtplib.SMTP(host='smtp.mailtrap.io', port=2525)
s.starttls()
s.login(SENDER_ADDRESS, PASSWORD)
# 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 = api_response #message_template.substitute(PERSON_NAME=name.title())
# Prints out the message body for our sake
print(message)
# setup the parameters of the message
msg['From'] = SENDER_ADDRESS
msg['To'] = email
msg['Subject'] = "This is TEST"
# 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
# Terminate the SMTP session and close the connection
s.quit()
if __name__ == '__main__':
main()