Skip to content

Instantly share code, notes, and snippets.

@BlitzKraft
Created June 19, 2016 21:58
Show Gist options
  • Save BlitzKraft/a344152d4eee250d75922aa650ea51ca to your computer and use it in GitHub Desktop.
Save BlitzKraft/a344152d4eee250d75922aa650ea51ca to your computer and use it in GitHub Desktop.
### importing libraries
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
import csv
import time
#Creating an object to contains the contacts
class contact:
"This represents the contacts within our contact list"
def __init__(self, firstname="", lastname="", company="", email = ""):
self.firstname = firstname
self.lastname = lastname
self.company = company
self.email = email
# Creating a list to house all of our individual contats
contacts = []
# Here are all the files with the contacts that we're sending an email to.
filenames = ['LIST OF CSV FILES']
ext = ".csv"
fileDir = "DIR WHERE FILES LIVE"
for i in range(0,3):
mediaFile = open(fileDir + filenames[i] + ext, "rU")
mediaList = csv.reader(mediaFile)
if i == 0:
for row in mediaList:
indv = contact(row[1], row[2], row[4],row[11])
contacts.append(indv)
elif i ==1:
for row in mediaList:
indv = contact(row[7], row[8], row[4],row[14])
contacts.append(indv)
else:
for row in mediaList:
indv = contact(row[0], row[1], row[2],row[3])
contacts.append(indv)
#Perform a check on first names to make sure they actually have one that I can use
contacts2 = []
for person in contacts:
if len(person.firstname) >1:
contacts2.append(person)
#Perform a check on duplicate emails
uniq = set()
contacts3 = []
for x in range(0,len(contacts2)):
if contacts2[x].email not in uniq:
uniq.add(contacts2[x].email)
contacts3.append(contacts2[x])
htmlTop = """<html> <head></head>
<body>"""
htmlBot = """</body></html>"""
body = """<p> STUFF HERE</p>"""
fromaddr = "YOUR EMAIL ADDRESS"
toaddr = "TO EMAIL ADDRESS"
#scottFile = open("/Users/alexandercarnes/Documents/Scott Steingberg/Sample Press Email.txt","r")
#scottFile2 = open("/Users/alexandercarnes/Documents/Scott Steingberg/Sample Press Email-test.txt","wr")
#lines = scottFile.readlines()
#scottFile.close()
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "i-hat4-pa22w1rd5@fuKKTh*sShh!T")
for i in range(0,len(contacts3)):
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg["CC"] = toaddr
dearJohn = "<p>Dear %s\n</p>" % (contacts3[i].firstname)
msg['Subject'] = "SUBJECT%s" %(contacts3[i].firstname)
msg.attach(MIMEText(dearJohn + body1, 'html'))
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
del msg
time.sleep(1)
server.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment