Changing the default “Subject” field for verification emails in Laravel 5.7
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
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.