AppDividend
Latest Code Tutorials

How To Create Newsletter in Laravel

Laravel Newsletter Tutorial

1,446

Get real time updates directly on you device, subscribe now.

How To Create Newsletter in Laravel is the topic, we will discuss today. In this example, we use laravel 5.6. We use spatie/laravel-newsletter package for creating a newsletter in laravel. MailChimp is an email marketing service which provides us to send newsletters to our subscribers. Top bloggers and marketers use professional email marketing services to send email newsletters. Email newsletters can help your business attach to customers.

How To Create Newsletter in Laravel

We are going to Configure Laravel Project.

Step 1: Download Laravel Project

Establish  Laravel Project by the typing following command.

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

Step 2: Install Newsletter Package

First, install 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 have already account then sign in.

Step 4: Get MailChimp API Key And List Id

After successfully sign up or sign we can get api key and list id. So follow this image.

Laravel Newsletter Tutorial

You can see 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 now we can get list id so follow below image to get list id.

 

How To Integrate Newsletter in Laravel

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

How to Generate Newsletter in Laravel

Step 5: Set API Key And List Id in .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 this 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 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 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 below image like mail.

laravel 5.6 newsletter tutorial

Finally, Our How To Create Newsletter in Laravelis over. Thanks for taking.

Leave A Reply

Your email address will not be published.

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