16

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

4

7 回答 7

33

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. 您可以根据应用程序的需要自由自定义此视图。

来源

于 2018-09-08T04:36:46.907 回答
12

已经在评论中回答了。通过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目录中。

颜色和样式由 CSS 文件控制resources/views/vendor/mail/html/themes/default.css

于 2020-08-04T06:40:34.707 回答
5

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

{
  "Verify Email Address" : "Подтверждение email адреса"
}
于 2018-10-28T20:29:19.360 回答
3

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

于 2018-09-08T04:42:09.297 回答
2

看我很容易做到以下步骤:


在路线文件中

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

在 AppServiceProvider.php 文件中

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);

        });

    }
}

现在使用 Markdown 创建 EmailVerification

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

根据需要编辑 EmailVerrification 和刀片文件

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]);
    }
}

在刀片文件中根据需要更改设计并使用 verifyUrl 显示验证链接和 $user 显示用户信息

谢谢,快乐的编码:)

于 2020-09-23T23:21:45.910 回答
0
vendor\laravel\framework\src\Illuminate\Mail\resources\views\html

你会在这个文件位置找到 Laravel 默认的电子邮件模板。

于 2021-08-04T17:11:31.007 回答
-1

如果通知支持作为电子邮件发送,则应在通知类上定义 toMail 方法。此方法将接收 $notifiable 实体并应返回 Illuminate\Notifications\Messages\MailMessage 实例。邮件消息可能包含文本行以及“号召性用语”。

/**
 * 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 将负责电子邮件视图。

于 2019-04-21T10:54:46.483 回答