Skip to content

Instantly share code, notes, and snippets.

@cwardzala
Last active August 29, 2015 14:23
Show Gist options
  • Save cwardzala/69b88f7d7767207506c6 to your computer and use it in GitHub Desktop.
Save cwardzala/69b88f7d7767207506c6 to your computer and use it in GitHub Desktop.
RB Email scripts
import os
import mandrill
import sys
import json
prod_key = os.environ.get('MANDRILL_PROD_KEY')
test_key = os.environ.get('MANDRILL_TEST_KEY')
mandrill_client = mandrill.Mandrill(prod_key)
isProd = str(mandrill_client) == '<Mandrill %s>' % prod_key
yes = set(['yes','y', 'ye'])
no = set(['no','n', ''])
def safetyCheck ():
if isProd:
sys.stdout.write('You are using a production key do you want to continue? (yes|no) ')
choice = raw_input().lower()
if choice in yes:
return True
elif choice in no:
print 'aborted'
return False
else:
print 'Please respond with \'yes\' or \'no\''
return False
return False
with open('rb_default-autoship.json') as data_file:
subs = json.load(data_file)
to = []
merge_vars = []
global_vars = []
for subscription in subs:
email = subscription['email'] if 'email' in subscription else None
name = subscription['name'] if 'name' in subscription else email
to.append({
'email': email,
'name': name,
'type': 'to'
})
try:
message = {
'merge_language': 'handlebars',
'merge_vars': merge_vars,
'to': to,
'preserve_recipients': False
}
if safetyCheck():
result = mandrill_client.messages.send_template(template_name='rb-default-auto-ship', template_content=None, message=message, async=True)
print result
else:
sys.exit(0)
except mandrill.Error, e:
print 'A mandrill error occurred: %s - %s' % (e.__class__, e)
raise
import os
import mandrill
import sys
import json
prod_key = os.environ.get('MANDRILL_PROD_KEY')
test_key = os.environ.get('MANDRILL_TEST_KEY')
mandrill_client = mandrill.Mandrill(prod_key)
isProd = str(mandrill_client) == '<Mandrill %s>' % prod_key
yes = set(['yes','y', 'ye'])
no = set(['no','n', ''])
def safetyCheck ():
if isProd:
sys.stdout.write('You are using a production key do you want to continue? (yes|no) ')
choice = raw_input().lower()
if choice in yes:
return True
elif choice in no:
print 'aborted'
return False
else:
print 'Please respond with \'yes\' or \'no\''
return False
return False
with open('rb_preselect.json') as data_file:
subs = json.load(data_file)
to = []
merge_vars = []
global_vars = []
for subscription in subs:
email = subscription['email'] if 'email' in subscription else None
name = subscription['name'] if 'name' in subscription else email
to.append({
'email': email,
'name': name,
'type': 'to'
})
try:
message = {
'merge_language': 'handlebars',
'merge_vars': merge_vars,
'to': to,
'preserve_recipients': False
}
if safetyCheck():
result = mandrill_client.messages.send_template(template_name='rb-pre-select', template_content=None, message=message, async=True)
print result
else:
sys.exit(0)
except mandrill.Error, e:
print 'A mandrill error occurred: %s - %s' % (e.__class__, e)
raise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment