【问题标题】:Changing the default “Subject” field for verification emails in Laravel 5.7在 Laravel 5.7 中更改验证电子邮件的默认“主题”字段
【发布时间】:2019-02-23 18:10:22
【问题描述】:

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

【问题讨论】:

    标签: laravel laravel-mail laravel-5.7


    【解决方案1】:

    您无需编写任何代码。通知将所有字符串都包装在 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.'));
    }
    

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

    如果您在 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 创建翻译文件

    【讨论】:

    • 这是我使用的解决方案。谢谢。
    • 仍在使用 Laravel 版本 6。谢谢!
    【解决方案2】:

    这是 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 功能...尚未尝试过,但这应该可以。

    【讨论】:

    • 我总是很乐意提供帮助!
    • 谢谢伙计。我是通过 Google 来到这里的,这就是我的答案。
    • @Ruub 另一个答案也很好,但是如果你想要一些 laravel 提供的默认通知中没有的东西,这个很好。
    • 哇,谢谢。这是进行自定义的准确和正确的方法。
    • 这绝对是要走的路
    【解决方案3】:

    你能把你的函数贴在你邮寄的地方吗? 我用:

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

    即:向 $user 发送邮件,设置主题,盲送,然后在传递用户的同时发送邮件。这也适用于降价邮件。您使用-&gt; 运算符为邮件添加所有额外内容,这样您就可以添加密件抄送(就像我所做的那样)以及抄送等。

    【讨论】:

    • 不,laravel 5.7 内置了验证邮件...它是在“幕后”发送的
    • 这不是问题嘿嘿和这个更相关:laravel.com/docs/5.7/verification
    • 我明白了!不用担心,祝你们好运:-)
    猜你喜欢
    • 2023-01-11
    • 2019-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-18
    • 2019-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多