自定义 Laravel 默认验证邮件更改标题

问题描述

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

The header I meant:

“你好”这个词

所在文件代码

vendor/Laravel/Framework/src/illuminate/Auth/Notifications/VerifyEmail.PHP
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.'));
    }

解决方法

就像官方 Laravel Docs 中提到的那样,您可以通过向 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);
    });
}
,

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

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

它将在您的资源/视图/供应商文件夹中创建一个邮件和降价文件夹。在里面你会发现布局或标题等组件。

创建通知 您想要做的是创建通知、事件或邮件类,以便在发生某些事情时发送电子邮件。 我决定带着通知去。创建任何通知时(您可以阅读有关如何通过 artisan 创建通知的更多信息),您将获得这样的类:

    <?php

     namespace App\Notifications;

     use Illuminate\Bus\Queueable;
     use Illuminate\Notifications\Notification;
    use Illuminate\Contracts\Queue\ShouldQueue;
    use Illuminate\Notifications\Messages\MailMessage;

   class UserRegistered extends Notification
   {
   use Queueable;

  public $user;

  public function __construct($user)
  {
    $this->user = $user;
  }


public function via($notifiable)
{
    return ['mail'];
}

/**
 * Get the mail representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return \Illuminate\Notifications\Messages\MailMessage
 */
public function toMail($notifiable)
{  
    return (new MailMessage)
        ->from('info@sometimes-it-wont-work.com','Admin')
        ->subject('Welcome to the the Portal')
        ->markdown('mail.welcome.index',['user' => $this->user]);
   }

/** * 获取通知的数组表示。 * * @param 混合 $notifiable * @return 数组 */ 公共函数 toArray($notifiable) { 返回 [ // ]; } } 在这里,注意 toMail 方法以及类的构造函数,因为我们将向它传递一个对象。另请注意,我们正在使用 ->markdown('some.blade.php'); 下一步是推送此通知工作。在你的 RegisterController 的某个地方你可能想调用它(不讨论你将如何执行它,同步或排队......)。不要忘记在顶部包含通知的命名空间。

  $user = User::create([
        'name' => $data['name'],'email' => $data['email'],'lastname' => $data['lastname'],'password' => bcrypt($data['password']),]);

  $user->notify(new UserRegistered($user));

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

接下来你可以去resources/views/mail/welcome/index.blade.php(它可以是你想要的任何文件夹和文件名)并粘贴这个:

@component('mail::layout')
{{-- Header --}}
@slot('header')
    @component('mail::header',['url' => config('app.url')])
        Header Title
    @endcomponent
@endslot

{{-- 正文 --}} 这是我们的主要信息 {{ $user }}

{{-- Subcopy --}}
@isset($subcopy)
    @slot('subcopy')
        @component('mail::subcopy')
            {{ $subcopy }}
        @endcomponent
    @endslot
@endisset

  {{-- Footer --}}
    @slot('footer')
    @component('mail::footer')
        © {{ date('Y') }} {{ config('app.name') }}. Super FOOTER!
    @endcomponent
    @endslot
    @endcomponent

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