Skip to content

Instantly share code, notes, and snippets.

@manuelgeek
Last active February 13, 2019 23:32
Show Gist options
  • Save manuelgeek/3d30526adba831c1bc3d706dd87e30ad to your computer and use it in GitHub Desktop.
Save manuelgeek/3d30526adba831c1bc3d706dd87e30ad to your computer and use it in GitHub Desktop.
a simple subscription code with Laravel 5.6 and MailChimp
add this line
return [
......
$provides => [
......
......,
Skovmand\Mailchimp\MailchimpServiceProvider::class,
],
.....
]
<?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' => 'Test Name',
'from_email' => '[email protected]',
'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');
}
}
}
@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')) !!} --}}
<form action="{{route('subscribe')}}" method="post">
@csrf
<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>
{{-- {!! Form::close() !!} --}}
</div>
</div>
<div class="col-md-7">
<div class="well well-sm">
{{-- {!! Form::open(array('route' => 'sendCompaign','class'=>'form-horizontal')) !!} --}}
<form action="{{route('sendCompaign')}}" method="post" class="form-horizontal">
@csrf
<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() !!} --}}
</form>
</div>
</div>
</div>
</div>
@endsection
Mailchimp provides manage subscribers, send emails using campaign and also track email results
If you have newsletter website or any tutorial website then you should add email subscriber function that way we can inform
through email
create a new laravel project
composer create-project --prefer-dist laravel/laravel subscription
If you are from scratch, i mean if you don't have account then you can create new account from
here : Create New Account.https://mailchimp.com/
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. https://us11.admin.mailchimp.com/account/api/
Open your .env file and paste here this way:
MAILCHIMP_API_KEY=API Key Here
In this step we will install skovmand/mailchimp-laravel package for use MailChimp api methods
composer require skovmand/mailchimp-laravel
The provider will be autodiscoverd in laravel 5.6 or 5.5
make controller app/Http/Controllers/MailChimpController.php
php artisan make:controller MailChimpController
add these to routes.php
Route::get('manageMailChimp', 'MailChimpController@manageMailChimp');
Route::post('subscribe',['as'=>'subscribe','uses'=>'MailChimpController@subscribe']);
Route::post('sendCompaign',['as'=>'sendCompaign','uses'=>'MailChimpController@sendCompaign']);
MailchimpController.php
lets do php artisan vendor:publish and publish mailchimp config file
make our view inresources/views/mailchimp.blade.php
run php artisan make:auth, my view is extending app layout, and currently we dont have it in our resources
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment