Add customer name and email in confirm mail notification laravel

To add the customer name and email 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;
    private $customerEmail;

    public function __construct($customerName, $customerEmail)
    {
        $this->customerName = $customerName;
        $this->customerEmail = $customerEmail;
    }

    public function via($notifiable)
    {
        return ['mail'];
    }

    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->subject('Confirm Your Email Address')
            ->greeting('Hello '. $this->customerName. '!')
            ->line('Thank you for confirming your email address.')
            ->action('Confirm Email', url('/email/confirm'))
            ->line('If you did not request this, please ignore this email.');
    }
}

In this code, we're adding two new properties customerName and customerEmail to the notification, and passing them to the constructor. We're also modifying the toMail method to include the customer's name in the greeting and subject.

Step 3: Update the registration controller

In your RegisterController.php file, update the register method to pass the customer's name and email to the notification:

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Notifications\ConfirmEmailNotification;
use Illuminate\Foundation\Auth\RegistersUsers;

class RegisterController extends Controller
{
    //...

    protected function register(Request $request)
    {
        //...

        $user = User::create($request->all());

        // Send confirmation email
        $customerName = $request->input('name');
        $customerEmail = $request->input('email');
        $notification = new ConfirmEmailNotification($customerName, $customerEmail);
        $user->notify($notification);

        return redirect()->route('login');
    }
}

In this code, we're getting the customer's name and email from the registration form, and passing them to the ConfirmEmailNotification instance.

Step 4: Update the email template

In your resources/views/emails directory, create a new file confirm-email.blade.php. This will be the template for the confirmation email.

Add the following code to the file:

Hello {{ $customerName }},

Thank you for confirming your email address.

Best regards,
Your App Name

In this code, we're using the {{ $customerName }} syntax to display the customer's name.

Step 5: Update the mail configuration

In your config/mail.php file, update the from and view settings:

'mail' => [
    //...
    'from' => [
        'address' => env('MAIL_FROM_ADDRESS', '[email protected]'),
        'name' => env('MAIL_FROM_NAME', 'Your App Name'),
    ],
    'view' => 'emails.confirm-email',
],

In this code, we're setting the from address and name, and specifying the confirm-email view as the template for the confirmation email.

That's it! Now, when a user registers, they should receive a confirmation email with their name and email address.