Skip to main content

How do i collect the users emails into mandrill ?

Gems / Libraries • Asked by Ahmed Althani

I have watched the mandrill tutorial. I just have a quick question. How can i link the list on mandrill or any email service with the users emails (when the user signs up ) and update the list whenever someone signs up ? Thanks


Hey Ahmed,

Sorry for the slow reply on this one. You can add an after_create callback to your User model to add the email to the mailing list via their API. If you're using Mailchimp, you can use the Gibbon gem to do that.

I do something like this for GoRails when someone subscribes:

class User
    def add_to_mailchimp
    $gibbon.lists.subscribe(
      id: "a04070a071",
      email: {email: email},
      double_optin: false,
      merge_vars: {
        FNAME: first_name,
        LNAME: last_name,
        PLAN: (subscribed? ? "screencast-9" : ""),
      }
    ) if Rails.env.production?
  rescue Gibbon::MailChimpError => e
    Rails.logger.error "Unable to add #{email} to mailchimp: #{e}"
  end
end
# config/initializers/gibbon.rb
$gibbon = Gibbon::API.new Rails.application.secrets.mailchimp_api_key
# config/secrets.yml
production:
  mailchimp_api_key: MY_API_KEY

Thanks for your help Chris :) !!!


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 27,623+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.