How to Create Newsletter in Laravel

To create a newsletter in Laravel, use the “spatie/laravel-newsletter” package for creating a newsletter in laravel. MailChimp is an email marketing service that lets us send newsletters to our subscribers.

Here is the step-by-step guide to creating a newsletter in Laravel.

Step 1: Download Laravel Project

Establish  Laravel Project by typing the following command.

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

Step 2: Install Newsletter Package

Install the Newsletter via the Composer package manager.

composer require spatie/laravel-newsletter 

The package will automatically register itself.

To publish the config file to config/newsletter.php run:

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

Step 3: Sign Up in MailChimp

First, you sign up in MailChimp. If you already have an account, then sign in.

Step 4: Get MailChimp API Key And List Id

After successfully signing up or signing i,n we can get api key and list id. So follow this image.

Laravel Newsletter Tutorial

You can see the below screen. Then move to Extras >> API keys.

Laravel Newsletter Example

Under the Your API keys section, tick on Create A Key and copy your API key, which we want in a minute.

laravel newsletter mailchimp tutorial

After successfully generate api key, we can now get the list id, so follow the below image to get the list id.

 

How To Integrate Newsletter in Laravel

Click on the List name and defaults, and you get to list id in the below image.

How to Generate Newsletter in Laravel

Step 5: Set API Key And List-Id in the .env file

Next, we set up api key and id in the .env file.

//.env

MAILCHIMP_APIKEY=xxxx
MAILCHIMP_LIST_ID=xxxx

Step 6: Create a view file

Create a file in resources  >>  views  >>   newsletter.blade.php and put the following code in it.

<!-- newsletter.blade.php -->

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Laravel Newsletter Tutorial With Example</title>
    <link rel="stylesheet" href="{{asset('css/app.css')}}">
    <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">Submit</button>
          </div>
        </div>
      </form>
    </div>
  </body>
</html>

Step 7: Create a Controller and route

php artisan make:controller NewsletterController 

It will build a controller file called NewsletterController.php.

Add the following code to the controller.

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

We register the route in a web.php file.

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

Laravel Newsletter Tutorial with Example

You can see that alert message Thanks For Subscribe then you move to your mail and see the inbox. You have to look at the below image like mail.

laravel 5.6 newsletter tutorial

That’s it for this tutorial. Thanks for taking it.

4 thoughts on “How to Create Newsletter in Laravel”

  1. Thank you, this is great article.
    I have small question hope you can help, I want create a small newsletter system build-in Laravel app without Mailchimp or other Mail provider, do you have experience on that?

    Thanks,
    Duong

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.