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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52408042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:04:22  来源:igfitidea点击:

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

laravellaravel-maillaravel-5.7

提问by GabMic

I'm trying to change the default subjectfield in the verification email that comes with Laravel 5.7. How and where do I change it? I have searched all over the place and online. Because it's brand new I can't find an answer.

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

回答by Erubiel

This is the MustVerifyEmail trait

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

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.phpsince it contains the notification and can be used as an example to your custom verify notification.

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

In User.php

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

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

Then you can override the notification toMail function... Haven't given it a try, but that should work.

然后你可以覆盖通知 toMail 功能......还没有尝试过,但这应该有效。

回答by Snapey

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.

你不需要编码任何东西。该通知将所有字符串包装在 Lang 类中,以便您可以提供从英语到另一种语言的翻译字符串,如果您只想更改措辞,甚至可以提供英语到英语的翻译字符串。

Look in /vendor/laravel/framework/src/Illuminate/Auth/Notifications/VerifyEmail.php

查看/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.

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

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

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

回答by party-ring

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.

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