How to Create Newsletter in Laravel 10

Websolutionstuff | Nov-29-2023 | Categories : Laravel

Hey there! In this article, I'm thrilled to guide you through the exciting world of creating newsletters in Laravel 10. As we navigate this together, you'll discover how to effortlessly design, compose, and send newsletters that captivate your audience.

Let's dive into the simplicity and effectiveness of crafting newsletters using Laravel 10, making communication a breeze!

Also, you can use in newsletter in Laravel 8, Laravel 9, and Laravel 10.

newsletter-laravel-10

Here's a step-by-step guide on how to create a newsletter in Laravel 10:

Step 1: Set Up a New Laravel 10 Project

If you don't have a Laravel 10 project yet, create one using the following command in your terminal:

composer create-project laravel/laravel newsletter-laravel-10

 

Step 2: Configure Database

Open the .env file in your project and set up your database configuration.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=Enter_your_Database_Name
DB_USERNAME=Enter_Your_Database_Username
DB_PASSWORD=Enter_Your_Database_Password

 

Step 3: Install Newsletter Package

Run the following command to install the Laravel Newsletter package using Composer:

composer require spatie/laravel-newsletter

Note: This is for older laravel version

After the package is installed, open the config/app.php file.

Add the following service provider to the providers array:

'providers' => [
    // Other providers...

    Spatie\Newsletter\NewsletterServiceProvider::class,
],

'aliases' => [
    // Other aliases...

    'Newsletter' => Spatie\Newsletter\NewsletterFacade::class,
],

Run the following command to publish the configuration file:

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

 

Step 4: Sign Up in MailChimp, Get the MailChimp API Key and List the ID
  1. Sign Up on MailChimp:

    • If you don't have a MailChimp account, visit the MailChimp website and sign up for a free account.
  2. Log in to MailChimp:

    • Log in to your MailChimp account using your credentials.
  3. Navigate to Dashboard:

    • After logging in, go to the MailChimp dashboard.
  4. Access API Keys:

    • Click on your account name in the top right corner and select "Account."
    • Under the "Extras" dropdown, choose "API keys."
  5. Generate an API Key:

    • If you don't have an API key, generate a new one. Copy the generated key; you will need it to configure the Laravel Newsletter package.

 

Step 5: Set Environment Variables:

In your .env file, set the following environment variables

MAILCHIMP_APIKEY=your_mailchimp_api_key
MAILCHIMP_LIST_ID=your_mailchimp_list_id

 

 

Step 6: Create a Route for Newsletter Preview

In your web.php routes file, add a route to preview your newsletter:

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

 

Step 7: Create a Controller

Now, create a NewsletterController using the following command:

php artisan make:controller NewsletterController

app/http/controller/NewsletterController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Newsletter;

class NewsletterController extends Controller
{
    public function index()
    {
        return view('news-letter');
    }

    public function store(Request $request)
    {
        if (!Newsletter::isSubscribed($request->email) ) 
        {
            Newsletter::subscribePending($request->email);
            return redirect('newsletter')->with('success', 'Thanks For Subscribe Our Website');
        }
        return redirect('newsletter')->with('failure', 'Sorry! You have already subscribed');
            
    }
}

 

Step 8: Create Blade File

Navigate to the resources/views directory. create a new Blade file. For example, let's name it news-letter.blade.php

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>How to Create Newsletter in Laravel 10 - Websolutionstuff</title>
		<link rel="stylesheet" href="{{asset('css/app.css')}}">
		<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">  
	</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 class="mb-2 mt-2">How to Create Newsletter in Laravel 10 - Websolutionstuff</h2>
			<form method="post" action="{{url('newsletter/store')}}">
				@csrf		
				<div class="row">
					<div class="col-md-8"></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 9: Test the Newsletter Preview

Run your Laravel development server:

php artisan serve

 


You might also like:

Recommended Post
Featured Post
How To Create Line Chart In Laravel 9 Using Highcharts
How To Create Line Chart In La...

In this article, we will see how to create a line chart in laravel 9 using highcharts. A line chart is a graph...

Read More

Oct-04-2022

Line Breaks In Laravel Blade
Line Breaks In Laravel Blade

In this post, I will show you how to break lines for Textarea in laravel blade. Many times when you save...

Read More

Jul-26-2020

How to Install Material Theme in Angular 17
How to Install Material Theme...

In this article, we'll install the material theme in angular 17. Material Theme brings a polished design and us...

Read More

Mar-29-2024

Laravel 8 Google Pie Chart Example
Laravel 8 Google Pie Chart Exa...

This article will show the laravel 8 google pie chart example. Google charts use to visualize data on you...

Read More

Mar-03-2021