Laravel:如何在使用Mailable发送电子邮件时自定义电子邮件的标题?

zwl*_*619 6 laravel-5

mailable当用户注册时,我写了一封发送电子邮件,我按照文档(https://laravel.com/docs/5.5/mail)执行此操作:

首先,生成一个mailable:

php artisan make:mail UserRegistered
Run Code Online (Sandbox Code Playgroud)

好的,因此目录中有一个UserRegistered.php文件app/Mail,我写的build()方法如下:

public function build()
{
    return $this->view('emails.activate-user')
                ->with([
                    'name' => $this->user->name,
                    'url' => route('activateUser',['token'=>$this->user->confirmation_token])
                ]);
}
Run Code Online (Sandbox Code Playgroud)

电子邮件可以成功发送,电子邮件的标题是User Registered,我想自定义标题,怎么做?

Sur*_*amy 18

你必须使用subject方法

public function build()
{
    return $this->view('emails.activate-user')
                ->subject("My mail title")
                ->with([
                    'name' => $this->user->name,
                    'url' => route('activateUser',['token'=>$this->user->confirmation_token])
                ]);
}
Run Code Online (Sandbox Code Playgroud)

或者更新你的邮件类的构造函数

public function __construct()
    {

        $this->subject('Sample title');
    }
Run Code Online (Sandbox Code Playgroud)