4

I'm trying to use the MailChimp API to update an email address of a member when they change their email in our web app.

I'm using the Laravel MailChimp bundle and it's been working great (I can subscribe users, update groupings, update name etc), but I must have the merge_vars or something incorrect.

I use this:

$member_details = array(
    // grabbed from config and working (also API key handled by bundle)
    'id' => $id,
    // passed from function - corresponds to the old email address
    'email_address' => $mailchimp_old_email,
    'merge_vars' => array(
        // Old email again?
        'EMAIL' => $mailchimp_old_email,     
        // new email address        
        'NEW-EMAIL' => $mailchimp_new_email, 
    ),
    'replace_interests' => FALSE,
);

$response = Mailchimp::listUpdateMember($member_details);

So "$response = 1", which made me think it had worked, but the user email has not changed when I view the subscriber list on MailChimp.

The 1.3 API docs have listSubscribe detailing the merge_vars "EMAIL" and "NEW-EMAIL" and I read about it on this stackoverflow post. I did try using listSubscribe again even though it was an existing member, but this failed with a $response saying the member is already subscribed.

Any recommendations on where I might be going wrong? I haven't found a clear example of this sort of listUpdateMember api usage.

Community
  • 1
  • 1
alexleonard
  • 1,314
  • 3
  • 21
  • 37

2 Answers2

8

It turns out the answer is very simple.

https://twitter.com/MailChimp_API/status/351674145609748480

Apparently NEW-EMAIL isn't needed at all in the merge_vars - just EMAIL.

So the working code in my case is:

$member_details = array(
    // grabbed from config and working (also API key handled by bundle)
    'id' => $id,
    // passed from function - corresponds to the old email address
    'email_address' => $mailchimp_old_email,
    'merge_vars' => array(
        // new email address        
        'EMAIL' => $mailchimp_new_email,     
    ),
    'replace_interests' => FALSE,
);

$response = Mailchimp::listUpdateMember($member_details);

This works straight out. Feels like "NEW-EMAIL" really isn't necessary (or EMAIL should be removed and just use "NEW-EMAIL" as it's somewhat more descriptive of what's happening).

alexleonard
  • 1,314
  • 3
  • 21
  • 37
0

If you've updated to use MailChimp API v3, there's a new way to do it.

Use patch or put method on /lists/members endpoint.

email_address is a "top-level" property on the object you push (on the same level as merge_fields, which is called merge_vars in API 2) and represents the new email address.

MailChimp Documentation

stevevance
  • 381
  • 4
  • 10