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 Add Ckeditor In Laravel
How To Add Ckeditor In Laravel

In this article, I will explain you how to add ckeditor in laravel, CKEditor is a WYSIWYG rich text edito...

Read More

Jun-18-2020

PHP Access Modifiers Example
PHP Access Modifiers Example

In this example we will see PHP access modifiers example. In PHP default access modifier is public. PHP provide differen...

Read More

Sep-06-2021

Carbon Add Hours To Date In Laravel 9
Carbon Add Hours To Date In La...

In this article, we will see carbon add hours to date in laravel 9. Carbon provides addHour and addHours() function...

Read More

Nov-22-2022

How to Set Auto Database BackUp using Cron Scheduler In Laravel
How to Set Auto Database BackU...

In this article, we will see how to set auto database backup using the cron scheduler in laravel. here we will set ...

Read More

Feb-18-2021