Changing the default “Subject” field for verification emails in Laravel 5.7
Asked Answered
C

3

8

I'm trying to change the default subject field in the verification email that comes with Laravel 5.7. How and where do I change it? I have searched all over the place and online. Because it's brand new I can't find an answer.

Cuevas answered 19/9, 2018 at 14:19 Comment(0)
I
20

You don't need to code anything. The notification has all the strings wrapped in the Lang class so that you can provide translation strings from english to another language, or even english to english if you just want to change the wording.

Look in /vendor/laravel/framework/src/Illuminate/Auth/Notifications/VerifyEmail.php

public function toMail($notifiable)
{
    if (static::$toMailCallback) {
        return call_user_func(static::$toMailCallback, $notifiable);
    }

    return (new MailMessage)
        ->subject(Lang::getFromJson('Verify Email Address'))
        ->line(Lang::getFromJson('Please click the button below to verify your email address.'))
        ->action(
            Lang::getFromJson('Verify Email Address'),
            $this->verificationUrl($notifiable)
        )
        ->line(Lang::getFromJson('If you did not create an account, no further action is required.'));
}

You can see all the strings there.

Create a file en.json if you don't have one on the resources/lang folder already.

add the original string and the replacement. eg

{
    "Verify Email Address": "My preferred subject",
    "Please click the button below to verify your email address.":"Another translation"
}

To translate to another language, change the locale in config/app.php and create a translation file with the locale.json

Incorporable answered 19/9, 2018 at 22:40 Comment(1)
This solution is like a trick/hack. Erubiel solution below is the most accurate and right way to achieve this.Quinine
B
13

This is the MustVerifyEmail trait

<?php

namespace Illuminate\Auth;

trait MustVerifyEmail
{
    /**
     * Determine if the user has verified their email address.
     *
     * @return bool
     */
    public function hasVerifiedEmail()
    {
        return ! is_null($this->email_verified_at);
    }

    /**
     * Mark the given user's email as verified.
     *
     * @return bool
     */
    public function markEmailAsVerified()
    {
        return $this->forceFill([
            'email_verified_at' => $this->freshTimestamp(),
        ])->save();
    }

    /**
     * Send the email verification notification.
     *
     * @return void
     */
    public function sendEmailVerificationNotification()
    {
        $this->notify(new Notifications\VerifyEmail);
    }
}

As you can see is sending a Notification named VerifyEmail, so i think overriding this method on the user model with your own notification would be enough. You should also check this file: vendor/laravel/framework/src/Illuminate/Auth/Notifications/VerifyEmail.php since it contains the notification and can be used as an example to your custom verify notification.

In User.php

    public function sendEmailVerificationNotification()
    {
        $this->notify(new MyNotification);
    }

Then run

php artisan make:notification MyNotification

And in your notification you can just extend to Illuminate\Auth\Notifications\VerifyEmail

Then you can override the notification toMail function... Haven't given it a try, but that should work.

Break answered 19/9, 2018 at 14:33 Comment(2)
@Ruub the other answer is good also, but this one is good in case you want something that is not available in the default notification provided by laravel.Break
@Lliw, if you need a more custom solution, yes. But I do suggest the accepted answer if you just need to change a couple stringsBreak
J
0

Can you post your function where you mail? I use:

\Mail::to($user)->subject('Your Subject')->bcc([$reports,$me])->send(new Declined($user));

Which is: send a mail to the $user, set the subject, blind copy in, then send the mail whilst passing in the user. This is for markdown mail also. You use the -> operator to add all of the extras for the mail, so with that you can add in BCC (like I have done) and also CC etc.

Jacinthe answered 19/9, 2018 at 14:49 Comment(3)
No, laravel 5.7 has a verification email built in... it's sent "behind scenes"Break
That is not the question hehehe is more related to this: laravel.com/docs/5.7/verificationBreak
I see! No worries, best of luck guys :-)Jacinthe

© 2022 - 2024 — McMap. All rights reserved.