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
Datatables Localization Example
Datatables Localization Exampl...

In this post i will show you datatables localization example, jquery datatable plugin can localize a...

Read More

May-10-2021

How to Create Multi Language Website in Laravel
How to Create Multi Language W...

In this article, we will see how to create a multi-language website in laravel. In this example, you can understand...

Read More

Nov-09-2020

Laravel 10 Login and Registration with Auth
Laravel 10 Login and Registrat...

Welcome to my guide on creating a secure and visually appealing user authentication system in Laravel 10! Laravel, one o...

Read More

Aug-23-2023

Routing - Laravel 7/8 Routing Example
Routing - Laravel 7/8 Routing...

In this article, we will give you information about the basic route, named route, and advanced route in laravel 7 and la...

Read More

Nov-01-2020