Mailchimp API Add Subscriber – Code Examples
Previously I did a lot of work with Mailchimp API in PHP while creating my Mailchimp and WordPress sync plugin, so why not share some working and ready code example with you guys.
In this tutorial examples I am going to use PHP and cURL in order to connect to Mailchimp API. I have a tutorial with WordPress examples as well.
The long story short, here is the ready to use code.
// let's start with some variables
$api_key = 'YOUR MAILCHIMP API KEY HERE';
$email = 'no-reply@rudrastyh.com'; // the user we are going to subscribe
$status = 'subscribed'; // we are going to talk about it in just a little bit
$merge_fields = array( 'FNAME' => 'Misha' ); // FNAME, LNAME or something else
$list_id = ''; // List / Audience ID
// start our Mailchimp connection
$connection = curl_init();
curl_setopt(
$connection,
CURLOPT_URL,
'https://' . substr( $api_key, strpos( $api_key, '-' ) + 1 ) . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/' . md5( strtolower( $email ) )
);
curl_setopt( $connection, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Authorization: Basic '. base64_encode( 'user:'.$api_key ) ) );
curl_setopt( $connection, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $connection, CURLOPT_CUSTOMREQUEST, 'PUT' );
curl_setopt( $connection, CURLOPT_POST, true );
curl_setopt( $connection, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt(
$connection,
CURLOPT_POSTFIELDS,
json_encode( array(
'apikey' => $api_key,
'email_address' => $email,
'status' => $status,
'merge_fields' => $merge_fields,
//'tags' => array( 'Coffee', 'Snowboard' ) // you can specify some tags here as well
) )
);
$result = curl_exec( $connection );
Let’s break down this whole code example into small parts and describe every of them.
- On line
4
we usedsubscribed
status because we are going to subscribe a user. If your goal is to unsubscribe this specific user using Mailchimp API, you can change the status tounsubscribed
. Orpending
if it is required to send a confirmation email. $result
is a JSON object, so in order to process the result from the API, you have tojson_decode()
it first, exampleprint_r( json_decode( $results ) )
.- You might also notice that API key is used as a part of URL and for basic auth.
- How to get an API key and Audience ID we will talk in just a little bit.
$result = json_decode( $result );
if( 400 === $result->status ){
foreach( $result->errors as $error ) {
echo '<p>Error: ' . $error->message . '</p>';
}
} elseif( 'subscribed' === $result->status ){
echo "<p>Thank you, {$result->full_name}!</p>";
}
Get Mailchimp API key
The very first lines of our code require us to specify Mailchimp API key. I am more than sure that you know how to get it, but just in case I will remind you.
Sign-in into your Mailchimp dashboard and click on your photo in the bottom left part of the screen. Then – Account & Billing.

Then, in Account & billing page please go to Extras – API keys.

Everything else should be intuitively simple, I am sure you will figure it out.
Find out your Audience ID (former List ID)
Login to Mailchimp, go to Audiences (Lists) then click on the list title, then in list menu Settings > List name and defaults.


Misha Rudrastyh
Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.
Need some developer help? Contact me
Hii,
I tried your code on my site but it’s not working for me.
by the way should not this line be 3.0?
curl_setopt($mch_api, CURLOPT_USERAGENT, 'PHP-MCAPI/2.0');
Hi,
hmm, everything works for me. Can you give me FTP access to your website (by email)?
Now it’s working. Actually it was my browser issue. thanks for this awesome help.
:)
Ok, great :)
Hi, great code :)
Do you have an AJAX example not connected to WordPress?
Hi, thank you :)
All you need is to change form action to a file where you would like to place mailchimp subscription code.
Hi, I used the code in my site as neccessary – but I am not sure what to put as the
form action
. Do I set it as functions.php?Hi,
form action attribute contains the URL of WordPress AJAX handler.
Hi, Thanks !!
Saved lot of time
Thanks, this was helpful when other tutorials were not.
Hi ,
i need your help. I need the disable the double optin in mailchimp for my shopify. can you please what are all the steps i need to do for disable the double optin?
Hi,
Is MailChimp functionality integrated into your shopify theme? If yes, I recommend to perform a search through your theme files for a keyword
pending
. And when you find it, replace tosubscribed
.Hi,
Thanks for your reply.
Actually i have a newsletter sign up form in my shopify site. So after i have created the signup form in my mailchimp account, i took the form action url alone and used in the shopify.Thats all.
so i think mailchimp functionality is integrated with shopify like you are asking. but i have checked all the files. there is no keyword pending. I have a two shopify sites which one i am using “brooklyn” theme and another one is “Launch-pad star” theme. I am using the mailchimp sign up form integrated with the two sites of nesletter sign up form. But i can’t disable the double optin in those two sites.
Can you please provide any other solution for this??? I really need your help.
Thanks in Advance!!!
I think it is because you are using HTML sign-up form — it doesn’t allow to disable double optin.
If you want to subscribe users without confirmation, you have to use MailChimp API for these purposes. All the examples are above. Or you can try to look for an extension.
OH! Is it the whole thing?!
Not 100% whole, but… yes :) If you need more help, contact me via email, I could write all the code for you.
Can you give an example of this working? I am totally lost.
The example is above :) How can I help you?
Thank you for answering. I have so many questions – like:
Where do I put the API?
How do I make the API add new users based on a custom form?
I’m sorry, Sean, but answers to your questions are very clear in the post.
So, I do not want just to duplicate the text and code from the post here, so I recommend you to read the post once again or ask me the other questions.
Hi Misha,
first of all thank you for this!
I used your WP example and adding users to a mailchimp list with the form works fine, but after user submission of the email a the browser popup dialog opens on the frontend and after closing this the user is redirected to
http://mywebsite.com/admin-ajax.php?fname=Peter&lname=Piper&email=testmail%40testwebsite.com&action=mailchimpsubscribe
My questions:
How can i prevent the browser popup, and redirect to another link without displaying the user information in the browser adress bar? I am using the Foundation framework and would ideally like to open up a modal dialog (http://foundation.zurb.com/sites/docs/reveal.html) upon successful subscription.
And is it possible to just have one field on the frontend for the user’s email adress instead of first & last name + email adress?
Thank you for your time!
Hi Josh,
of course user shouldn’t be redirected to admin-ajax.php. Oh, sorry, I forgot to add
return false;
in my jQuery code – plese try now.Yes, it is possible to have just email address field on the frontend.
Thanks!
Is it possible to retrive the number of subscribers displayed before the form? Like “Join the 456 others”
Hello,
yes, and this post can help you.
Thanks, this code is brilliant! I have it working without being connected to WordPress so thanks! I was just wondering if there is a way to add groupings to the submission through multiple checkboxes? Thanks in advance!
Yes,
I think you can find more info in this post.
Unfortunately that code only work with WordPress and I am working outside of WP. I cannot find the translation for functions like ‘wp_remote_get’… any ideas?
You could try to use the same parameters from
wp_remote_get()
for your cURL request.Hello, whenever I try to submit the form I get the error:
POST http://newage.dev/wp-admin/admin-ajax.php 500 (Internal Server Error) jquery.min.js:4
In the console.
Do you know how to fix this?
Never mind, I got it to work, but it keeps showing the annoying alert box. How do I turn it off?
Hello,
just remove this line of code
alert(data);
or replace it withconsole.log(data);
.I have the same error. How did you solve it?
The error could be in two cases:
1) you forgot to insert
rudr_mailchimp_subscriber_status()
function,2) syntax error somewhere in your code (maybe you forgot about
;
or so ).Hi Valentin,
1) You can use GET
/lists/{list_id}/members/{subscriber_hash}
method where subscriber_hash ismd5( $email )
.2) You should pull out the add_action() lines, and the rudr_ functions outside the WP_Widget class first of all :)
I’m looking for a way to trigger the subscribe action when a checkbox is check, as also call the unsubscribe function when the checkbox is uncheck.
Does anyone have already achieved something like that?
It’d be a really useful minimal UI enabling the user to easily to opt-in a newsletter subscription, perfect for scenarios such as user’s account settings.
Hello Adriano,
just in the PHP code use the following:
Hey Misha, thanks for the tip! That seems promising, I’ll give it a try.
Is there any way to add a a subscriber even if it is missing some ‘required’ merge fields?
You can use dummy data for a required merge field I suppose.
Hi Sean,
try to add
timestamp_signup
parameter to the$data
array :)Hi Misha!
Just this?:
'timestamp_signup' => date("Y-m-d H:i:s"),
Dang that wasn’t it.
I do not believe that worked. The Date Added in the list is still 12/31/1969
I know that you are a very busy guy – so I wanted to add this comment as I figured it out.
If you add this line:
'timestamp_opt' => date("Y-m-d H:i:s")
The MC API will record the date in date added correctly. Thank you so much!
Great, I’m glad that everything works finally. I think your comment will be helpful for other users. Thanks.
HI Misha
This a great tutorials i want to add location of submitted user form how can i add in this
Hi,
I recommend you to consider using HTML5 geolocation.
Can you show me in your current example you created as might help to other as well
Hope :)
I have no ready code for this task :)
Hi,
it seems like javascript error, please check your browser console.
Hi,
I have tried your code.But it is not working for me.For AJAX which code should I use?Please tell me the steps.
Hi,
just read the post carefully – all the info is in it. I really have nothing to say, everything is in the post content, I do not want just to duplicate it here.
Hey Sean,
Have you already read it in official API reference? https://developer.mailchimp.com/documentation/mailchimp/reference/automations/emails/queue/
If this link doesn’t help, I can create a tutorial about it, but the tutorial will be published only on July 4th.
Hi Misha!
Yes I have, but I am a bit lost on how to send this via the cURL. Any help would be great. Thank you!
Hi Sean!
One question – do you use WordPress or not? Because if yes, you can simplify your MailChimp connection.
Hi Misha,
I’ve been using this code for a month and it works great. I want to add a thank you page, though. Is there a way to do that on top of this code?
So basically the user would be taken to a /thank-you page after submitting the email address.
Thanks!
Hi Alex,
you can redirect your subscribers easily to any page after they submitted their email addresses. If you use PHP example, do it with
header('Location: http://thank.you')
or if you use jQuery example, you can implement it withwindow.location.href = 'http://thank.you'
.Thanks! I placed the jQuery redirect and it’s just what I wanted. Error messages don’t show anymore though, but it’s quite alright.
Hello,
I need to get record from Mailchimp with my certain condition on date.I am fetching all subscriber and perform some operation on it.But as my subscriber list is to long server will not response and give me error “Excessive resource usage”.
SO I think if I can take only some specific subscriber then the server load is reduced.
I have to validate by “JOIN_DATE”.
Hello Ritesh,
You can do it this way:
Hi Misha,
Thanks for replay.But I am not using Wp I wrote code in core php and try to implement your code in it but not getting favorable response,getting all records.One more thing I want to tell you that
I am implementing my condition in LAST_PUR i.e., last purchase date.
Sorry, I’ve removed your code because you inserted it incorrectly. You must use the buttons under the comment field.
You filter the results after the response is received – it is incorrect. You should pass
since_timestamp_opt
parameter in your request!Great!
Thank you!
Its work.
It gives me an error :(
“title”:”API Key Missing”,”status”:401,”detail”:”Your request did not include an API key.”
Include API key :)
No worries. I found the problem :)
sorry for last message
i am using same code but not give any response.
any PHP notice?
Hello, I am getting 404 error when trying to integrate in my wp plugin.
Got the error, I paste wrong list ID. anyway thanks for your tutorial :)
Hello,
Ok, I’m glad you’ve figured it out 🙃
hello
how can i make 2 form with Different list ID in one page
sorry for my bad english.
my problem fixed thanks for your tutorial :D
Hey, thanks so much for this! I can’t seem to figure out how to do two lists with separate IDs. Is there a way to do that with this code?
Hey Joel,
Do you mean to subscribe for two lists at once?
If yes, this should help.
Oh no, I’m sorry. I have a page with two different sign up forms for different lists and I’d like to use this method. However, I can’t figure out how to get the two forms to go to the correct lists.
Thanks so much for your response!
I think you can even use the same code, just add input hidden fields with different list IDs to each form.
Oh perfect, that makes sense. Thank you so much for your help!
Spotted a wee typo :)
if( $resul->status == 400 ){
Thank you, fixed 🙃
Hi
Thanks a lot for this! One question, is there any possibility to add tags during the subscription as well?
Hi,
Yes, it is possible, you can use
merge_tags
parameter, example:Thank you so much for this great script, it helped me a lot! :)
Hi Misha,
Great tutorial, thanks a lot for spending time to help me/us.
I can’t get it to work, do I need a SSL certificate to make it work … currently my domain is on HTTP.
Thanks for this. I need to display error message if user is already subscribed. Did not find any key in API return to achieve this. I am using simple PHP. Can you guide?
Thank u
Hi there, great article BTW, has been of immense help to me so thank you.
Do you know how to tag a subscriber whilst adding them to an audience?
cheers chris
It’s working for me, lots of Thanks
Great article, it works absolutely great.
It helped me out so much after looking around for a bit which didn’t work as well but guide is very helpful and works fantastic.
Thanks
Thank you very much!!!