首页 > 解决方案 > Laravel 电子邮件验证模板位置

问题描述

我一直在阅读有关 laravel 电子邮件验证新功能的文档。在哪里可以找到发送给用户的电子邮件模板?它没有在这里显示:https ://laravel.com/docs/5.7/verification#after-verifying-emails

标签: laravellaravel-5.7

解决方案


Laravel 使用VerifyEmail通知类的这个方法来发送电子邮件:

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

源代码中的方法

如果您想使用自己的电子邮件模板,您可以扩展基本通知类。

1) 在app/Notifications/文件中创建VerifyEmail.php

<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Lang;
use Illuminate\Auth\Notifications\VerifyEmail as VerifyEmailBase;

class VerifyEmail extends VerifyEmailBase
{
//    use Queueable;

    // change as you want
    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.'));
    }
}

2)添加到用户模型:

use App\Notifications\VerifyEmail;

/**
 * Send the email verification notification.
 *
 * @return void
 */
public function sendEmailVerificationNotification()
{
    $this->notify(new VerifyEmail); // my notification
}

此外,如果您需要刀片模板:

make:auth当命令执行时,laravel 将生成所有必要的电子邮件验证视图。此视图放置在 resources/views/auth/verify.blade.php. 您可以根据应用程序的需要自由自定义此视图。

来源


推荐阅读