Skip to content

Instantly share code, notes, and snippets.

@analyticsPierce
Created November 12, 2013 16:33
Show Gist options
  • Save analyticsPierce/7434085 to your computer and use it in GitHub Desktop.
Save analyticsPierce/7434085 to your computer and use it in GitHub Desktop.
Here is a quick ruby file for sending customer info from a db to a list in mailchimp.
#! /usr/bin/env ruby
require 'logger'
require 'net/http'
require 'mysql2'
require 'gibbon'
@log = Logger.new("/mailchimp_sender_log.log", 5, 1024000)
@log.level = Logger::INFO
@myst_db = Mysql2::Client.new(:host => "host", :username => "username", :password => "password")
// add your own db credentials
@log.info("my db connection: #{@myst_db}")
# establish mailchimp connection
api_key = "api-key" // add your own mailchimp api key
@gibbon = Gibbon::API.new(api_key)
@log.info("mailchimp connection ok with api_key: #{api_key}")
@mc_list_id = 'id' // add your own mailchimp list id
referrals = @myst_db.query("SELECT * FROM mind_app_demo.referral_program_mc")
#def subscribe_batch_to_list(batch)
referrals.each_slice(3) do |batch|
begin
prepared_batch = batch.map do |referral|
{
:EMAIL => {:email => referral['email']},
:EMAIL_TYPE => 'html',
:merge_vars => {
:MMERGE6 => referral['field_1'],
:MMERGE7 => referral['field_2'],
:MMERGE8 => referral['field_3'],
:MMERGE9 => referral['field_4'],
:MMERGE11 => referral['field_5'],
:MMERGE12 => referral['field_6'],
:MMERGE13 => referral['field_7'],
:MMERGE14 => referral['field_8'],
:MMERGE15 => referral['field_9'],
:FNAME => referral['first_name']
}
}
end
@log.info("prepared_batch : #{prepared_batch}")
result = @gibbon.lists.batch_subscribe(
:id => @mc_list_id,
:batch => prepared_batch,
:double_optin => false,
:update_existing => true
)
@log.info("#{result}")
rescue Exception => e
@log.warn("Unable to load batch into mailchimp because #{e.message}")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment