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

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

我正在尝试更改 Laravel 5.7 随附的验证电子邮件中的默认 subject 字段。我如何以及在哪里更改它?我到处都在网上搜索过。因为它是全新的我找不到答案。

这是 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);
    }
}

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

User.php

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

然后运行

php artisan make:notification MyNotification

并且在您的通知中,您可以扩展到 Illuminate\Auth\Notifications\VerifyEmail

然后你可以覆盖通知 toMail 函数...还没有试过,但应该可以。

你能post你的邮件功能吗? 我使用:

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

即:发邮件给$user,设置主题,盲抄进去,然后传入用户发送邮件。这也适用于降价邮件。您使用 -> 运算符为邮件添加所有额外内容,这样您就可以添加密件抄送(就像我所做的那样)和抄送等。

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

查看/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.'));
}

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

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

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

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

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

创建翻译文件