NomNomCameron
NomNomCameron

Reputation: 469

Mailchimp API for Rails - list.subscribe(:double_optin => false) not working

I am using the mailchimp-api gem and can get the submitted email to work by submitting an email and having a confirmation email sent to that email that signed up but I want to disable the double_optin flag. I am trying to do it with:

@mc = Mailchimp::API.new('my api key here')
@mc.list.subscribe('list_id', {'email' => params[:email]}, :double_optin => false)

This is still sending a confirmation email to that email address.

I really don't like how it redirects to a mailchimp page to have you confirm your subscription and have to click another button to be redirected to the site. If you could customize the confirmation email that would be one thing but having this generic confirmation page is terrible.

I am wondering if you have to have a paid account to be able to toggle the :double_optin flag?

Upvotes: 2

Views: 1400

Answers (5)

lacoder
lacoder

Reputation: 183

This just expands on the answer from @Jordan above to explain why that works and why the accepted answer is incorrect.

If you are using the mailchimp-api gem, you can see in the documentation that the format for a single subscribe is subscribe(id, email, merge_vars = nil, email_type = 'html', double_optin = true, update_existing = false, replace_interests = true, send_welcome = false). After a lot of guessing and testing, I figured out that they don't want you to pass the parameter names, just the values. So, while I was originally passing double_optin: false in the function, they really just want you to pass false as the 5th parameter. In other words, this is what worked for me:

mailchimp = Mailchimp::API.new('mailchimp_api_key')
subscribe = mailchimp.lists.subscribe(
  'list_ID_number',
  { email: user.email },
  { fname: user.first_name, lname: user.last_name },
  'html',
  false,
  false,
  false,
  false
)

Note that the second and third parameters (email and merge_vars) were passed as hashes because that's how those parameters are defined in the documentation referenced above.

After making that change (removing the parameter names), subscriptions went right through without the confirmation emails.

Upvotes: 2

Nimish Gupta
Nimish Gupta

Reputation: 3175

After going through mailchimp-api gem I found this solution

@mc.list.subscribe('list_id', { email: params[:email] }, nil, double_optin = false)

Upvotes: 0

Jordan
Jordan

Reputation: 1457

This is what I used from mailchimp-api

subscribe(id, email, merge_vars = nil, email_type = 'html', double_optin = true, update_existing = false, replace_interests = true, send_welcome = false) ⇒ Hash

The double_optin is the parameter which will be passed

So, I used:

client.lists.subscribe(@list_id, email, nil, 'html', false)

Upvotes: 3

koanima
koanima

Reputation: 577

The accepted answer using subscribe method does not work and will still send the confirmation email, but it is easy to do this using the batch_subscribe method instead.

First, create an array containing a hash for each subscriber you want to add (or just one), like this:

subscribers = [{ "EMAIL" => { "email" => user.email},
                 :EMAIL_TYPE => 'html',
                 :merge_vars => { "NAME" => user.name
                                }
              }]

Then, use batch_subscribe method, like this:

mailchimp = Mailchimp::API.new(MAILCHIMP_API_KEY)
mailchimp.lists.batch_subscribe(MAILCHIMP_LIST_ID, subscribers, false, false, false)

The three "falses" at the end refer to, in order: double_optin, update_existing, and replace_interests. You can set those to true or false, but double_optin needs to be "false" if you want to skip sending the confirmation email.

Here is the documentation for the batch_subscribe method.

Upvotes: 0

NomNomCameron
NomNomCameron

Reputation: 469

Ended up getting this to work with the following:

@mc = Mailchimp::API.new('my api key here')
@mc.list.subscribe({:id => 'list_id', :email => {:email => params[:email]}, :double_optin => false})

Note the difference here is that every parameter in @mc.list.subscribe is in a hash.

Upvotes: 1

Related Questions