import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from string import Template from config import gmail_login, gmail_password #SMTP server server = smtplib.SMTP('smtp.gmail.com:587') server.starttls() server.login(gmail_login, gmail_password) with open("templates/email_2015-04-05.txt") as fh: s_text = Template(fh.read()) with open("templates/email_2015-04-05.html") as fh: s_html = Template(fh.read()) with open("test.csv") as fh: mailing_list = [line.strip().split(",") for line in fh] for email, name, username in mailing_list: print(email, name, username) msg = MIMEMultipart('alternative') msg['Subject'] = 'Wedding Invitation' msg['From'] = "Iva and Guillaume <{0}>".format(gmail_login) msg['To'] = email part1 = MIMEText(s_text.substitute(name=name, username=username), 'plain') part2 = MIMEText(s_html.substitute(name=name, username=username), 'html') msg.attach(part1) msg.attach(part2) server.send_message(msg) server.quit()