Skip to content

Instantly share code, notes, and snippets.

@chrisdev
Created June 5, 2015 04:09
Show Gist options
  • Save chrisdev/69a9e923f9d37671222e to your computer and use it in GitHub Desktop.
Save chrisdev/69a9e923f9d37671222e to your computer and use it in GitHub Desktop.
# sending the email
# ----------
import sys
import mandrill
API_KEY = 'API_KEY_HERE'
def send_mail(template_name, file_name, batch_size=2, starting_pos=0):
mandrill_client = mandrill.Mandrill(API_KEY)
if file_name:
try:
f = open(file_name, 'r')
except:
print "Unexpected error:", sys.exc_info()[0]
email_list = f.read().split('\n')
if starting_pos < len(email_list):
for em in email_list[starting_pos:starting_pos+batch_size]:
message = {
'to': []
}
message['to'].append({'email': em})
mandrill_client.messages.send_template(template_name, [], message)
else:
print "Starting position is greater than the size of the mailing list"
return 0
return starting_pos+batch_size
if __name__ == '__main__':
file_name = "email_list.txt"
batch_size = 302
starting_pos = 1200
print "Starting to send %d emails starting from position %d" % (batch_size, starting_pos)
pos = send_mail(template_name='newsletter-confirm-subscription',
file_name=file_name,
batch_size=batch_size,
starting_pos=starting_pos)
print "The new starting pos is %d" % (pos)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment