-
Notifications
You must be signed in to change notification settings - Fork 4
/
mass-email.py
executable file
·68 lines (56 loc) · 1.86 KB
/
mass-email.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
#!/usr/bin/python
# Imports
import markdown, sys, csv, getpass, smtplib, argparse
from email.mime.text import MIMEText
from jinja2 import Template
# Argument Parsing
parser = argparse.ArgumentParser()
parser.add_argument('-m', '--markdown', help='Path to Markdown Template', required=True)
parser.add_argument('-c', '--csv', help='Path to CSV file', required=True)
parser.add_argument('-v', '--verbose', help='Write out emails')
args = parser.parse_args()
# Variables
markdownf = args.markdown
csvf = args.csv
verbose = False
# User input
username = raw_input("Username: ")
password = getpass.getpass("Password: ")
name = raw_input("Name: ")
subject = raw_input("Subject: ")
emails_sent = 0
# Login to SMTP Server
def login(username, password):
try:
server = smtplib.SMTP("smtp.gmail.com:587")
server.starttls()
server.login(username, password)
return server
except smtplib.SMTPAuthenticationError:
print "Incorrect Username or Password"
server.close()
sys.exit(1)
server = login(username, password)
# Read Markdown file into template
with open(markdownf, 'r') as md_file:
md_template = Template(md_file.read())
# Read CSV file, render template
with open(csvf, 'r') as csv_file:
csv_data = csv.DictReader(csv_file)
for row in csv_data:
rendered_template = md_template.render(row)
html = markdown.markdown(rendered_template)
# Format Email
msg = MIMEText(html, 'html')
msg['Subject'] = subject
msg['From'] = name
msg['To'] = row['email']
# Print out emails to screen
if verbose == True:
print msg.as_string()
# Send email
server.sendmail(username, [row['email']], msg.as_string())
print "Email sent to: %s" % row['email']
emails_sent += 1
server.close()
print "\nTotal Emails Sent:", emails_sent