In this tutorial, we will tell you how to integrate Mailchimp API in your Laravel application. Mailchimp provides manage subscribers, sends emails using the campaign, and also tracks email results. Also, Mailchimp can track through you how many subscribers open and read emails. If you have a newsletter website or a tutorial website, you should add an email subscriber function in a way that we can notify via email.
We use Skovmand/Mailchimp-Laravel Laravel package for Mailchimp API in the Laravel application. In this example, we can subscriber email and send a campaign from the Laravel application from scratch, so if you don’t know how to do then it’s very simple to do. you can see the bellow preview after finish a few steps and you will see like bellow preview on your Laravel website.
Step 1: Create MailChimp Account Setting
If you are from scratch, And you don’t have an account then you can create a new account from here: Create a new account.
So let’s, now you have to create a new list, click on Lists on the menu and create a new list. Then After create successfully your lists then select your list from, got to settings-> Your List name and defaults, and copy your list id, use it on API.
Get API Key so click here : API Key.
Then, Open .env file and paste here
.env
APP_ENV=local
APP_DEBUG=true
APP_KEY=HZoXrOanofcjLSbmF67kVtJyVHMFE3XU
DB_HOST=127.0.0.1
DB_DATABASE=learn
DB_USERNAME=root
DB_PASSWORD=root
MAILCHIMP_API_KEY=API Key Here
Read Also: How To Create Custom Helper Function In Laravel
Step 2: Install Package
In this second step, you will install Skovmand/Mailchimp-Laravel package to use MailChimp API methods. so run this command in your cmd or terminal.
composer require skovmand/mailchimp-laravel
You need to add the provider path and alias path in the config/app.php file so open that file and add bellow code.
config/app.php
return [
......
$provides => [
......
......,
Skovmand\Mailchimp\MailchimpServiceProvider::class,
],
.....
]
Step 3: Add Route
In this third step, You will add a new three routes for creating a small example that way you can understand very well. one for layout, the second one for subscribing, and the last one for the send campaign. so first you add bellow route in your routes.php file.
app/Http/routes.php
Route::get('manageMailChimp', 'MailChimpController@manageMailChimp');
Route::post('subscribe',['as'=>'subscribe','uses'=>'MailChimpController@subscribe']);
Route::post('sendCompaign',['as'=>'sendCompaign','uses'=>'MailChimpController@sendCompaign']);
Read Also: How To Create 404 Error Page In Laravel
Step 4: Add Controller
In this fourth step, you will add the MailChimpController file to your example. Then you can copy the entire controller file and then you can just copy and paste.
app/Http/Controllers/MailChimpController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
use Config;
class MailChimpController extends Controller
{
public $mailchimp;
public $listId = '0e5ec5601as';
public function __construct(\Mailchimp $mailchimp)
{
$this->mailchimp = $mailchimp;
}
public function manageMailChimp()
{
return view('mailchimp');
}
public function subscribe(Request $request)
{
$this->validate($request, [
'email' => 'required|email',
]);
try {
$this->mailchimp
->lists
->subscribe(
$this->listId,
['email' => $request->input('email')]
);
return redirect()->back()->with('success','Email Subscribed successfully');
} catch (\Mailchimp_List_AlreadySubscribed $e) {
return redirect()->back()->with('error','Email is Already Subscribed');
} catch (\Mailchimp_Error $e) {
return redirect()->back()->with('error','Error from MailChimp');
}
}
public function sendCompaign(Request $request)
{
$this->validate($request, [
'subject' => 'required',
'to_email' => 'required',
'from_email' => 'required',
'message' => 'required',
]);
try {
$options = [
'list_id' => $this->listId,
'subject' => $request->input('subject'),
'from_name' => $request->input('from_email'),
'from_email' => 'hardik@discoverprograming.com',
'to_name' => $request->input('to_email')
];
$content = [
'html' => $request->input('message'),
'text' => strip_tags($request->input('message'))
];
$campaign = $this->mailchimp->campaigns->create('regular', $options, $content);
$this->mailchimp->campaigns->send($campaign['id']);
return redirect()->back()->with('success','send campaign successfully');
} catch (Exception $e) {
return redirect()->back()->with('error','Error from MailChimp');
}
}''
}
Step 5: Add Blade file
In this last step, you have to just create a new blade file mailchimp.blade.php and put the bellow code on that file.
resources/views/mailchimp.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 class="col-md-7">
<div class="well well-sm">
{!! Form::open(array('route' => 'sendCompaign','class'=>'form-horizontal')) !!}
<fieldset>
<legend class="text-center">Send Campaign</legend>
<!-- Name input-->
<div class="form-group">
<label class="col-md-3 control-label" for="name">Subject</label>
<div class="col-md-9">
<input id="name" name="subject" type="text" placeholder="Your Subject" class="form-control">
</div>
</div>
<!-- Email input-->
<div class="form-group">
<label class="col-md-3 control-label" for="email">To</label>
<div class="col-md-9">
<input id="email" name="to_email" type="text" placeholder="To " class="form-control">
</div>
</div>
<!-- From Email input-->
<div class="form-group">
<label class="col-md-3 control-label" for="email">From</label>
<div class="col-md-9">
<input id="email" name="from_email" type="text" placeholder="From " class="form-control">
</div>
</div>
<!-- Message body -->
<div class="form-group">
<label class="col-md-3 control-label" for="message">Your message</label>
<div class="col-md-9">
<textarea class="form-control" id="message" name="message" placeholder="Please enter your message here..." rows="5"></textarea>
</div>
</div>
<!-- Form actions -->
<div class="form-group">
<div class="col-md-12 text-right">
<button type="submit" class="btn btn-primary btn-lg">Send Campaign</button>
</div>
</div>
</fieldset>
{!! Form::close() !!}
</div>
</div>
</div>
</div>
@endsection
Read Also: Laravel Notification Alert Using Notify Plugin
I hope it will help you…