Laravel 电子邮件验证模板位置

Laravel Email Verification Template Location

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

Laravel 使用这种 VerifyEmail 通知 class 的方法发送电子邮件:

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

Method in source code.

如果您想使用自己的电子邮件模板,可以扩展 Base Notification Class。

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
}

此外,如果您需要 blade 模板:

laravel will generate all of the necessary email verification views when the make:auth command is executed. This view is placed in resources/views/auth/verify.blade.php. You are free to customize this view as needed for your application.

Source.

实际上他们不使用任何 blade 或模板文件。他们创建通知并在通知中为其编写代码。

此外,如果您想翻译标准邮件 VerifyEmail(或其他使用 Lang::fromJson(...) 的地方),您需要在 [=14] 中创建新的 json 文件=]/ 并将其命名为 ru.json,例如。 它可能包含下面的 (resources/lang/ru.json) 文本并且必须是有效的。

{
  "Verify Email Address" : "Подтверждение email адреса"
}

如果通知支持作为电子邮件发送,您应该在通知上定义一个 toMail 方法class。此方法将接收一个 $notifiable 实体,并且应该 return 一个 Illuminate\Notifications\Messages\MailMessage 实例。邮件消息可能包含文本行以及 "call to action".

/**
 * Get the mail representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return \Illuminate\Notifications\Messages\MailMessage
 */
public function toMail($notifiable)
{
    $url = url('/invoice/'.$this->invoice->id);

    return (new MailMessage)
                ->greeting('Hello!')
                ->line('One of your invoices has been paid!')
                ->action('View Invoice', $url)
                ->line('Thank you for using our application!');
}

您可以使用 laravel 电子邮件生成器,如下所述:https://laravel.com/docs/5.8/notifications#mail-notifications。 Laravel 将处理电子邮件视图。

已在评论中回答。通过 toMail() 方法发送。

vendor\laravel\framework\src\Illuminate\Auth\Notifications\VerifyEmail::toMail();

用于模板结构和外观;也看看这个位置,你也可以发布修改模板:

\vendor\laravel\framework\src\Illuminate\Notifications\resources\views\email.blade.php
\vendor\laravel\framework\src\Illuminate\Mail\resources\views\

要发布这些位置:

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

执行 运行 此命令后,邮件通知模板将位于 resources/views/vendor 目录中。

颜色和样式由 resources/views/vendor/mail/html/themes/default.css

中的 CSS 文件控制

Look I do that very easy do the following steps :


在路由文件中

Auth::routes(['verify' => true]);

In AppServiceProvider.php File

namespace App\Providers;
use App\Mail\EmailVerification;
use Illuminate\Support\ServiceProvider;
use View;
use URL;
use Carbon\Carbon;
use Config;
use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Notifications\Messages\MailMessage;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        // Override the email notification for verifying email
        VerifyEmail::toMailUsing(function ($notifiable){        
            $verifyUrl = URL::temporarySignedRoute('verification.verify',
            \Illuminate\Support\Carbon::now()->addMinutes(\Illuminate\Support\Facades 
            \Config::get('auth.verification.expire', 60)),
            [
                'id' => $notifiable->getKey(),
                'hash' => sha1($notifiable->getEmailForVerification()),
            ]
        );
        return new EmailVerification($verifyUrl, $notifiable);

        });

    }
}

Now Create EmailVerification With Markdown

php artisan make:mail EmailVerification --markdown=emails.verify-email

根据需要编辑 EmailVerrification 和 blade 文件

class EmailVerification extends Mailable
{
    use Queueable, SerializesModels;
    public $verifyUrl;
    protected $user;
    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($url,$user)
    {
        $this->verifyUrl = $url;
        $this->user = $user;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        $address = 'mymail@gmail.com';
        $name = 'Name';
        $subject = 'verify Email';
        return $this->to($this->user)->subject($subject)->from($address, $name)->
        markdown('emails.verify',['url' => $this->verifyUrl,'user' => $this->user]);
    }
}

in the blade file change the design as you want and use verifyUrl to display the verification link and $user to display user information

谢谢,编码愉快:)

vendor\laravel\framework\src\Illuminate\Mail\resources\views\html

您将在此文件位置找到 Laravel 默认电子邮件模板。