import smtplib from string import Template from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText MY_ADDRESS = 'marlene.bargou@tum.de' PASSWORD = 'password' # TODO: delete password LINK = 'http://localhost:8000/room/raum1/?participant_label=' def get_contacts(filename): """ Return two lists names, emails containing names and email addresses read from a file specified by filename. """ mail = [] label = [] with open(filename, mode='r', encoding='utf-8') as contacts: for contact in contacts: mail.append(contact.split()[0]) label.append(contact.split()[1]) return mail, label 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(): mail, label = get_contacts('contacts.txt') # read contacts message_template = read_template('message.txt') # set up the SMTP server s = smtplib.SMTP(host='postout.lrz.de', port=587) s.starttls() s.login(MY_ADDRESS, PASSWORD) # For each contact, send the email: for mail, label in zip(mail, label): msg = MIMEMultipart() # create a message # add in the actual person name to the message template message = message_template.substitute(OTREE_LINK=LINK, HASH_LABEL=label) # Prints out the message body for our sake print(message) # setup the parameters of the message msg['From'] = MY_ADDRESS msg['To'] = mail msg['Subject'] = "Experiment" # 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()