Skip to content

Instantly share code, notes, and snippets.

@zevaverbach
Created April 13, 2017 16:22
Show Gist options
  • Save zevaverbach/8ab47f0990b6dbec6879e9e7ccb14355 to your computer and use it in GitHub Desktop.
Save zevaverbach/8ab47f0990b6dbec6879e9e7ccb14355 to your computer and use it in GitHub Desktop.
basic email automation proof of concept for teaching
from datetime import date
from email import encoders
from email.mime.application import MIMEApplication
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
from time import sleep
FROM_EMAIL = 'put your from email here (can be same as MAIL_USERNAME)'
MAIL_PASSWORD = 'put your email password here'
MAIL_USERNAME = 'put your email username here'
INTERVAL = 'put an int here for how often to check (in seconds) whether prospects should be contacted'
REACH_OUT_EVERY = 'int for how long to wait between reaching out to contacts'
SERVICE_TYPE = 'the service you\'re offering'
first_reachout_email_message = "Hey, do you ever use transcription?"
second_reachout_email_message = "Hi {first_name}, Sorry to bug you again, but do you use {service}?"
first_reachout_email_message = "Hey {first_name}, do you ever use {service}?"
third_reachout_email_message = "Hi {first_name}, my last check-in: Do you use {service}?"
messages = [first_reachout_email_message, second_reachout_email_message, third_reachout_email_message]
prospect = dict(name='Joe', industry='qual', email='prospect@pr.io')
prospect2 = dict(name='Brendan', industry='qual', email='brendan@pr.io')
prospect3 = dict(name='Person', industry='qual', email='person@pr.io')
prospects = [prospect, prospect2, prospect3]
for prospect in prospects:
prospect['last_contacted'] = date(1990, 1, 1)
prospect['last_email_sent_index'] = None
def should_be_contacted(prospect, reach_out_every):
if (date.today() - prospect['last_contacted']).days > reach_out_every:
return True
return False
def get_index_of_next_template(prospect):
if prospect['last_email_sent_index'] is None:
return 0
return prospect['last_email_sent_index'] + 1
def run_automated_email():
for prospect in prospects:
if should_be_contacted(prospect, REACH_OUT_EVERY):
template_index = get_index_of_next_template(prospect)
if template_index >= len(templates) + 1:
continue
send_email(recipients=prospect['email'], subject='hey',
message=templates[template_index].format(first_name=prospect['name'], service=SERVICE))
prospect['last_email_sent_index'] = template_index
def send_email_from_gmail(from_, to, subject, html, text, login, pass_, attachments=[]):
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.login(login, pass_)
msg = MIMEMultipart('alternative')
msg["From"] = from_
msg["Reply-to"] = from_
msg['To'] = to
msg['Subject'] = subject
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
msg.attach(part1)
msg.attach(part2)
if len(attachments) > 0:
for filepath in attachments:
with open(filepath, 'rb') as fp:
attachment = MIMEBase('application', 'octet-stream')
attachment.set_payload(fp.read())
encoders.encode_base64(attachment)
attachment.add_header('Content-Disposition', 'attachment',
filename=os.path.basename(filepath))
msg.attach(attachment)
server.sendmail(from_, [to], msg.as_string())
server.quit()
def send_email(recipients, subject, message, attachments=[]):
if isinstance(recipients, list):
recipients = ', '.join(recipients)
send_email_from_gmail(from_=FROM_EMAIL, to=recipients, subject=subject,
html=f'<p>{message}</p>', text=message, login=MAIL_USERNAME,
pass_=MAIL_PASSWORD, attachments=attachments)
def main():
while True:
run_automated_email()
sleep(INTERVAL)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment