自动将用户信息发送到 Mailchimp

Sending user info to Mailchimp automatically

我需要做什么才能在用户注册我的应用程序 (Devise) 后立即自动将用户名和电子邮件发送到我的 Mailchimp 帐户?

您可能应该在创建挂钩后使用常规用户

class User < ActiveRecord::Base
  after_create :send_to_mailchimp

  def send_to_mailchimp
    # send data to mailchimp
  end
end

我喜欢使用 Gibbon,一个与他们的 api

一起工作的 mailchimp 包装器
  def add_to_mailchimp(email_address, first_name, last_name)
    list_id = "your_list_id"
    gibbon = Gibbon::Request.new
    # only need the md5_email if you want to use 'upsert' (find_or_create_by) or 'update', not 'create'
    md5_email = Digest::MD5.hexdigest(email_address)

     gibbon.lists(list_id).members(md5_email).upsert(body: {email_address: email_address, status: "subscribed", merge_fields: {FNAME: first_name, LNAME: last_name}})
  end