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

 - Webdesign Mechelen

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.


Questions/Suggestions?

Related Articles

 - Webdesign Mechelen

How To Install OneNote On Ubuntu (2017)

Do you love keeping your notes in OneNote, made by Microsoft and can't really live without it? Well in this video I'm going to show you how you can set it up on your linux ubuntu device.

 - Webdesign Mechelen

How To Create An Automatic Sitemap For Your Rails App On Heroku (RailsShorts)

Wish your sitemap was automatically updated once a week or faster without having to manually update it and push the changes to your server? Combine sitemap generator & fog to fix this!

 - Webdesign Mechelen

How To Do Basic CSV Manipulations In Ruby (RubyShorts)

Need some basic stuff done on your CSV like creating, reading, writing or appending? Here's a short overview!

 - Webdesign Mechelen

How To Handle Errors In Ruby With Begin, Rescue & Ensure (RubyShorts)

Are your trying to catch some errors in your ruby application but can't really wrap your head around the begin, rescue and ensure blocks in ruby? Here are some pointers!

 - Webdesign Mechelen

How To Query A Basic API In Ruby (RubyShorts)

Here's a quick article on how you can quickly retrieve data from an API endpoint using the open-uri and json library

How To Install OneNote On Ubuntu (2017)

Do you love keeping your notes in OneNote, made by Microsoft and can't really live without it? Well in this video I'm going to show you how you can set it up on your linux ubuntu device.

How To Create An Automatic Sitemap For Your Rails App On Heroku (RailsShorts)

Wish your sitemap was automatically updated once a week or faster without having to manually update it and push the changes to your server? Combine sitemap generator & fog to fix this!

How To Do Basic CSV Manipulations In Ruby (RubyShorts)

Need some basic stuff done on your CSV like creating, reading, writing or appending? Here's a short overview!

How To Handle Errors In Ruby With Begin, Rescue & Ensure (RubyShorts)

Are your trying to catch some errors in your ruby application but can't really wrap your head around the begin, rescue and ensure blocks in ruby? Here are some pointers!

How To Query A Basic API In Ruby (RubyShorts)

Here's a quick article on how you can quickly retrieve data from an API endpoint using the open-uri and json library