Laravel 10 - Welcome Notification Email
Laravel 10 - Welcome Notification Email
In this tutorial, we’ll explore how to send a welcome email notification in Laravel 10, enhancing user engagement and providing a personalized experience
If you're a video person, feel free to skip the post and check out the video instead!
In the above video, UI Authentication was used instead of Breeze Auth, which may result in differences in methods and implementations.
Step # 1 : Create Fresh Laravel Project.
Two commands to create fresh laravel project
Global Command : laravel new notifications
Or use
Non Global Command : composer create-project laravel/laravel --prefer-dist notifications
Step # 2 : Access the project.
Open a terminal (e.g., Git Bash) and navigate to your Laravel project's root folder.
Git Bash : cd c:xampp/htdocs/notifications
Step # 3 : Install Breeze Authentication.
To send a welcome notification email, we need authentication in place so that we can trigger the email when a user creates an account. Therefore, let's set up the Breeze Authentication.
Install Laravel Breeze Auth.
Command : composer require laravel/breeze --dev
Install Breeze Scaffolding.
Command : php artisan breeze:install
You will be prompted to select a stack (e.g., Blade or Inertia). Based on your selection, you may be asked to choose additional options, such as enabling dark mode or Pest testing, if applicable.
Install dependencies and compile assets.
Command : npm install && npm run dev
In a new terminal window or tab (while keeping the Vite server running), navigate to the same project directory to execute further Laravel command.
Run migrations.
Command : php artisan migrate
Step # 4 : Create notification.
Run the following command to create a new notification
Command : php artisan make:notification WelcomeMailNotification
This command will generate a Notifications folder and create a WelcomeMailNotification.php file within the app directory.
Let's access WelcomeMailNotification.php and update toMail method like.
public function toMail(object $notifiable): MailMessage
{
return (new MailMessage)
->line('Welcome To Code Shotcut')
->action('Action Url', url('/'))
->line('Thank you for using Code Shotcut!');
}
Step # 5 : Create Mailtrap account.
Link : https://mailtrap.io/
After creating an account you need to create a project for testing purpose.
Access the testing project and you will see all the details required in next step.
Step # 6: Update .env.
Replace the details below with your Mailtrap credentials for testing purposes.
MAIL_MAILER=smtp
MAIL_HOST=sandbox.smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME="Your Mailtrap username"
MAIL_PASSWORD="Your Mailtrap password"
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="Your email address"
MAIL_FROM_NAME="${APP_NAME}"
Step # 7 : Update RegisteredUserController.
Access RegisteredUserController.php.
Import WelcomeMailNotification class
use App\Notifications\WelcomeMailNotification;
Update store method like below.
public function store(Request $request): RedirectResponse
{
$request->validate([
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'confirmed', Rules\Password::defaults()],
]);
$user = User::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
]);
event(new Registered($user));
//Send Welcome Mail Notification
$user->notify(new WelcomeMailNotification());
Auth::login($user);
return redirect(RouteServiceProvider::HOME);
}
Step # 8 : It's time to test.
Start the Laravel development server.
Command : php artisan serve.
Access below URL.
127.0.0.1:8000/registerCreate an account and you will receive welcome notification email in Mailtrap.
If you want to pass a custom view. Update RegisteredUserController store method like below.
public function store(Request $request): RedirectResponse
{
$request->validate([
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'lowercase', 'email', 'max:255', 'unique:'.User::class],
'password' => ['required', 'confirmed', Rules\Password::defaults()],
]);
$user = User::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
]);
event(new Registered($user));
//Send Welcome Mail Notification and pass user.
$user->notify(new WelcomeMailNotification($user));
Auth::login($user);
return redirect(route('dashboard', absolute: false));
}
Update WelcomeMailNotification.php like following.
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class WelcomeMailNotification extends Notification
{
use Queueable;
protected $user;
// Constructor to accept the user object
public function __construct($user)
{
$this->user = $user;
}
public function via($notifiable)
{
return ['mail'];
}
// Customize the email using a view and pass the user data
public function toMail($notifiable)
{
return (new MailMessage)
->subject('Welcome to Code Shotcut')
->view('welcome-email', ['user' => $this->user]);
// Use custom view
}
public function toArray($notifiable)
{
return [];
}
}
Create a custom view as defined in toMail method and define the following code.
welcome-email.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Welcome to Code Shotcut</title>
</head>
<body>
<h1>Welcome, {{ $user->name }}!</h1>
<p>Thank you for joining us.</p>
<p>Your email is: {{ $user->email }}</p>
</body>
</html>
Create a new account and you will receive an email with updated view.
Share this with friends!
To engage in commentary, kindly proceed by logging in or registering
Subscribe to Our Newsletter
Stay ahead of the curve! Join our newsletter to see what everyone’s talking about.
0 Comments