Laravel Mailchimp API Integration Example
In this post you can learn how to integrate mailchimp api in your laravel 5 application. Mailchimp provides manage subscribers, send emails using campaign and also track email results etc. Mailchimp through you can track how much subscribers open email and read. If you have newsletter website or any tutorial website then you should add email subscriber function that way we can inform through email.
I use skovmand/mailchimp-laravel laravel package for mailchimp api in laravel 5 application. In this example you can subscriber email and send campaign from laravel application from scratch, so if you don't know how to do then it's very simple to do. you can see bellow perview after finish few step and you will see like bellow preview in your laravel 5 website.
Preview:
Step 1: Create MailChimp Account Setting
If you are from scratch, i mean if you don't have account then you can create new account from here : Create New Account.
Ok, now you have to create new List, click on Lists on menu and create new list. After create successfully lists then select your list, got 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 api key : API Key
Open your .env file and paste here this way:
.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
Step 2: Install Package
In this step we will install skovmand/mailchimp-laravel package for use MailChimp api methods. so first fire bellow command in your cmd or terminal:
composer require skovmand/mailchimp-laravel
Now we need to add provider path and alias path in 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 step we will add new three route for creating small example that way we can undestand very well. one for layout, second one for subscribe and last one for send campaign. so first 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']);
Step 4: Add Controller
In this step we will add MailChimpController file for our example. you can copy whole controller file then also 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@itsolutionstuff.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
This is a last step and you have to just create new blade file mailchimp.blade.php and put 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
Try this..
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel 10 Mail | Laravel 10 Send Mail Tutorial
- Laravel Change Mail Driver Dynamically Example
- Laravel Mail Send with PDF Attachment Example
- How to set CC And BCC Email Address In Laravel Mail?
- Laravel 9 Database Seeder Tutorial Example
- Laravel 9 Send Mail using Gmail SMTP Server
- Laravel Search Case Insensitive Query Example
- Laravel Send Mail using Mailtrap Example
- How to send mail using mailable in laravel 5.3?
- How to Send Mail using Sendgrid in Laravel?
- How to Send Mail in PHP Laravel?
- Laravel Mailgun Setup Example
- How to create Event for Mail sending in Laravel 5.2?
- How to Send Mail using Gmail SMTP in Laravel?