在Laravel 5.7中更改验证电子邮件的默认"主题"字段

Gab*_*Mic 6 laravel laravel-mail laravel-5.7

我正在尝试更改subjectLaravel 5.7附带的验证邮件中的默认字段.我如何以及在何处更改它?我自己和网上搜遍了整个地方.因为它是全新的我找不到答案.你能帮我吗?谢谢!

Eru*_*iel 12

这是MustVerifyEmail特征

<?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);
    }
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的那样,发送一个名为VerifyEmail的通知,因此我认为使用您自己的通知在用户模型上覆盖此方法就足够了.您还应该检查此文件:vendor/laravel/framework/src/Illuminate/Auth/Notifications/VerifyEmail.php因为它包含通知,可以用作自定义验证通知的示例.

User.php

    public function sendEmailVerificationNotification()
    {
        $this->notify(new MyNotification);
    }
Run Code Online (Sandbox Code Playgroud)

然后跑

php artisan make:notification MyNotification
Run Code Online (Sandbox Code Playgroud)

在您的通知中,您可以延伸到 Illuminate\Auth\Notifications\VerifyEmail

然后你可以覆盖通知toMail函数...没有试一试,但这应该工作.


Sna*_*pey 10

您不需要编写任何代码.通知包含Lang类中包含的所有字符串,以便您可以提供从英语到另一种语言的翻译字符串,如果您只想更改措辞,则可以提供英语到英语.

查看/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.'));
}
Run Code Online (Sandbox Code Playgroud)

你可以看到那里的所有字符串.

如果您已在resources/lang文件夹中没有文件,请创建文件en.json.

添加原始字符串和替换.例如

{
    "Verify Email Address": "My preferred subject",
    "Please click the button below to verify your email address.":"Another translation"
}
Run Code Online (Sandbox Code Playgroud)

要翻译成另一种语言,请更改config/app.php中的语言环境,并使用locale.json创建翻译文件