Laravel电子邮件验证模板位置

21
7个回答

42

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
}

如果您需要Blade模板:

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

来源


9
这是验证页面的模板,不是电子邮件。电子邮件是通过vendor\laravel\framework\src\Illuminate\Auth\Notifications\VerifyEmail::toMail()方法发送的。 - laze
@laze 谢谢,我写了如何覆盖基本电子邮件的方法。 - Илья Зеленько
是的,现在没问题了,这才是真正的解决方案! ;) - laze

20

已在评论中回答。由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文件控制。

请问,email.blade.php是一个组件,哪个模板使用了这个组件,我该如何自定义它呢?谢谢。 - Zahra19977

6
此外,如果您想翻译标准邮件VerifyEmail(或其他使用Lang :: fromJson(...)的邮件),您需要在resources / lang /中创建新的json文件,并将其命名为ru.json,例如。 它可以包含(resources / lang / ru.json)下面的文本,并且必须有效。
{
  "Verify Email Address" : "Подтверждение email адреса"
}

4

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


那段代码在哪里?代码中的HTML标签在哪里? - Adelin

4

看,我很容易做到这一点 按照以下步骤操作:


在路由文件中

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

按照您的需求编辑电子邮件验证和刀片文件

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

在 Blade 文件中,您可以根据需要更改设计,并使用 verifyUrl 显示验证链接,$user 显示用户信息。
谢谢,愉快编码 :)

完美,这正是我一直在寻找的解决方案! - lortschi

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

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


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

您可以按照此处的文档https://laravel.com/docs/5.8/notifications#mail-notifications使用Laravel电子邮件构建器。 Laravel将负责电子邮件视图。


虽然这个链接可能回答了问题,但最好在此处包含答案的基本部分并提供参考链接。如果链接页面更改,仅链接的答案可能会失效。- 来自审查 - Mark Rotteveel
@Zoe 我认为 Laravel 在未来几年内删除旧文档的可能性非常小,因为他们在保留旧文档在线上方面有着非常好的记录。我已经更新了我的帖子。 - scre_www
1
@scre_www 链接的来源并不重要 - 规则是普遍适用的。不过我已经撤回了我的标记。 - Zoe stands with Ukraine

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接