如何使用Laravel 5.5自定义主题电子邮件

时间:2018-10-26 14:55:28

标签: laravel laravel-5

我有一个事件,一旦在系统上创建了车辆,就会触发一个侦听器。

这是我的活动:

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

这是我的听众:

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

我使用了artisan命令来创建电子邮件:

php artisan make:mail SchedulingConfirmation --markdown=emails.leads.scheduling.confirmation

我在自定义电子邮件主题时遇到了麻烦,目前我的课程如下:

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

当我触发电子邮件时,它以计划确认

为主题到达邮件陷阱

2 个答案:

答案 0 :(得分:1)

您只需在更新可邮寄邮件时将主题传递进来:

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

答案 1 :(得分:0)

在Mailable的构造函数中设置主题

    class SchedulingConfirmation extends Mailable
    {
        use Queueable, SerializesModels;

        public $vehicle;
        public $lead;

        public function __construct(Vehicle $vehicle, Lead $lead)
        {
            $this->vehicle = $vehicle;
            $this->subject('This is my subject');
            $this->lead = $lead;
        }

        public function build()
        {
            return $this
                ->markdown('emails.leads.scheduling.confirmation');
        }
    }