Create Newsletter in Laravel

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="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">  
 </head>
 <body>
  <div >
  @if (\Session::has('success'))
   <div >
    <p>{{ \Session::get('success') }}</p>
   </div><br />
   @endif
   @if (\Session::has('failure'))
   <div >
    <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 >
     <div ></div>
      <div >
       <label for="Email">Email:</label>
       <input type=text name=email>
      </div>
     </div>
    <div >
     <div ></div>
     <div >
      <button type=submit >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.