How to Integrate MailChimp Newsletter in Laravel Application

Do you want to integrate the MailChimp newsletter functionality to your Laravel application? MailChimp is an email marketing service that allows us to send newsletters to our subscribers. In this article, I show you how to integrate the MailChimp newsletter into the Laravel website.

MailChimp is one of the popular email marketing services, which manages the subscribers of your website. Using MailChimp, your subscribers will get a newsletter about new content, announcements, deals, and much more depending on your choice. This is a kind of automated system. The website owner does not need to remember sending the newsletter to subscribers. MailChimp handles on its own, which saves us a lot of time.

Another popular email marketing service providers are:

Recently, I have published an article on MailChimp integration using MailChimp API and PHP. One of the readers asked about MailChimp integration in Laravel. So, I decided to write this article to answer the reader’s question.

Having said that, let’s integrate the MailChimp newsletter in the Laravel application.

Get MailChimp API Key and Audience ID

In order to integrate the MailChimp API, you need to get an API key and Audience ID. Follow the steps below to get these credentials.

Login to your MailChimp account. Under the user icon, select the Account.

account-link

Click on Extra->API keys.

Click API Keys

Under the Your API keys section, click on Create A Key and copy your API key which we need in a moment.

Copy API Key

Now you have your API key ready. Next, get an Audience ID to which you need to add your subscribers. For this, click on the Audience menu and then select the option Settings from the Manage Audience drop-down.

audience

Under the Settings click on the ‘Audience name and defaults’.

audience-name-defaults

On the next page, you will find your Audience ID.

Audience ID

Installation of spatie/laravel-newsletter Library

Spatie has built the fantastic library Laravel newsletter. I am going to integrate the MailChimp newsletter using this library.

Open the terminal in your project’s root directory and run the command:

composer require spatie/laravel-newsletter

After installing the library, run the command below to publish the config file to config/newsletter.php.

php artisan vendor:publish --provider="Spatie\Newsletter\NewsletterServiceProvider"

Head over to the ‘config’ directory and open the file ‘newsletter.php’.

newsletter.php

<?php

return [

    /*
     * The API key of a MailChimp account. You can find yours at
     * https://us10.admin.mailchimp.com/account/api-key-popup/.
     */
    'apiKey' => env('MAILCHIMP_APIKEY'),

    /*
     * The listName to use when no listName has been specified in a method.
     */
    'defaultListName' => 'subscribers',

    /*
     * Here you can define properties of the lists.
     */
    'lists' => [

        /*
         * This key is used to identify this list. It can be used
         * as the listName parameter provided in the various methods.
         *
         * You can set it to any string you want and you can add
         * as many lists as you want.
         */
        'subscribers' => [

            /*
             * A MailChimp list id. Check the MailChimp docs if you don't know
             * how to get this value:
             * http://kb.mailchimp.com/lists/managing-subscribers/find-your-list-id.
             */
            'id' => env('MAILCHIMP_LIST_ID'),
        ],
    ],

    /*
     * If you're having trouble with https connections, set this to false.
     */
    'ssl' => true,

];

You don’t need to do any changes here except ‘ssl’ value. If you don’t have an SSL certificate on your server then set this value to false. You may seen the constants MAILCHIMP_APIKEY and MAILCHIMP_LIST_ID in the above code. It means you need to add these constants with actual values in your .env file.

.env

....

MAILCHIMP_APIKEY=YOUR_MAILCHIMP_API_KEY
MAILCHIMP_LIST_ID=YOUR_MAILCHIMP_LIST_ID

After this clear the configuration cache using the command:

php artisan config:clear

Integrate MailChimp Newsletter in Laravel Application

You have installed and configured the library. Now you are good to go ahead with MailChimp integration on your Laravel website. Let’s create a simple newsletter form in your view file.

<form action="{{ url('ROUTE_HERE') }}" method="post">
    <div class="form-group">
        <label for="exampleInputEmail">Email</label>
        <input type="email" name="user_email" id="exampleInputEmail" class="form-control">
    </div>
    {{ csrf_field() }}
    <button type="submit" class="btn btn-primary">Submit</button>
</form>

Make sure to replace the placeholder ROUTE_HERE with the actual value. Next, in your controller file, add the facade of the installed package.

use Newsletter;

In the controller’s function where you are posting the form write the code as follows:

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Newsletter;

....

public function store(Request $request)
{
    if ( ! Newsletter::isSubscribed($request->user_email) ) {
        Newsletter::subscribe($request->user_email);
    }
}
?>

Here, I am first checking whether a user is already added to the subscriber list or not. If not, then add it to the subscriber list. I used the method subscribe() which directly subscribe the user to the list. If you need to send a confirmation email to the user before subscribing to the list then replace the below line

Newsletter::subscribe($request->user_email);

with

Newsletter::subscribePending($request->user_email);

subscribePending() function sets a user status to ‘pending’ in the MailChimp list until the user confirms the subscription.

Mailchimp also provides the Audience fields which store extra information about users directly inside the MailChimp dashboard. Just in case, if you are looking to add Audience fields then you can do it by adding one more parameter in the above method. Here, I am adding values for the default Audience fields FNAME and LNAME.

Newsletter::subscribe($request->user_email, ['FNAME'=>'ENTER_FIRST_NAME', 'LNAME'=>'ENTER_LAST_NAME']);

If you want to add tags for the subscriber then use the addTags() method. Pass the multiple elements in an array in case you need to assign multiple tags. In my case, I am assigning the ‘Blogger’ tag to the subscriber.

Newsletter::addTags(['Blogger'], $request->user_email);

The user can delete the subscriber from list using the code:

Newsletter::delete('SUBSCRIBER_EMAIL');

These are the basic methods normally required on the website. You may also want to check additional functions provided by this package on GitHub.

I hope you understand how to integrate the MailChimp newsletter to your Laravel website. Please share your thoughts and suggestions in the comment section below.

Related Articles

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

12 thoughts on “How to Integrate MailChimp Newsletter in Laravel Application

  1. hello
    i wanted to know what if i also need to add phone number and whatsapp number to subscription list apart from email and name?
    could you help me?

    1. Create new Audience fields for your numbers in the Mailchimp dashboard like FNAME AND LNAME. After that you can pass it in key=>value pair from the code.

  2. Hi,
    i’im using Laravel 8 and it’s doesn’t work for me.
    I have an error “Class Newsletter not found” when NewsLetterController is call.
    Do you have an idear, please ?
    Stéphane

Leave a Reply

Your email address will not be published. Required fields are marked *