Add customer name in confirm mail notification laravel
To add the customer name in the confirm mail notification in Laravel, you can follow these steps:
Step 1: Create a new notification
In your Laravel project, run the following command to create a new notification:
php artisan make:notification ConfirmEmailNotification
This will create a new file ConfirmEmailNotification.php
in the app/Notifications
directory.
Step 2: Modify the notification
In the ConfirmEmailNotification.php
file, add the following code:
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class ConfirmEmailNotification extends Notification
{
use Queueable;
private $customerName;
public function __construct($customerName)
{
$this->customerName = $customerName;
}
public function via($notifiable)
{
return ['mail'];
}
public function toMail($notifiable)
{
return (new MailMessage)
->subject('Confirm Your Email Address')
->greeting('Hello '. $this->customerName. '!')
->line('You are receiving this email because you requested to confirm your email address.')
->action('Confirm Email', url('/email/confirm'))
->line('Thank you for confirming your email address.');
}
}
In this code, we're adding a new property customerName
to the notification, and then using it in the toMail
method to include the customer's name in the email subject and greeting.
Step 3: Update the email confirmation controller
In your EmailConfirmationController.php
file, update the sendConfirmationEmail
method to pass the customer's name to the notification:
namespace App\Http\Controllers\Auth;
use App\Notifications\ConfirmEmailNotification;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class EmailConfirmationController extends Controller
{
//...
public function sendConfirmationEmail(Request $request)
{
$customer = Auth::user();
$notification = new ConfirmEmailNotification($customer->name);
$notification->send($request->user());
return redirect()->route('home');
}
}
In this code, we're creating a new instance of the ConfirmEmailNotification
class and passing the customer's name to it. We then send the notification using the send
method.
Step 4: Update the email template
In your resources/views/emails
directory, update the confirm-email.blade.php
file to include the customer's name:
<!DOCTYPE html>
<html>
<head>
<title>Confirm Email</title>
</head>
<body>
<h1>Hello {{ $customerName }}!</h1>
<p>You are receiving this email because you requested to confirm your email address.</p>
<p><a href="{{ url('/email/confirm') }}">Confirm Email</a></p>
<p>Thank you for confirming your email address.</p>
</body>
</html>
In this code, we're using the {{ $customerName }}
variable to include the customer's name in the email template.
That's it! Now, when a user requests to confirm their email address, they should receive an email with their name included in the subject and greeting.