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.
Here's a step-by-step guide on how to create a newsletter in Laravel 10:
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
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
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"
Sign Up on MailChimp:
Log in to MailChimp:
Navigate to Dashboard:
Access API Keys:
Generate an API Key:
In your .env
file, set the following environment variables
MAILCHIMP_APIKEY=your_mailchimp_api_key
MAILCHIMP_LIST_ID=your_mailchimp_list_id
In your web.php
routes file, add a route to preview your newsletter:
Route::get('newsletter','NewsletterController@index');
Route::post('newsletter/store','NewsletterController@store');
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');
}
}
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>
Run your Laravel development server:
php artisan serve
You might also like:
In this post i will show you datatables localization example, jquery datatable plugin can localize a...
May-10-2021
In this article, we will see how to create a multi-language website in laravel. In this example, you can understand...
Nov-09-2020
Welcome to my guide on creating a secure and visually appealing user authentication system in Laravel 10! Laravel, one o...
Aug-23-2023
In this article, we will give you information about the basic route, named route, and advanced route in laravel 7 and la...
Nov-01-2020