Customize Laravel Default Verification Email (Change The Header)
Asked Answered
A

4

5

I'm trying to change and modify the default verification email in Laravel, I've found the file when you can change the contents of the default email but inside the file it has only the Subject and the lines I couldn't find the header of the email to change it, so where can I find the line of header and change it?

The header I meant:

The "Hello" word

The Code of the file which is in

Vendor/Laravel/Framework/src/illuminate/Auth/Notifications/VerifyEmail.php
protected function buildMailMessage($url)
    {
        return (new MailMessage)
            ->subject(Lang::get('Verify Email Address'))
            ->line(Lang::get('Please click the button below to verify your email address.'))
            ->action(Lang::get('Verify Email Address'), $url)
            ->line(Lang::get('If you did not create an account, no further action is required.'));
    }
Amateurism answered 15/3, 2021 at 9:41 Comment(1)
check this link #52417304Timtima
T
5

To Customize Laravel Notification Email Templates (Header and Footer) Initially Laravel will use their components hidden in the core of the framework, that you can export by doing

    php artisan vendor:publish --tag=laravel-mail

It will create a mail and markdown folders inside your resources/view/vendor folder. Inside you will find component like layout or header etc.

Creating Notification What you want to do, is either create a notification, event or a mail class in order to fire off an email when something happens. I decided to go with a notification. When creating any notification (You can read more about how to create a notification via artisan) you will get a class like this:

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class UserRegistered extends Notification {
    use Queueable;

    public $user;

    public function __construct($user) {
        $this->user = $user;
    }


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

    /**
     * Get the mail representation of the notification.
     *
     * @param mixed $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable) {
        return (new MailMessage)
            ->from('[email protected]', 'Admin')
            ->subject('Welcome to the the Portal')
            ->markdown('mail.welcome.index', ['user' => $this->user]);
    }

    /**
     * Get the array representation of the notification.
     *
     * @param mixed $notifiable
     * @return array
     */
    public function toArray($notifiable) {
        return [
            //
        ];
    }
}

Here, pay attention to the toMail method as well as the constructor of the class because we will pass an object to it. Also, note that we are using ->markdown(‘some.blade.php’); The next step is to push this notification to work. Somewhere in your RegisterController, you might want to call this (Not going into how you will execute it, either sync or queued … ). Don’t forget to include the namespace of the notification at the top.

  $user = User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'lastname' => $data['lastname'],
        'password' => bcrypt($data['password']),
    ]);

  $user->notify(new UserRegistered($user));

Why am I going so deep? Well because I also want to show you how to pass your data into the email template.

Next you can go to resources/views/mail/welcome/index.blade.php (It can be any folder and filename you want) and pasting this:

@component('mail::layout')
{{-- Header --}}
@slot('header')
    @component('mail::header', ['url' => config('app.url')])
        Header Title
    @endcomponent
@endslot

{{-- Body --}} This is our main message {{ $user }}

{{-- Subcopy --}}
@isset($subcopy)
    @slot('subcopy')
        @component('mail::subcopy')
            {{ $subcopy }}
        @endcomponent
    @endslot
@endisset

  {{-- Footer --}}
    @slot('footer')
    @component('mail::footer')
        © {{ date('Y') }} {{ config('app.name') }}. Super FOOTER!
    @endcomponent
    @endslot
    @endcomponent

You can now easily add any image to your header or change the link inside the footer etc. Hope this helps.

Timtima answered 15/3, 2021 at 10:29 Comment(0)
C
5

Like mentioned in the official Laravel Docs, you can do that by adding code to the boot method of the App\Providers\AuthServiceProvider.

use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Notifications\Messages\MailMessage;

public function boot()
{
    // ...

    VerifyEmail::toMailUsing(function ($notifiable, $url) {
        return (new MailMessage)
            ->subject('Verify Email Address')
            ->line('Click the button below to verify your email address.')
            ->action('Verify Email Address', $url);
    });
}
Catchy answered 15/3, 2021 at 9:48 Comment(1)
Thanks, this is definitely the simplest way to do this. Make sure you put this in AuthServiceProvider and not AppServiceProvider.Speculate
T
5

To Customize Laravel Notification Email Templates (Header and Footer) Initially Laravel will use their components hidden in the core of the framework, that you can export by doing

    php artisan vendor:publish --tag=laravel-mail

It will create a mail and markdown folders inside your resources/view/vendor folder. Inside you will find component like layout or header etc.

Creating Notification What you want to do, is either create a notification, event or a mail class in order to fire off an email when something happens. I decided to go with a notification. When creating any notification (You can read more about how to create a notification via artisan) you will get a class like this:

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class UserRegistered extends Notification {
    use Queueable;

    public $user;

    public function __construct($user) {
        $this->user = $user;
    }


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

    /**
     * Get the mail representation of the notification.
     *
     * @param mixed $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable) {
        return (new MailMessage)
            ->from('[email protected]', 'Admin')
            ->subject('Welcome to the the Portal')
            ->markdown('mail.welcome.index', ['user' => $this->user]);
    }

    /**
     * Get the array representation of the notification.
     *
     * @param mixed $notifiable
     * @return array
     */
    public function toArray($notifiable) {
        return [
            //
        ];
    }
}

Here, pay attention to the toMail method as well as the constructor of the class because we will pass an object to it. Also, note that we are using ->markdown(‘some.blade.php’); The next step is to push this notification to work. Somewhere in your RegisterController, you might want to call this (Not going into how you will execute it, either sync or queued … ). Don’t forget to include the namespace of the notification at the top.

  $user = User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'lastname' => $data['lastname'],
        'password' => bcrypt($data['password']),
    ]);

  $user->notify(new UserRegistered($user));

Why am I going so deep? Well because I also want to show you how to pass your data into the email template.

Next you can go to resources/views/mail/welcome/index.blade.php (It can be any folder and filename you want) and pasting this:

@component('mail::layout')
{{-- Header --}}
@slot('header')
    @component('mail::header', ['url' => config('app.url')])
        Header Title
    @endcomponent
@endslot

{{-- Body --}} This is our main message {{ $user }}

{{-- Subcopy --}}
@isset($subcopy)
    @slot('subcopy')
        @component('mail::subcopy')
            {{ $subcopy }}
        @endcomponent
    @endslot
@endisset

  {{-- Footer --}}
    @slot('footer')
    @component('mail::footer')
        © {{ date('Y') }} {{ config('app.name') }}. Super FOOTER!
    @endcomponent
    @endslot
    @endcomponent

You can now easily add any image to your header or change the link inside the footer etc. Hope this helps.

Timtima answered 15/3, 2021 at 10:29 Comment(0)
P
4

Both answers to the question give absolutely correct solutions to the way you should publish the email template, as you should never modify it inside the "vendor" folder. However, I believe that your question was mostly regarding the way you could modify the "Hello!" string, and this was not answered above. As per https://laravel.com/api/8.x/Illuminate/Notifications/Messages/MailMessage.html you should use the "greeting" method. In other words your code should look like this:

return (new MailMessage)
        ->greeting(Lang::get('Hi there!'))
        ->subject(Lang::get('Verify Email Address'))
        ->line(Lang::get('Please click the button below to verify your email address.'))
        ->action(Lang::get('Verify Email Address'), $url)
        ->line(Lang::get('If you did not create an account, no further action is required.'));
Perspicuous answered 20/10, 2021 at 8:28 Comment(0)
D
0

I ended up using Mail facade in User model..

public function sendPasswordResetNotification($token){
    // $this->notify(new MyCustomResetPasswordNotification($token)); <--- remove this, use Mail instead like below

    $data = [
        $this->email
    ];

    Mail::send('email.reset-password', [
        'fullname'      => $this->fullname,
        'reset_url'     => route('user.password.reset', ['token' => $token, 'email' => $this->email]),
    ], function($message) use($data){
        $message->subject('Reset Password Request');
        $message->to($data[0]);
    });
}
Dolf answered 29/6, 2022 at 9:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.