Advertisement
Guest User

Untitled

a guest
Jul 4th, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. # sending the email
  2. # ----------
  3. import sys
  4. import mandrill
  5.  
  6.  
  7. API_KEY = 'API_KEY_HERE'
  8.  
  9.  
  10. def send_mail(template_name, file_name, batch_size=2, starting_pos=0):
  11. mandrill_client = mandrill.Mandrill(API_KEY)
  12.  
  13. if file_name:
  14. try:
  15. f = open(file_name, 'r')
  16. except:
  17. print "Unexpected error:", sys.exc_info()[0]
  18.  
  19. email_list = f.read().split('\n')
  20. if starting_pos < len(email_list):
  21. for em in email_list[starting_pos:starting_pos+batch_size]:
  22. message = {
  23. 'to': []
  24. }
  25. message['to'].append({'email': em})
  26. mandrill_client.messages.send_template(template_name, [], message)
  27. else:
  28. print "Starting position is greater than the size of the mailing list"
  29. return 0
  30.  
  31.  
  32. return starting_pos+batch_size
  33.  
  34. if __name__ == '__main__':
  35. file_name = "email_list.txt"
  36. batch_size = 302
  37. starting_pos = 1200
  38. print "Starting to send %d emails starting from position %d" % (batch_size, starting_pos)
  39. pos = send_mail(template_name='newsletter-confirm-subscription',
  40. file_name=file_name,
  41. batch_size=batch_size,
  42. starting_pos=starting_pos)
  43. print "The new starting pos is %d" % (pos)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement