Mailchimp Integration with Laravel

Introduction
In this tutorial, you can see how you can work on the MailChimp integration in PHP, with the use of Laravel to be precise. Here we will learn how we can integrate Mailchimp API into our Laravel 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. If we have a newsletter website or any other tutorial website then we should add the email subscriber function that way we can inform through email.
Prerequisite
1. Laravel application needs to be set up.
2. Mailchimp-Laravel package for Laravel application.
Creating a Mailchimp Account
Steps-
- You can 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 here this way:
.env
APP_ENV=local
APP_DEBUG=true
APP_KEY=DB_HOST=127.0.0.1
DB_DATABASE=learn
DB_USERNAME=root
DB_PASSWORD=root#mail chimp api key and list id
MAILCHIMP_APIKEY=
MAILCHIMP_LIST_ID=
5. Install Mailchimp Package
composer require skovmand/mailchimp-laravel
6. Install the Newsletter package
composer require spatie/laravel-newsletter
To publish the config file to config/newsletter.php
run:
php artisan vendor:publish --provider="Spatie\Newsletter\NewsletterServiceProvider"
7. Use MAILCHIMP_APIKEY and MAILCHIMP_LIST_ID in the config/newsletter.php file
<?phpreturn [/** 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,];
7. Create a NewsletterController by using the following command:
php artisan make:controller NewsletterController
And add the following code to the controller.
//NewsletterController.php
<?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 ');
}}
8. Create blade file to display subscription form-
resources/views/newsletter.blade.php
@extends('layouts.app')@section('content')<h2 class="text-center">MailChimp API Example</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
9. Add route in web.php file
Route::get('newsletter','NewsletterController@create');
Route::post('newsletter','NewsletterController@store');
Well done….! Happy Learning!!!
Conclusion
In this article, you learned the work on Mailchimp in PHP, with the use of Laravel in detail.
That’s all! I hope you enjoyed reading this article and learned something new. Do share this article if you find it useful.
Follow me- https://medium.com/@dipak.b_39701 and my team- https://medium.com/nonstopio to know what we are building secretly….
Reference-
- https://medium.com/@arcomito/grabbing-your-mailchimp-api-key-and-list-id-for-mailchimp-integration-22ef4b20792f
- https://www.itsolutionstuff.com/post/laravel-5-mailchimp-api-integration-from-scratch-with-exampleexample.html
- https://codingspoint.com/how-to-mailchimp-api-integration-in-laravel/
- https://onlinecode.org/laravel-mailchimp-api-integration-from-scratch-with-example-2/#google_vignette