Laravel Mailchimp api integration from scratch with example

Laravel Mailchimp api integration from scratch with example

In this post we will give you information about Laravel Mailchimp api integration from scratch with example. Hear we will give you detail about Laravel Mailchimp api integration from scratch with exampleAnd how to use it also give you demo for it if it is necessary.

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 => [

......

......,

SkovmandMailchimpMailchimpServiceProvider::class,

],

.....

]

Also see:How to set gmail configration for mail in Laravel?

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 AppHttpControllers;


use IlluminateHttpRequest;

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@onlinecode',

'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

Also see:How to send mail using mailable in laravel 5.3?

@extends('layouts.app')


@section('content')

<h2 >MailChimp API Example</h2>

<div >


@if ($message = Session::get('success'))

<div >

<button type="button" data-dismiss="alert">×</button>

<strong>{{ $message }}</strong>

</div>

@endif


@if ($message = Session::get('error'))

<div >

<button type="button" data-dismiss="alert">×</button>

<strong>{{ $message }}</strong>

</div>

@endif


<div >

<div >

<div >

{!! Form::open(array('route' => 'subscribe')) !!}

<div>

<h3 >Subscribe Your Email</h3>

<input name="email" id="email" type="email" placeholder="Your Email" required>

<br/>

<div >

<button type="submit">Subscribe</button>

</div>

</div>

{!! Form::close() !!}

</div>

</div>

<div >

<div >

{!! Form::open(array('route' => 'sendCompaign','class'=>'form-horizontal')) !!}

<fieldset>

<legend >Send Campaign</legend>


<!-- Name input-->

<div >

<label for="name">Subject</label>

<div >

<input id="name" name="subject" type="text" placeholder="Your Subject" >

</div>

</div>


<!-- Email input-->

<div >

<label for="email">To</label>

<div >

<input id="email" name="to_email" type="text" placeholder="To " >

</div>

</div>


<!-- From Email input-->

<div >

<label for="email">From</label>

<div >

<input id="email" name="from_email" type="text" placeholder="From " >

</div>

</div>


<!-- Message body -->

<div >

<label for="message">Your message</label>

<div >

<textarea id="message" name="message" placeholder="Please enter your message here..." rows="5"></textarea>

</div>

</div>


<!-- Form actions -->

<div >

<div >

<button type="submit" >Send Campaign</button>

</div>

</div>

</fieldset>

{!! Form::close() !!}

</div>

</div>

</div>

</div>

@endsection

Try this..

Hope this code and post will helped you for implement Laravel Mailchimp api integration from scratch with example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

7  +  3  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US