aboutsummaryrefslogtreecommitdiffstats
path: root/mailing.py
blob: 40e0b39a012f479f8ec47003b0ba00cace2fdc9d (plain)
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
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()