How to Upload Subscribers to Mailchimp Using CSV File (RubyShorts)

How to Upload Subscribers to Mailchimp Using CSV File (RubyShorts) - Webdesign Antwerpen

How To Upload Subscribers To Mailchimp Using CSV File (Ruby) 

Ever wanted to bulk upload users to your mailchimp account but were hindered because of the omnivore alert? Well with some magical ruby code and an API-key you won't have any problems :)

First up, enter this in the command line;

gem install mailchimp-api
require 'mailchimp'
require 'csv'

subscribers = []
contents = CSV.parse(File.read('path/to/file.csv', headers: true, header_converters: :symbol))
contents.each_with_index do |row, i|
  # next if i == 0
  mail = row[:email]
  first = row[:first_name]
  if first.nil?
    subscriber = { 'EMAIL' => { 'email' => mail } }
    p "added #{mail} without name"
  else
    p "added #{mail} with #{first}"
    subscriber = { 'EMAIL' => { 'email' => mail },
                   :EMAIL_TYPE => 'html',
                   :merge_vars => { 'FNAME' => first.capitalize } }
            end
  subscribers << subscriber
end

mailchimp = Mailchimp::API.new('xxxxxxxxxxxx-YOUR-API-KEY-xxxxxxxxxxxxxx')
mailchimp.lists.batch_subscribe('YOUR-LIST-ID', subscribers, false, true, false)

And voila! All people imported! You might have to tweak a thing or two so the code matches the correct CSV collumns. This example was for a CSV file that contained a email and a first name.

Want to stay updated?

Follow my RSS feed and get notified about new articles

Subscribe via RSS

Comments