自定义 Laravel 默认验证电子邮件(更改标题)

Pes*_*san 5 php email-verification laravel

我正在尝试更改和修改 Laravel 中的默认验证电子邮件,当您可以更改默认电子邮件的内容时,我找到了该文件,但在文件内它只有主题和我找不到标题的行电子邮件的标题行进行更改,那么我在哪里可以找到标题行并进行更改?

我的意思是标题:

“你好”这个词

文件的代码位于

Vendor/Laravel/Framework/src/illuminate/Auth/Notifications/VerifyEmail.php
Run Code Online (Sandbox Code Playgroud)
protected function buildMailMessage($url)
    {
        return (new MailMessage)
            ->subject(Lang::get('Verify Email Address'))
            ->line(Lang::get('Please click the button below to verify your email address.'))
            ->action(Lang::get('Verify Email Address'), $url)
            ->line(Lang::get('If you did not create an account, no further action is required.'));
    }
Run Code Online (Sandbox Code Playgroud)

Jul*_*n S 5

正如Laravel官方文档中提到的,您可以通过bootApp\Providers\AuthServiceProvider.

use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Notifications\Messages\MailMessage;

public function boot()
{
    // ...

    VerifyEmail::toMailUsing(function ($notifiable, $url) {
        return (new MailMessage)
            ->subject('Verify Email Address')
            ->line('Click the button below to verify your email address.')
            ->action('Verify Email Address', $url);
    });
}
Run Code Online (Sandbox Code Playgroud)


Bas*_*mal 5

自定义 Laravel 通知电子邮件模板(页眉和页脚)\n最初 Laravel 将使用隐藏在框架核心中的组件,您可以通过执行以下操作将其导出

\n
    php artisan vendor:publish --tag=laravel-mail\n
Run Code Online (Sandbox Code Playgroud)\n

它将在您的 resources/view/vendor 文件夹中创建邮件和 markdown 文件夹。在里面你会发现布局或标题等组件。

\n

创建通知\n您想要做的是创建通知、事件或邮件类,以便在发生某些情况时发出电子邮件。\n我决定使用通知。创建任何通知时(您可以阅读有关如何通过 artisan 创建通知的更多信息),您将获得如下所示的类:

\n
<?php\n\nnamespace App\\Notifications;\n\nuse Illuminate\\Bus\\Queueable;\nuse Illuminate\\Notifications\\Notification;\nuse Illuminate\\Contracts\\Queue\\ShouldQueue;\nuse Illuminate\\Notifications\\Messages\\MailMessage;\n\nclass UserRegistered extends Notification {\n    use Queueable;\n\n    public $user;\n\n    public function __construct($user) {\n        $this->user = $user;\n    }\n\n\n    public function via($notifiable) {\n        return ['mail'];\n    }\n\n    /**\n     * Get the mail representation of the notification.\n     *\n     * @param mixed $notifiable\n     * @return \\Illuminate\\Notifications\\Messages\\MailMessage\n     */\n    public function toMail($notifiable) {\n        return (new MailMessage)\n            ->from('info@sometimes-it-wont-work.com', 'Admin')\n            ->subject('Welcome to the the Portal')\n            ->markdown('mail.welcome.index', ['user' => $this->user]);\n    }\n\n    /**\n     * Get the array representation of the notification.\n     *\n     * @param mixed $notifiable\n     * @return array\n     */\n    public function toArray($notifiable) {\n        return [\n            //\n        ];\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

在这里,请注意 toMail 方法以及该类的构造函数,因为我们将向它传递一个对象。另请注意,我们正在使用 \n->markdown(\xe2\x80\x98some.blade.php\xe2\x80\x99);\n下一步是推送此通知以使其工作。在 RegisterController 的某个地方,您可能想调用它(不讨论如何执行它,同步或排队 \xe2\x80\xa6 )。不要忘记在顶部包含通知的命名空间。

\n
  $user = User::create([\n        'name' => $data['name'],\n        'email' => $data['email'],\n        'lastname' => $data['lastname'],\n        'password' => bcrypt($data['password']),\n    ]);\n\n  $user->notify(new UserRegistered($user));\n
Run Code Online (Sandbox Code Playgroud)\n

我为什么要走这么深?好吧,因为我还想向您展示如何将数据传递到电子邮件模板中。

\n

接下来,您可以转到 resources/views/mail/welcome/index.blade.php (可以是您想要的任何文件夹和文件名)并粘贴以下内容:

\n
@component('mail::layout')\n{{-- Header --}}\n@slot('header')\n    @component('mail::header', ['url' => config('app.url')])\n        Header Title\n    @endcomponent\n@endslot\n
Run Code Online (Sandbox Code Playgroud)\n

{{-- Body --}}\n这是我们的主要信息{{ $user }}

\n
{{-- Subcopy --}}\n@isset($subcopy)\n    @slot('subcopy')\n        @component('mail::subcopy')\n            {{ $subcopy }}\n        @endcomponent\n    @endslot\n@endisset\n\n  {{-- Footer --}}\n    @slot('footer')\n    @component('mail::footer')\n        \xc2\xa9 {{ date('Y') }} {{ config('app.name') }}. Super FOOTER!\n    @endcomponent\n    @endslot\n    @endcomponent\n
Run Code Online (Sandbox Code Playgroud)\n

您现在可以轻松地将任何图像添加到页眉或更改页脚内的链接等。\n希望这会有所帮助。

\n