Advertisement
Guest User

Untitled

a guest
Apr 13th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. from datetime import date
  2. from email import encoders
  3. from email.mime.application import MIMEApplication
  4. from email.mime.base import MIMEBase
  5. from email.mime.multipart import MIMEMultipart
  6. from email.mime.text import MIMEText
  7. import smtplib
  8. from time import sleep
  9.  
  10. FROM_EMAIL = 'put your from email here (can be same as MAIL_USERNAME)'
  11. MAIL_PASSWORD = 'put your email password here'
  12. MAIL_USERNAME = 'put your email username here'
  13. INTERVAL = 'put an int here for how often to check (in seconds) whether prospects should be contacted'
  14. REACH_OUT_EVERY = 'int for how long to wait between reaching out to contacts'
  15. SERVICE_TYPE = 'the service you\'re offering'
  16.  
  17. first_reachout_email_message = "Hey, do you ever use transcription?"
  18. second_reachout_email_message = "Hi {first_name}, Sorry to bug you again, but do you use {service}?"
  19. first_reachout_email_message = "Hey {first_name}, do you ever use {service}?"
  20. third_reachout_email_message = "Hi {first_name}, my last check-in: Do you use {service}?"
  21.  
  22. messages = [first_reachout_email_message, second_reachout_email_message, third_reachout_email_message]
  23.  
  24. prospect = dict(name='Joe', industry='qual', email='prospect@pr.io')
  25. prospect2 = dict(name='Brendan', industry='qual', email='brendan@pr.io')
  26. prospect3 = dict(name='Person', industry='qual', email='person@pr.io')
  27.  
  28. prospects = [prospect, prospect2, prospect3]
  29.  
  30. for prospect in prospects:
  31. prospect['last_contacted'] = date(1990, 1, 1)
  32. prospect['last_email_sent_index'] = None
  33.  
  34.  
  35. def should_be_contacted(prospect, reach_out_every):
  36. if (date.today() - prospect['last_contacted']).days > reach_out_every:
  37. return True
  38. return False
  39.  
  40.  
  41. def get_index_of_next_template(prospect):
  42. if prospect['last_email_sent_index'] is None:
  43. return 0
  44. return prospect['last_email_sent_index'] + 1
  45.  
  46.  
  47. def run_automated_email():
  48. for prospect in prospects:
  49. if should_be_contacted(prospect, REACH_OUT_EVERY):
  50. template_index = get_index_of_next_template(prospect)
  51. if template_index >= len(templates) + 1:
  52. continue
  53. send_email(recipients=prospect['email'], subject='hey',
  54. message=templates[template_index].format(first_name=prospect['name'], service=SERVICE))
  55. prospect['last_email_sent_index'] = template_index
  56.  
  57.  
  58. def send_email_from_gmail(from_, to, subject, html, text, login, pass_, attachments=[]):
  59. server = smtplib.SMTP('smtp.gmail.com', 587)
  60. server.ehlo()
  61. server.starttls()
  62. server.login(login, pass_)
  63.  
  64. msg = MIMEMultipart('alternative')
  65.  
  66. msg["From"] = from_
  67. msg["Reply-to"] = from_
  68. msg['To'] = to
  69. msg['Subject'] = subject
  70.  
  71. part1 = MIMEText(text, 'plain')
  72. part2 = MIMEText(html, 'html')
  73. msg.attach(part1)
  74. msg.attach(part2)
  75.  
  76. if len(attachments) > 0:
  77.  
  78. for filepath in attachments:
  79. with open(filepath, 'rb') as fp:
  80. attachment = MIMEBase('application', 'octet-stream')
  81. attachment.set_payload(fp.read())
  82. encoders.encode_base64(attachment)
  83. attachment.add_header('Content-Disposition', 'attachment',
  84. filename=os.path.basename(filepath))
  85. msg.attach(attachment)
  86.  
  87. server.sendmail(from_, [to], msg.as_string())
  88. server.quit()
  89.  
  90.  
  91. def send_email(recipients, subject, message, attachments=[]):
  92. if isinstance(recipients, list):
  93. recipients = ', '.join(recipients)
  94.  
  95. send_email_from_gmail(from_=FROM_EMAIL, to=recipients, subject=subject,
  96. html=f'<p>{message}</p>', text=message, login=MAIL_USERNAME,
  97. pass_=MAIL_PASSWORD, attachments=attachments)
  98.  
  99.  
  100. def main():
  101. while True:
  102. run_automated_email()
  103. sleep(INTERVAL)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement