Laravel电子邮件验证模板位置

时间:2018-09-08 03:59:52

标签: laravel laravel-5.7

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

7 个答案:

答案 0 :(得分:8)

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

Method in source code

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

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
}

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

  

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

Source

答案 1 :(得分:6)

已经在评论中答复。通过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文件控制

答案 2 :(得分:2)

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

答案 3 :(得分:1)

我很容易做到 请执行以下步骤:


在路线文件中

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创建电子邮件验证

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显示用户信息

感谢,祝您编码愉快:)

答案 4 :(得分:0)

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

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

答案 5 :(得分:0)

如果通知支持通过电子邮件发送,则应在通知类上定义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将处理电子邮件视图。

答案 6 :(得分:0)

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

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

相关问题