Creating a mailing list for an application is a pretty common task for a web developer. Numerous sites and applications want page visitors to subscribe to newsletters or emails. This week’s post details the easiest and quickest way to implement a MailChimp mailing list in a PHP application.
MailChimp is a very well-known email and marketing platform. When I was tasked with setting up this functionality on our application, our client had already selected MailChimp. Naturally, I searched the web and found that MailChimp has a robust and easy-to-use API. It’s well-documented. I found articles explaining how set it up, but I saw one particular article about using the mailchimp-api wrapper created by drewm. This wrapper saves time and effort that you’d spend writing the calls to the API yourself.
The ReadMe explains how to set up the application one of two ways. You can either download the MailChimp.php file and include it manually or require mailchimp-api with Composer. My preference is to use Composer:
composer require drewm/mailchimp-api
Make certain that you run ‘composer install’ and add the autoloader to your application.
Either way, once you have mailchimp-api set up, you can easily use it in a class by including:
After that, create a MailChimp object passing your API Key as the parameter
$mailChimp = new MailChimp('abc123abc123abc123abc123abc123-us1');
Then to subscribe a user, you just call post on the MailChimp object and their email address, which sets the status to subscribed.
$result = $mailChimp->post("lists/$list_id/members", [ 'email_address' => 'firstname.lastname@example.org', 'status' => 'subscribed', ]);
‘$list_id’ in this example is the actual ID of the Mailchimp list to which you want the user subscribed. You can also ’print_r’ (the result variable) to ensure the request was successful. That’s very little code to subscribe a user and it’s all very straightforward.
Now, to go a little more in-depth, for the site where I implemented this functionality, we actually created a database for when users signed up. We had different mailing lists based on which upcoming website/feature a user was interested in. So, for example, when they signed up for emails about an upcoming banking site, we updated their ‘mail_list’ attribute in the database to ‘banking’ and then set the ‘$list_id’ of the post call according to which mailing list they wanted to sign up for.
Another advantage of using a database is that you can check if a user has already submitted their email and then inform them (we did so with ajax validation when they actually try to enter information in the form).
Furthermore, if you want to include visitors names with the email addresses of visitors who sign up for your mailing lists, you can add ‘merge_fields’ to the post call. That would look like this:
$result = $mailChimp->post("lists/$list_id/members", [ 'email_address' => 'email@example.com', 'status' => 'subscribed', ‘Merge_fields’ => [ ‘FNAME’ => ‘Michael’, ‘LNAME’ => ‘Spreitzer’ ]);
The ‘FNAME’ and ‘LNAME’ value is replaced by the user’s input (for us it was their name in the database that we got via form).
You can also call $mailChimp->success() to return whether a post was successful or not. We used this to check and update a visitor’s ‘subscribed’ attribute in our database if they were successfully subscribed.
You can do lots more with mailchimp-api, such as removing users, batch operations, etc., but for this post, I wanted to stay focused on subscribing users. This is a very common task when building websites and applications and using this wrapper for MailChimp made the process simple.
Now you can set up the wrapper yourself, make a post call to add a subscriber, and use some of the other ideas we implemented with MailChimp subscriptions. You should have a solid idea of what to do when trying to set up subscriptions to MailChimp for your site.
2amigos Consulting Group can help you integrate your newsletter solution or build your entire mail campaign application. Have a need or an idea? Get in touch and let’s talk.