laravel - How to customize subject email using Laravel 5.5
问题描述
I have an event that triggers a listener as soon as a vehicle is created on the system.
This is my event:
class VehicleCreated
{
use Dispatchable, InteractsWithSockets, SerializesModels;
private $vehicle;
public function __construct(Vehicle $vehicle)
{
$this->vehicle = $vehicle;
}
public function getVehicle()
{
return $this->vehicle;
}
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}
This is my listener:
class SendSchedulingConfirmationListener implements ShouldQueue
{
use InteractsWithQueue;
public function handle(VehicleCreated $event)
{
$vehicle = $event->getVehicle();
$lead = $vehicle->lead;
Mail::to($lead->email)
->queue(new SchedulingConfirmation($vehicle, $lead));
}
}
I used the artisan command to create the email:
php artisan make:mail SchedulingConfirmation --markdown=emails.leads.scheduling.confirmation
I'm having trouble customizing the subject of the email currently my class looks like this:
class SchedulingConfirmation extends Mailable
{
use Queueable, SerializesModels;
public $vehicle;
public $lead;
public function __construct(Vehicle $vehicle, Lead $lead)
{
$this->vehicle = $vehicle;
$this->lead = $lead;
}
public function build()
{
return $this
->subject('This is my subject')
->markdown('emails.leads.scheduling.confirmation');
}
}
When I fire the email it arrives in mailtrap with the subject of Scheduling Confirmation
解决方案
您可以在新建邮件时传递主题:
$subject = 'truly awesome subject line';
Mail::to($lead->email)
->queue(new SchedulingConfirmation($vehicle, $lead, $subject));
所以在你的类中,只需$subject
在构造函数中添加作为参数:
class SchedulingConfirmation extends Mailable
{
use Queueable, SerializesModels;
public $vehicle;
public $lead;
public function __construct(Vehicle $vehicle, Lead $lead, $subject)
{
$this->vehicle = $vehicle;
$this->lead = $lead;
$this->subject = $subject
}
public function build()
{
return $this
->subject($this->subject)
->markdown('emails.leads.scheduling.confirmation');
}
}
推荐阅读
- http - HTTP POST 数据内容长度
- java - WebRTC 在通话期间开始/停止录音
- sql-server - 存储过程:Sql server 表备份
- string - ^x^y 历史命令中第一次出现字符串的unix技巧(不是最后一个/上一个命令)?
- ruby - 尝试将每个转换为 while 循环,会创建 TypeError。为什么符号有问题?
- git - 使用哪个路径来添加 Git 远程?
- c++ - GCC/G++ 编译器设置 GUI
- scala - 为什么“println”没有被包含在一个方法中,允许在一个scala类中?
- oracle - Weblogic Datasource 给出 ORA-12519
- c# - 文本框不能扫描多个数字,只能扫描一个数字条形码