问题
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 by myself and online. Because it's brand new I can't find an answer. Can you help me, please? Thanks!
回答1:
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
回答2:
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.
回答3:
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.
来源:https://stackoverflow.com/questions/52408042/changing-the-default-subject-field-for-verification-emails-in-laravel-5-7