How to build newsletter subscriber system on Laravel 10

How to build newsletter subscriber system on Laravel 10

Posted on:September 5, 2023 at 05:00 PM

Newsletter section is one of the important parts to interact with visitor. In todays article, I will show you how to build a newsletter system on laravel 10.

What you need?

Table of Content


This is my plan:

  • User can able to submit email to /subscribe route. - Upon submission, a job will be dispatch to send email
  • In the email, user is able to verify email to /subscribe/<<hash>> route.
  • Send an welcome email.

Write Code

Let’s write code now.

Create table, model, settings, and migration

I want to create table for subscribers and model with Subscriber.

php artisan make:model Subscriber -m

It will create migration and model file together.

Update your table database/migrations/{***********}_create_subscribers_table.php


    public function up(): void
        Schema::create('subscribers', function (Blueprint $table) {

Make your model Subscriber.php guarded nothing.

class Subscriber extends Model
    use HasFactory;

+    protected $guarded = '';

Setup your database

In .env file:


Run migration In the terminal, run this:

php artisan migrate


We need at least two routes to achieve our goal.


use App\Http\Controllers\SubscribeController;

Route::post('/subscribe', [SubscribeController::class, 'post']);
Route::post('/subscribe/{hash}', [SubscribeController::class, 'show']);

Write Controller

In the controller, we need write logic now.

Create a form request

php artisan make:request NewsletterRequest 

Update your App\Http\Requests\NewsletterRequest

class NewsletterRequest extends FormRequest
    public function authorize(): bool
        return true;

    public function rules(): array
        return [
            'email' => 'email|unique:email|min:6|max:64'

Create & Update your controller now

php artisan make:controller SubscribeController 

Now, update the controller.

class SubscribeController extends Controller
    public function store(NewsletterRequest $request)
        $validated = $request->validated();

        // Maybe you need more validation rules???

        $Subscriber = Subscriber::create([
            'email' => $validated['email']


        return redirect()->back()->with('success', 'You have successfully subscribed. Please check your email spam folder.');

    public function show(string $hash)
        $subscriber = Subscriber::where('hash', $hash)->firstOrFail();

            'hash' => null,
            'verified_at' => now()

        return redirect(route('home'))
            ->with('success', 'You have successfully verified your email.');

ℹ️ Feel free to update your code/logic.

Create job

You need to create a job:

php artisan make:job SubscriberJoinJob

Now, update your job App\Jobs\SubscriberJoinJob:

class SubscriberJoinJob implements ShouldQueue
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public function __construct(public Subscriber $subscriber)

    public function handle(): void
            ->send(new UserJoin($this->subscriber));

Crate a mailable class

Run this command to create a mailable class:

php artisan make:mail UserJoin

Update the mailable class App\Mail\UserJoin:

class UserJoin extends Mailable
    use Queueable, SerializesModels;

    public function __construct(public Subscriber $user)

     * Get the message envelope.
    public function envelope(): Envelope
        return new Envelope(
            subject: 'User Join',

     * Get the message content definition.
    public function content(): Content
        return new Content(
            view: '',

     * Get the attachments for the message.
     * @return array<int, \Illuminate\Mail\Mailables\Attachment>
    public function attachments(): array
        return [];

Check more about mail:

Update Mail Setup

As I mentioned that, I will use mailtrap for testing email, so you need to setup in the .env file.

MAIL_USERNAME= # value from mailtrap
MAIL_PASSWORD= # value from mailtrap
MAIL_FROM_ADDRESS= # set the sender email address

ℹ️ Check more details how to setup

Setup Queue

ℹ️ You don’t have to do anything if you don’t want to queue the email.

If you want to queue the email, then you need to setup the config, migrate the db, and start the queue worker.

Migrate Database Run the following command:

php artisan queue:table
php artisan migrate


Setup the config Open .env file, and update QUEUE_CONNECTION value.



Start the Queue Worker Since your job is in the queue, someone needs to process it. Therefore you need a queue worker who will process the task. To process the task, simply run this command in the terminal and keep it open.

php artisan queue:work


▶ Check the source file 🔗

Hope it will help you to solve the particular issue.

Thanks for reading 🙂.