Laravel Send Welcome Email After Registration Example

By Hardik Savani April 16, 2024 Category : Laravel

Hello Friends,

This detailed guide will cover laravel send email after registration. We will use laravel send email after registration. Here you will learn laravel send email on registration. This post will give you a simple example of laravel send welcome email after registration. Let's see below example laravel send welcome email notification after registration.

In this tutorial, i will show you how to send welcome email notification to user after register. we will use laravel ui to create basic authentication. then we will create WelcomeMailNotification notification to send welcome mail to user. then we will send notification from RegisterController controller file.

You can send email after registration in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 versions.

So, let's follow the below step to done following steps:

Step 1: Install Laravel

This step is not required; however, if you have not created the laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app

Step 2: Create Welcome Notification

In this step, we need to create "Notification" by using laravel artisan command, so let's run bellow command, we will create WelcomeMailNotification notification class.

php artisan make:notification WelcomeMailNotification

now you can see new folder will create as "Notifications" in app folder. You need to make following changes as like bellow class.

app/Notifications/WelcomeMailNotification.php

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;

use Illuminate\Contracts\Queue\ShouldQueue;

use Illuminate\Notifications\Messages\MailMessage;

use Illuminate\Notifications\Notification;

use App\Models\User;

class WelcomeMailNotification extends Notification

{

use Queueable;

public $user;

/**

* Create a new notification instance.

*/

public function __construct(User $user)

{

$this->user = $user;

}

/**

* Get the notification's delivery channels.

*

* @return array

*/

public function via(object $notifiable): array

{

return ['mail'];

}

/**

* Get the mail representation of the notification.

*/

public function toMail(object $notifiable): MailMessage

{

$mailData = [

'name' => $this->user->name,

'email' => $this->user->email

];

return (new MailMessage)->markdown(

'emails.welcome', ['mailData' => $mailData]

);

}

/**

* Get the array representation of the notification.

*

* @return array

*/

public function toArray(object $notifiable): array

{

return [

];

}

}

Next, we need to create welcome.blade.php file to send welcome email content. so, let's create as the bellow:

resources/views/emails/welcome.blade.php

@component('mail::message')

Welcome to ItSolutionStuff.com

Name: {{ $mailData['name'] }}<br/>

Email: {{ $mailData['email'] }}

Thanks,<br/>

{{ config('app.name') }}

@endcomponent

Step 3: Email Configuration

in this step, we need to add email configuration to .env file. So you can simply add as like following.

.env

MAIL_MAILER=smtp

MAIL_HOST=smtp.gmail.com

MAIL_PORT=465

MAIL_USERNAME=mygoogle@gmail.com

MAIL_PASSWORD=rrnnucvnqlbsl

MAIL_ENCRYPTION=tls

MAIL_FROM_ADDRESS=mygoogle@gmail.com

MAIL_FROM_NAME="${APP_NAME}"

Step 4: Create Auth Scaffold

Now, in this step, we will create auth scaffold command to create login, register and dashboard. so run following commands:

Laravel UI Package:

composer require laravel/ui

Generate Auth:

php artisan ui bootstrap --auth

npm install

npm run build

Step 5: Update RegisterController File

Here, in this step, we will update RegisterController controller file. we will send notification to user after registration here. so, let's update code:

app/Http/Controllers/Auth/RegisterController.php

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;

use App\Providers\RouteServiceProvider;

use App\Models\User;

use Illuminate\Foundation\Auth\RegistersUsers;

use Illuminate\Support\Facades\Hash;

use Illuminate\Support\Facades\Validator;

use App\Notifications\WelcomeMailNotification;

class RegisterController extends Controller

{

/*

|--------------------------------------------------------------------------

| Register Controller

|--------------------------------------------------------------------------

|

| This controller handles the registration of new users as well as their

| validation and creation. By default this controller uses a trait to

| provide this functionality without requiring any additional code.

|

*/

use RegistersUsers;

/**

* Where to redirect users after registration.

*

* @var string

*/

protected $redirectTo = RouteServiceProvider::HOME;

/**

* Create a new controller instance.

*

* @return void

*/

public function __construct()

{

$this->middleware('guest');

}

/**

* Get a validator for an incoming registration request.

*

* @param array $data

* @return \Illuminate\Contracts\Validation\Validator

*/

protected function validator(array $data)

{

return Validator::make($data, [

'name' => ['required', 'string', 'max:255'],

'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],

'password' => ['required', 'string', 'min:8', 'confirmed'],

]);

}

/**

* Create a new user instance after a valid registration.

*

* @param array $data

* @return \App\Models\User

*/

protected function create(array $data)

{

$user = User::create([

'name' => $data['name'],

'email' => $data['email'],

'password' => Hash::make($data['password']),

]);

$user->notify(new \App\Notifications\WelcomeMailNotification($user));

return $user;

}

}

Run Laravel App:

All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:

php artisan serve

Now, Go to your web browser, type the given URL and view the app output:

http://localhost:8000/register

Now, you will see the following output:

Register Page:

Welcome Email:

I hope it can help you...

Tags :
Shares