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('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"
    p "added #{mail} with #{first}"
    subscriber = { 'EMAIL' => { 'email' => mail },
                   :EMAIL_TYPE => 'html',
                   :merge_vars => { 'FNAME' => first.capitalize } }
  subscribers << subscriber

mailchimp ='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.


Related Articles

 - Webdesign Antwerpen

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 Antwerpen

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 Antwerpen

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 Antwerpen

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 Antwerpen

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

 - Webdesign Antwerpen

23 VSCode Extensions Every Web Developer Needs

Looking for the best code editor extensions to optimize your workflow? I got you covered. Here are all the ones you should be using in vscode!