Create Newsletter in Laravel 5.7

Generic placeholder image
Brijpal Sharma
laravel  

Date:-December 12th, 2018 , Posted On:- Web Development


Today topic is Create Newsletter in Laravel 5.7. we learn how to send the newsletter to subscribed visiter

In this tutorial, we use Laravel 5.7 and  Spatie/Laravel-newsletter package for creating the newsletter in Laravel 5.7. we also use a MailChimp marketing company service which provides to send newsletters to our subscribers.

The newsletter is a very effective way to send the newsletter to our fan visiter who subscribe to our website. most of the websites use this feature to call back visitors when something updated on the website.

So let's start...

Step:1 Download a new Laravel project

Install a new Laravel project using this following command. 

composer create-project --prefer-dist laravel/laravel newsletters

or 

laravel new newsletters

 

Install new Spatie/Laravel package

Install Spatie/Laravel package via composer using this following command.

composer require spatie/laravel-newsletter 

To publish the config file run this following command. config/newsletter.php

php artisan vendor:publish --provider="Spatie\Newsletter\NewsletterServiceProvider"

 

Step:2 Sign Up in MailChimp

We need to sign up in MailChimp if you are new user otherwise sign in.

 

After some validation and form fill up you will go on your dashboard. and you can get API key and list ID.

 

You can see the API key in Extras/API keys. in navbar.

In the API keys section, tick on Create A new Key and copy your API key which we use in Create Newsletter in Laravel 5.7.

 

After successfully generate API our key now we can get list id so follow the below image to get list ID.

Click on List name and defaults, button and you get to list Id.

 

Step: 3 Update .env file with the API key and ID

We update API key and id in the .env file.

//.env

MAILCHIMP_APIKEY=xxxx
MAILCHIMP_LIST_ID=xxxx

 

Step: 4 Create a new view file

newsletter.blade.php and put this following code in this file.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Create newsletter in laravel 5.7</title>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">    
  </head>
  <body>
    <div class="container">
    @if (\Session::has('success'))
      <div class="alert alert-success">
        <p>{{ \Session::get('success') }}</p>
      </div><br />
     @endif
     @if (\Session::has('failure'))
      <div class="alert alert-danger">
        <p>{{ \Session::get('failure') }}</p>
      </div><br />
     @endif
      <h2>Laravel Newsletter Tutorial With Example</h2><br/>
      <form method="post" action="{{url('newsletter')}}">
        @csrf
        </div>
        <div class="row">
          <div class="col-md-4"></div>
            <div class="form-group col-md-2">
              <label for="Email">Email:</label>
              <input type="text" class="form-control" name="email">
            </div>
          </div>
        <div class="row">
          <div class="col-md-4"></div>
          <div class="form-group col-md-4">
            <button type="submit" class="btn btn-success">Subscribe</button>
          </div>
        </div>
      </form>
    </div>
  </body>
</html>

 

Step:5 Create a new controller and route.

 

php artisan make:controller NewsLetterController  

 

In 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 ');
            
    }
}

And register some route in a web.php file.

Route::get('create-newsletter','NewsletterController@create');
Route::post('store-newsletter','NewsletterController@store');

 

When you submit your email for subscribing you will receive a mail on your email ID.

Finally, Our Create Newsletter in Laravel 5.7 tutorial is over.

 


About the author

user profile
Brijpal Sharma

Hello, My Name is Brijpal Sharma. I am a Web Developer, Professional Blogger and Digital Marketer from India. I am the founder of Codermen. I started this blog to help web developers & bloggers by providing easy and best tutorials, articles and offers for web developers and bloggers...

0 comments

You must be logged in to post a comment.