How To Integrate Mailchimp API In Laravel 9

WebSolutionStuff | Aug-01-2022 | Categories : Laravel

In this article, we will see how to integrate mailchimp API in laravel 9. Here we will learn how we can integrate Mailchimp API into our laravel 9 application. Mailchimp provides us manage subscribers, send emails by using campaigns and also track the email results, etc. By using Mailchimp we can track how many subscribers are open to email and read.

We will also use skovmand/mailchimp-laravel laravel package for mailchimp API in the laravel 9 application. A minimal service provider to set up and use the Mailchimp API v2 PHP library in Laravel.

So, let's see mailchimp integration in laravel 9 or laravel 9 mailchimp API integration.

Step 1: Create Mailchimp Account

In this step, we will create a new account from here: Create New Account. You have to create a new list, click on Lists on the menu and create a new list. After creating successful lists then select your list, go to settings->List name and defaults and copy your list id, we will use it on API.

Now, we can get API Key so click here and get the API key: API Key

Open your .env file and paste it in the .env file.

APP_ENV=local
APP_DEBUG=true
APP_KEY=

DB_HOST=127.0.0.1
DB_DATABASE=websolution
DB_USERNAME=root
DB_PASSWORD=root

MAILCHIMP_APIKEY=your_mailchimp_api_key
MAILCHIMP_LIST_ID=your_mailchimp_list_id

 

 

Step 2: Install Mailchimp Package

Now, will install mailchimp package using the composer command.

composer require skovmand/mailchimp-laravel

 

Step 3: Install The Newsletter Package

After installing mailchimp, we will install the news-letter package using the composer command.

composer require spatie/laravel-newsletter

To publish the config file to config/newsletter.php run:

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

 

 

Step 4: Use MAILCHIMP API KEY and MAILCHIMP LIST ID

In this step, we will use mailchimp API key and list ID in the config/newsletter.php file

<?php
return [
/*
* The driver to use to interact with MailChimp API.
* You may use "log" or "null" to prevent calling the
* API directly from your environment.
*/
'driver' => env('MAILCHIMP_DRIVER', 'api'),
/*
* 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'),
/*
* The GDPR marketing permissions of this audience.
* You can get a list of your permissions with this command: "php artisan newsletter:permissions"
*/
'marketing_permissions' => [
// 'email' => '',
// 'customized_online_advertising' => '',
],
],
],
/*
* If you're having trouble with https connections, set this to false.
*/
'ssl' => true,
];

 

Step 5: Create a NewsletterController

Now, we will create a controller using the below command.

php artisan make:controller NewsletterController

And add the following code to the controller.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Newsletter;

class NewsletterController extends Controller
{
    public function create()
    {
        return view('newsletter');
    }

    public function store(Request $request)
    {
        if (!Newsletter::isSubscribed($request->email) ) 
        {
            Newsletter::subscribePending($request->email);
            return redirect('newsletter')->with('success', 'Thanks For Subscribe');
        }
        return redirect('newsletter')->with('failure', 'Sorry! You have already subscribed ');
            
    }}

 

 

Step 6: Create a blade file

In this step, we will create a blade file for view display.

resources/views/newsletter.blade.php

@extends('layouts.app')
@section('content')
<h2 class="text-center">How To Integrate Mailchimp API In Laravel 9 - Websolutionstuff</h2>
<div class="container">
@if ($message = Session::get('success'))
<div class="alert alert-success alert-block">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>{{ $message }}</strong>
</div>
@endif
@if ($message = Session::get('error'))
<div class="alert alert-danger alert-block">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>{{ $message }}</strong>
</div>
@endif
<div class="row">
<div class="col-md-5">
<div class="well">
{!! Form::open(array('route' => 'subscribe')) !!}
<div>
<h3 class="text-center">Subscribe Your Email</h3>
<input class="form-control" name="email" id="email" type="email" placeholder="Your Email" required>
<br/>
<div class="text-center">
<button class="btn btn-info btn-lg" type="submit">Subscribe</button>
</div>
</div>
{!! Form::close() !!}
</div>
</div>
</div>
@endsection

 

Step 7: Add routes

Add route in web.php file

Route::get('newsletter','[email protected]');
Route::post('newsletter','[email protected]');

 


You might also like:



Recommended Post


Featured Post







Follow us
facebooklogo github instagram twitter

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock or some other adblocking software which is preventing the page from fully loading.

We need money to operate the site, and almost all of it comes from our online advertising.

Please add websolutionstuff to your ad blocking whitelist or disable your adblocking software.

×