Emailing is an action that marketing uses to send multiple messages via email to specific audiences, usually to sell something, show sales promotions or news. The most common platform to execute this is Mailchimp.
Mailchimp has a section to create audience lists and send emails to them. This is a manual process but if you have a platform with registered users, you can automatically add them into a mailing list.
The quick project this guide is based on was a fun and real implementation of a project I've been working on. Initially, users are imported through a CSV file and that's ok, but implementing the API and connecting our project directly to Mailchimp helps us to remove those extra steps and save time for the client.
In this case I’m going to use Laravel 8 and the library Mailchimp Marketing of PHP.
I will assume you have a laravel project installed, with users registered or a form to add users into your database.
All right, let's get our hands dirty.
Install the library via composer:
composer require mailchimp/marketing
If you need more information, check out the repository: https://github.com/mailchimp/mailchimp-marketing-php
First of all we’re going to setup our variables in an .env file. In this step we need data for:
MAILCHIMP_API_KEY MAILCHIMP_SERVER MAILCHIMP_AUDIENCE_LIST
To find those, we need to go to the mailchimp dashboard. For this you need to create an account here: https://mailchimp.com
1- Audience section
2- Clic on the button “Create Audience” and fill the form. Save it.
3- Go back to the “Audience” section and select “Manage Audience” of Blog users (this is the name of my audience list, as you can see in the step before, so this name might change depending on the project) then go to “Settings”
4- At the bottom of the page we can see “Unique id for audience Blog users”
That value is for MAILCHIMP_AUDIENCE_LIST
5- Now setup your API Keys in Account Profile, select “Extras” then “API keys”
This value is for MAILCHIMP_API_KEY
6- Lastly, we get the value of MAILCHIMP_SERVER. For this we need to copy this value from the url of the browser
The value in this case us6
With those values we can add it to the .env file and start getting data from our database and save it into the “Blog users” audience.
When I was working on this implementation, I made a command in laravel to execute it and it made it easier.
To create the laravel command:
php artisan make:command MailchimpAudience
We set the data into $signature and $description variables, for example
Now, in the class, all the functionality is inside the handle() function
We make a call to the Mailchimp APIClient and set the configuration calling our variables from the .env file, and reference the $list_id variable too.
$mailchimp = new \MailchimpMarketing\ApiClient(); $mailchimp->setConfig([ 'apiKey' => env('MAILCHIMP_API_KEY'), 'server' => env('MAILCHIMP_SERVER') ]); $list_id = env('MAILCHIMP_AUDIENCE_LIST');
We get all the users we want to add
$suscribe_users = User::all();
And iterate each user and subscribe to it. To do that we use setListMember function and pass it the params $list_id and $user_hash
To get the variable $user_hash just hash the email to md5
$user_hash = md5(strtolower($suscribe_user->email));
And in the array store data as: email, status and in merge_fields the fields such as name and last name
$mailchimp->lists->setListMember($list_id, $user_hash, [ "email_address" => $suscribe_user->email, "status_if_new" => "subscribed", "status" => "subscribed", "merge_fields" => [ "FNAME" => $suscribe_user->name, "LNAME" => $suscribe_user->lastname, ] ]); ```
What is the difference between “status_if_new” and “status”? Well, if a user was already registered in the list and we execute our custom command, the user is not re-inserted and keeps their status.
So, finally, our code looks like this:
Execute the command
php artisan mailchimp:audience
And the users are ready in our list
That 's all.
With the Mailchimp API it is even possible to remove users from the list, unsubscribe, etc. Finally, we can have this command run every day or every so often so that we can have an updated list.
Mailchimp is the tool with the most users and the most efficient way to create marketing campaigns. The API is so dynamic that it is very easy to connect our platforms with Mailchimp (with different programming languages, not only PHP). We only see a few things that the API can do but the functionality of this is ample.
To view more of this you can check the documentation here: https://mailchimp.com/developer/marketing/api/lists/
Hope you enjoyed it 🐒