nonstopio
Published in

nonstopio

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-

  1. You can create a new account from here: Create New Account.
  2. 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.
  3. Now we can get API Key so click here and get the API key: API Key
  4. 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-

  1. https://medium.com/@arcomito/grabbing-your-mailchimp-api-key-and-list-id-for-mailchimp-integration-22ef4b20792f
  2. https://www.itsolutionstuff.com/post/laravel-5-mailchimp-api-integration-from-scratch-with-exampleexample.html
  3. https://codingspoint.com/how-to-mailchimp-api-integration-in-laravel/
  4. https://onlinecode.org/laravel-mailchimp-api-integration-from-scratch-with-example-2/#google_vignette

--

--

--

A Bespoke Engineering Studio

Recommended from Medium

4 Things you can do right now to get the most out of Redis

Making Enemy avoid laser shooting at it

Spotluck uses Stitch to eliminate their custom Python scripts and replicate their MongoDB data to…

AWS Solutions Architect Job Interview Service based Questions

Warp WebSockets and Tokio Actors

Use Ansible to customize AWS EMR through bootstrap actions

9 innovative mobile apps built with Flutter framework

A Better Way to Artifact in Enterprise Architecture

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dipak Belsare

Dipak Belsare

Full Stack Developer

More from Medium

Laravel 8 Autocomplete Search from Database

Laravel Authentication | Laravel 8 Authentication using Jetstream Example

How to Install JWT Auth in Laravel Project

PayHere Laravel integration