2020-12-07 15:01:09 +01:00
|
|
|
import smtplib
|
|
|
|
|
|
|
|
from string import Template
|
|
|
|
|
|
|
|
from email.mime.multipart import MIMEMultipart
|
|
|
|
from email.mime.text import MIMEText
|
|
|
|
|
2022-10-05 11:46:40 +02:00
|
|
|
import pandas as pd
|
2020-12-07 15:01:09 +01:00
|
|
|
|
|
|
|
from vars import *
|
|
|
|
|
2022-10-05 11:46:40 +02:00
|
|
|
|
2020-12-07 15:01:09 +01:00
|
|
|
def get_contacts(filename):
|
|
|
|
"""
|
|
|
|
Return the lists containing the infos
|
|
|
|
read from a file specified by filename.
|
|
|
|
"""
|
2022-10-05 11:46:40 +02:00
|
|
|
|
|
|
|
return pd.read_csv(filename)
|
|
|
|
|
2020-12-07 15:01:09 +01:00
|
|
|
|
|
|
|
def read_template(filename):
|
|
|
|
"""
|
2022-10-05 11:46:40 +02:00
|
|
|
Returns a Template object comprising the contents of the
|
2020-12-07 15:01:09 +01:00
|
|
|
file specified by filename.
|
|
|
|
"""
|
2022-10-05 11:46:40 +02:00
|
|
|
|
2020-12-07 15:01:09 +01:00
|
|
|
with open(filename, 'r', encoding='utf-8') as template_file:
|
|
|
|
template_file_content = template_file.read()
|
|
|
|
return Template(template_file_content)
|
|
|
|
|
2022-10-05 11:46:40 +02:00
|
|
|
|
2020-12-07 15:01:09 +01:00
|
|
|
def main():
|
2022-10-05 11:46:40 +02:00
|
|
|
contacts = get_contacts('contacts.csv')
|
2020-12-07 15:01:09 +01:00
|
|
|
message_template = read_template('message.txt')
|
|
|
|
|
|
|
|
# set up the SMTP server
|
|
|
|
s = smtplib.SMTP(HOST, PORT)
|
|
|
|
s.starttls()
|
|
|
|
s.login(MY_ADDRESS, PASSWORD)
|
|
|
|
|
|
|
|
# For each contact, send the email:
|
2022-10-05 11:46:40 +02:00
|
|
|
for i in contacts.index:
|
2020-12-07 15:01:09 +01:00
|
|
|
msg = MIMEMultipart() # create a message
|
|
|
|
|
|
|
|
# add in the actual person name to the message template
|
2022-10-05 11:46:40 +02:00
|
|
|
contact_dict = {col: contacts[col][i] for col in contacts.columns}
|
|
|
|
message = message_template.substitute(contact_dict)
|
2020-12-07 15:01:09 +01:00
|
|
|
|
|
|
|
# Prints out the message body for our sake
|
|
|
|
print(message)
|
|
|
|
|
|
|
|
# setup the parameters of the message
|
2022-10-05 11:46:40 +02:00
|
|
|
msg['From'] = MY_ADDRESS
|
|
|
|
msg['To'] = contacts["email"][i]
|
|
|
|
msg['Subject'] = SUBJECT
|
|
|
|
|
2020-12-07 15:01:09 +01:00
|
|
|
# add in the message body
|
|
|
|
msg.attach(MIMEText(message, 'html'))
|
2022-10-05 11:46:40 +02:00
|
|
|
|
2020-12-07 15:01:09 +01:00
|
|
|
# send the message via the server set up earlier.
|
|
|
|
s.send_message(msg)
|
|
|
|
del msg
|
2022-10-05 11:46:40 +02:00
|
|
|
|
2020-12-07 15:01:09 +01:00
|
|
|
# Terminate the SMTP session and close the connection
|
|
|
|
s.quit()
|
2022-10-05 11:46:40 +02:00
|
|
|
|
|
|
|
|
2020-12-07 15:01:09 +01:00
|
|
|
if __name__ == '__main__':
|
2022-10-05 11:46:40 +02:00
|
|
|
main()
|