DEV Community

Cover image for Build Audience with Mailchimp API for Free Using Laravel
Mostafa Said
Mostafa Said

Posted on • Edited on

Build Audience with Mailchimp API for Free Using Laravel

Hello everyone πŸ‘‹,

Mailchimp is one of the most popular all-in-one marketing platforms that helps you manage and talk to your clients, customers, and other interested parties. There is no doubt that Newsletter is one of the most essential feature for any successful business. As a web developer building your own blog or portfolio or building a project for another business, you surely should consider including a Newsletter service.

Mailchimp is providing a free signup option with free account which is very useful for any business which just started to grow an audience. They have other paid plans as well but if you're just starting the free plan should be more than enough for your. You can read more about their plans from HERE.

Mailchimp's API is really one of the easiest APIs out there to deal with, also the documentation is unbelievably simple and easy to understand which means that you don't have to be an expert to use it. Today I'm going to explain how to use Mailchimp's API in your Laravel projects as a Newsletter service.

1- Create an account:

Head to https://mailchimp.com/ and sign up for free. The sign up process is actually pretty simply and easy.

2- Generate API key:

After you're logged in to your account, press on your name on the bottom left of the screen and choose 'Account' then 'Extras' then 'API Keys' then click on 'generate key'.

3- Install Client Library for PHP:

Open your terminal in your Laravel's root directory and hit the following command composer require mailchimp/marketing.
This will install all the dependencies you need to start using Mailchimp API.

4- Make your first API call:

Copy and paste the below code in your resources/routes/web.php file. This will be a route to a testing endpoint called ping just to check if your API call is successful or not.

Route::get('ping', function () {

    $mailchimp = new \MailchimpMarketing\ApiClient();

    $mailchimp->setConfig([
        'apiKey' => config('services.mailchimp.key'),
        'server' => 'us19'
    ]);

    $response = $mailchimp->ping->get();
    ddd($response);
});
Enter fullscreen mode Exit fullscreen mode
  • Replace config('services.mailchimp.key') with API key that Mailchimp provided to you.
  • Replace server => us19 with server prefix.

To find the value for the server prefix, log into your Mailchimp account and look at the URL in your browser. You’ll see something like https://us19.admin.mailchimp.com/ the us19 part is the server prefix. Note that your specific value may be different.

After this step if you hit that {Your Web URL}/ping You should see 'everything chimpy' returned in browser which means that all is great up to this point.

5- Get list ID:

Check your mailchimp account and see if you have an audience list created by default or not. You will know this when you head to Dashboard and click Audience from the sidebar, if you got 'You don't have an audience' then click on create an audience and fill in the form to create your first audience list. If you already have one then let's move on.

Replace the code we used earlier in resources/routes/web.php with the below code considering to change apiKey and server with your own as explained above.

Route::get('ping', function () {

    $mailchimp = new \MailchimpMarketing\ApiClient();

    $mailchimp->setConfig([
        'apiKey' => config('services.mailchimp.key'),
        'server' => 'us19'
    ]);

    $response = $mailchimp->lists->getAllLists();
    ddd($response);
});
Enter fullscreen mode Exit fullscreen mode

When you hit Because ddd in laravel is so awesome, this will return array of lists which will include your list ID.

6- Add member to the list:

Now lets add our first member to the list. Replace the code with the below code to manually add a member to the list.

  • Replace List_id with your list ID.
Route::get('ping', function () {

    $mailchimp = new \MailchimpMarketing\ApiClient();

    $mailchimp->setConfig([
        'apiKey' => config('services.mailchimp.key'),
        'server' => 'us19'
    ]);

    $response = $mailchimp->lists->addListMember("list_id", [
    "email_address" => "test.test@gmail.com",
    "status" => "subscribed",
]);
    ddd($response);
});
Enter fullscreen mode Exit fullscreen mode

After heading to {Your Web URL}/ping in browser, you should be able to see that contact added to the audience list on Mailchimp.

7- Make it dynamic:

Now you can create a form anywhere in your blade files and receive the user response in your code and send it to Mailchimp API.

  • A form to demonstrate:
<form method="POST" action="/newsletter">
    @csrf
    <div>
        <input id="email" name="email" type="text" 
               placeholder="Your email address" />
    </div>

    <button type="submit">
        Subscribe
    </button>
</form>
Enter fullscreen mode Exit fullscreen mode
  • resources/routes/web.php:
Route::get('newsletter', function () {

    $mailchimp = new \MailchimpMarketing\ApiClient();

    $mailchimp->setConfig([
        'apiKey' => config('services.mailchimp.key'),
        'server' => 'us19'
    ]);

    $response = $mailchimp->lists->addListMember("12345678", [
    "email_address" => request('email'),
    "status" => "subscribed",
]);
    return redirect('/');
});
Enter fullscreen mode Exit fullscreen mode

Of course you can now make a controller and clean up your code and just like that! You have a working Newsletter service on your Website.

Thank you for reading my article and I hope it was useful to you :) For more Laravel tips along with VueJS 3 and Tailwind, follow me ❀️

Top comments (0)