php 如何从 Laravel 5.7 自定义电子邮件验证电子邮件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52416804/
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-08-26 02:53:26  来源:igfitidea点击:

How to customize the email verification email from Laravel 5.7?

phplaravelemailemail-verification

提问by Wayne Fulcher

I just upgraded to Laravel 5.7 and now I am using the built in Email Verification. However there is 2 things I have not been able to figure out and the primary issue is how can I customize the email that is being sent to the user for verifying their email? I also can't figure out how to initiate sending that email if the users changes their email but I can save that for another thread.

我刚刚升级到 Laravel 5.7,现在我正在使用内置的电子邮件验证。但是,有两件事我无法弄清楚,主要问题是如何自定义发送给用户以验证其电子邮件的电子邮件?如果用户更改了他们的电子邮件,我也无法弄清楚如何开始发送​​该电子邮件,但我可以将其保存到另一个线程。

回答by Yves Kipondo

When you want to add Email Verification in Laravel 5.7the suggested method is to implement Illuminate\Contracts\Auth\MustVerifyEmailand use the Illuminate\Auth\MustVerifyEmailtrait on the App\UserModel.

当您想在Laravel 5.7 中添加电子邮件验证时,建议的方法是在模型上实现Illuminate\Contracts\Auth\MustVerifyEmail和使用Illuminate\Auth\MustVerifyEmail特征App\User

To make some custom behaviour you can override the method sendEmailVerificationNotificationwhich is the method that notifies the created user by calling the method notify, and passes as a parameter a new instance of the Notifications\MustVerifyEmailclass.

要进行一些自定义行为,您可以覆盖方法sendEmailVerificationNotification,该方法是通过调用方法通知创建的用户的方法notify,并将Notifications\MustVerifyEmail该类的新实例作为参数传递。

You can create a custom Notification which will be passed as a parameter to the $this->notify()within the sendEmailVerificationNotification method in your User Model:

您可以创建一个自定义通知,该通知将作为参数传递给$this->notify()用户模型中的 sendEmailVerificationNotification 方法:

public function sendEmailVerificationNotification()
{
    $this->notify(new App\Notifications\CustomVerifyEmail);
}

...then in your CustomVerifyEmailNotification you can define the way the verification will be handled. You can notify created user by sending an email with a custom verification.routewhich will take any parameters that you want.

...然后在您的CustomVerifyEmail通知中,您可以定义处理验证的方式。您可以通过发送带有自定义verify.route的电子邮件来通知创建的用户,该电子邮件将采用您想要的任何参数。

Email verification notification process

邮件验证通知流程

When a new user signs-up an Illuminate\Auth\Events\RegisteredEvent is emitted in the App\Http\Controllers\Auth\RegisterControllerand that Registeredevent has a listener called Illuminate\Auth\Listeners\SendEmailVerificationNotificationwhich is registered in the App\Providers\EventServiceProvider:

当新用户注册时,Illuminate\Auth\Events\Registered会在 中发出一个事件,App\Http\Controllers\Auth\RegisterController并且该Registered事件有一个调用的侦听器Illuminate\Auth\Listeners\SendEmailVerificationNotification,该侦听器在 中注册App\Providers\EventServiceProvider

protected $listen = [
    Registered::class => [
        SendEmailVerificationNotification::class,
    ]
];

The SendEmailVerificationNotificationlistener checks if the $user – which is passed as a parameter to new Registered($user = $this->create($request->all()))in the Laravel default authentication App\Http\Controllers\Auth\RegisterController– is an instance of Illuminate\Contracts\Auth\MustVerifyEmailwhich is the name of the trait that Laravel suggests is used in the App\UserModel when you want to provide default email verification and also check that $useris not already verified. If all that passes, the sendEmailVerificationNotificationmethod is called on that user:

SendEmailVerificationNotification监听器会检查$用户-这是作为参数传递给new Registered($user = $this->create($request->all()))在Laravel默认身份验证App\Http\Controllers\Auth\RegisterController-是一个实例Illuminate\Contracts\Auth\MustVerifyEmail,其是Laravel建议在使用的特征的名称App\User型号,当你想提供默认的电子邮件验证,也检查$user尚未验证的。如果所有这些都通过,则sendEmailVerificationNotification对该用户调用该方法:

if ($event->user instanceof MustVerifyEmail && !$event->user->hasVerifiedEmail())   {
    $event->user->sendEmailVerificationNotification();
}

回答by Andrew Earls

I think the simple way to do this is to make a new notification using the docs here: https://laravel.com/docs/5.7/notifications#creating-notifications

我认为这样做的简单方法是使用此处的文档制作新通知:https: //laravel.com/docs/5.7/notifications#creating-notifications

Then override the function:

然后覆盖函数:

public function sendEmailVerificationNotification()
{
    $this->notify(new App\Notifications\CustomEmailNotification);
}

In the users model.

在用户模型中。

Or you can

或者你可以

php artisan vendor:publish --tag=laravel-notifications

This will copy the templates to the resources/views/vendor/notifications directory and you can modify them there

这会将模板复制到 resources/views/vendor/notifications 目录,您可以在那里修改它们

回答by Tolga

For quick and easy way:

快速简便的方法:

php artisan vendor:publish --tag=laravel-notifications

It's creating a new file in:

它正在创建一个新文件:

\resources\views\vendor\notifications

This is Laravel's email themplate. You can change and customize it.

这是 Laravel 的电子邮件模板。您可以更改和自定义它。

回答by lagbox

Unfortunately this email that is sent out is not from a "view", it is a Notificationthat is built inline actually. This is where it is currently built when needing to be sent off: Illuminate\Auth\Notifications\VerifyEmail@toMail. This particular class has a static callback that can be set to build this email instead of letting it do it.

不幸的是,发送的这封电子邮件不是来自“视图”,它Notification实际上是内嵌的。这是它目前在需要发送时建造的地方:Illuminate\Auth\Notifications\VerifyEmail@toMail。这个特定的类有一个静态回调,可以设置它来构建这封电子邮件,而不是让它去做。

In a Service Provider in the bootmethod you will need to assign a callback for this class:

在方法中的服务提供者中,boot您需要为此类分配回调:

Something "like" this might work:

像这样的东西可能会起作用:

public function boot()
{
    \Illuminate\Auth\Notifications\VerifyEmail::toMailUsing(function ($notifiable) {

        // this is what is currently being done
        // adjust for your needs

        return (new \Illuminate\Notifications\Messages\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.'));

    });
}

As this is a notification you should have more options on customizing it.

由于这是一个通知,您应该有更多自定义选项。

If you want to use your own Notificationclass you can override the sendEmailVerificationNotificationmethod on the User(Authenticatable) model (this is from the MustVerifyEmailtrait).

如果你想使用你自己的Notification类,你可以覆盖( ) 模型sendEmailVerificationNotification上的方法(这是来自trait)。UserAuthenticatableMustVerifyEmail

Second Question:

第二个问题:

The VerificationController(App\Http\Controllers\Auth\VerificationController) that you should have has a method named resend(from the trait VerifiesEmails) that looks like a good candidate for this purpose.

您应该拥有的VerificationController( App\Http\Controllers\Auth\VerificationController) 有一个名为resend(来自 trait VerifiesEmails)的方法,看起来很适合用于此目的。

You should have routes setup for these verification routes via Auth::routes(['verify' => true]);

您应该通过以下方式为这些验证路线设置路线 Auth::routes(['verify' => true]);

Note:

笔记:

The verification system uses a field on the userstable email_verified_atin 5.7 to mark this. You would want to make sure you have this field. When the user changes email address I suppose you could make this nullthen redirect them to the resendroute, to send off the new verification. This will put them into an "unverified" state though until they reverify, if that is what you intend to happen.

验证系统使用5.7usersemail_verified_at中的一个字段来标记这一点。你会想确保你有这个字段。当用户更改电子邮件地址时,我想您可以将null其重定向到resend路由,以发送新的验证。这会将它们置于“未验证”状态,直到它们重新验证为止,如果这是您打算发生的情况。

Update:

更新:

Seems we were going down the right track. I found this SO answer that goes over similar things:

看来我们正走在正确的轨道上。我发现这个 SO 答案涵盖了类似的事情:

Changing the default “subject” field for the verification email in laravel 5.7

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

回答by Mere Development

Building slightly on the answer by Andrew Earls, you can also publish all the markdown mail components used by the application with this command:

以 Andrew Earls 的回答为基础,您还可以使用以下命令发布应用程序使用的所有降价邮件组件:

php artisan vendor:publish --tag=laravel-mail

php artisan vendor:publish --tag=laravel-mail

Once that's done you'll have a series of html and markdown files to modify in resources/views/vendor/mail. This will allow you to modify the overall email layout and also 'theme' the CSS. I'd highly recommend having a good read of the Mail docs - Customizing The Components.

完成后,您将拥有一系列 html 和 markdown 文件以在resources/views/vendor/mail. 这将允许您修改整体电子邮件布局以及“主题”CSS。我强烈建议您仔细阅读邮件文档 - 自定义组件

CSS theming

CSS 主题

As a general email theming quick-start (Laravel 5.7), you can:

作为通用电子邮件主题快速入门(Laravel 5.7),您可以:

  1. Publish the theme with php artisan vendor:publish --tag=laravel-mail.
  2. Copy resources/views/vendor/mail/html/themes/default.cssto your own file. e.g resources/views/vendor/mail/html/themes/wayne.css
  3. Edit config/mail.phpand where you see 'theme' => 'default'change it to 'theme' => 'wayne'
  4. Edit wayne.cssto restyle your emails.
  1. 用 发布主题php artisan vendor:publish --tag=laravel-mail
  2. 复制resources/views/vendor/mail/html/themes/default.css到您自己的文件中。例如resources/views/vendor/mail/html/themes/wayne.css
  3. 编辑config/mail.php并在您看到的位置'theme' => 'default'将其更改为'theme' => 'wayne'
  4. 编辑wayne.css以重新设计您的电子邮件。

Hope that helps someone.

希望能帮助某人。

回答by Taras Chernata

To send verification email you can just use the next code:

要发送验证电子邮件,您只需使用下一个代码:

 // send new verification email to user
 $user->sendEmailVerificationNotification();

回答by Nana Yaw Zaza Oduro

Navigate to these files

导航到这些文件

  • vendor/laravel/framework/src/Illuminate/Auth/MustVerifyEmail.php

  • vendor/laravel/framework/src/Illuminate/Auth/Notifications/VerifyEmail.php

  • 供应商/laravel/framework/src/Illuminate/Auth/MustVerifyEmail.php

  • 供应商/laravel/framework/src/Illuminate/Auth/Notifications/VerifyEmail.php

and then customize it. you can even introduce a constructor in vendor/laravel/framework/src/Illuminate/Auth/Notifications/VerifyEmail.php and pass value through vendor/laravel/framework/src/Illuminate/Auth/MustVerifyEmail.php

然后自定义它。你甚至可以在 vendor/laravel/framework/src/Illuminate/Auth/Notifications/VerifyEmail.php 中引入一个构造函数,并通过 vendor/laravel/framework/src/Illuminate/Auth/MustVerifyEmail.php 传递值

eg: Created my own constructorUtilized the user array values passed to the constructorPassing the constructor value from the

例如: 创建了我自己的构造函数利用传递给构造函数的用户数组值传递构造函数值