Do you want to integrate the Mailchimp newsletter into your Laravel application? Mailchimp is an email marketing service that allows sending newsletters to your subscribers. In this article, I show you how to add the Mailchimp newsletter to the Laravel website.
Mailchimp is one of the popular email marketing services, which manages the subscribers of your website. Using Mailchimp, you can send a newsletter about new content, announcements, deals, and many more to your subscribers. Mailchimp makes the process automated so you don’t need to remember of sending the newsletter. Mailchimp handles it on its own, which saves us a lot of time.
If you are looking to add a Mailchimp newsletter in plain PHP then check out the article Mailchimp integration using MailChimp API and PHP.
We are going to use the Mailchimp API to manage the subscribers. It requires you to grab your API key and Audience ID.
Get Mailchimp API Key and Audience ID
Follow the steps below to get these credentials.
Login to your Mailchimp account. Under the user icon, select Account.
Click on Extra->API keys.
Under the Your API keys section, click on Create A Key and copy your API key which we require in a moment.
Now you have your API key ready. Next, get an Audience ID where your subscribers will be added. For this, click on the Audience menu and then select the option Settings from the Manage Audience drop-down.
Under the Settings click on the ‘Audience name and defaults’.
On the next page, you will find your Audience ID.
Installation of Laravel Newsletter Library
A Spatie has built a fantastic library called 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"
Open the config/newsletter.php
file.
<?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 make any changes here except for ‘ssl’ value. If you don’t have the SSL certificate on your server then set this value to false
. You may notice the constants MAILCHIMP_APIKEY and MAILCHIMP_LIST_ID. These constants should add to the .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 blade file.
<form action="{{ url('ROUTE_HERE') }}" method="post">
@csrf
<p><input type="email" name="user_email" placeholder="Enter Email" /></p>
<button type="submit" name="submit">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 Mailchimp list or not. If not, then add it to the subscriber list. I used the method subscribe()
which directly subscribes the user to the list. If you want 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);
The subscribePending()
function sets a user status to ‘pending’ in the Mailchimp list until the user confirms the subscription.
Mailchimp also provides the Audience fields to store extra information about users. Just in case, if you are looking to add Audience fields, 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 wish to add tags for the subscriber then use the addTags()
method. Pass the multiple elements in an array 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 a list using the code:
Newsletter::delete('SUBSCRIBER_EMAIL');
These are the few methods required for newsletter integration. You may also want to check additional methods provided by this package on GitHub.
I hope you understand how to integrate the Mailchimp newsletter into your Laravel website. Please share your thoughts and suggestions in the comment section below.
Related Articles
- Mailchimp Integration with Contact Form 7 Plugin
- Mailchimp Integration in WordPress Without Using a Plugin
If you liked this article, then please subscribe to our YouTube Channel for video tutorials.
Is there another way to addTags?
Newsletter::addTags([‘Blogger’], $request->user_email);
no longer works
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?
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.
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
replace
use newsletter;
with :
use Spatie\Newsletter\NewsletterFacade as Newsletter;
Which parameter I should use to assign tag to the email subscriber.
I updated the article. A code is added that assigns tags to subscribers.
every thing is working fine but not getting email subscription in contacts
hi noman..how did you solve the issue I am experiencing the same here
hello i have do what you post but i have an error : invalide API Key
After adding credentials in .env file clear the configuration cache first.
from where I manage my subscriber list?
You can get members of a list using
Newsletter::getMembers();
Additionally, please refer a library documentation https://github.com/spatie/laravel-newsletterI have written the article using this library.
Did you get the solution to your question? If yes then please let me know, I need it as well.
i have followed these steps but i have got the error “404: The requested resource could not be found.”
Did you create a route and replace form action URL?