如何在mailable - Laravel中动态定义邮件主题



下面是这段代码。我的想法是,每封电子邮件都从我的一个表中得到一个值/条目。

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

我想从这个地方得到我的主题

这是我的mailable的所有代码:

class ContactanosMailable extends Mailable
{
use Queueable, SerializesModels;
$this->subject = $siniestro->siniestro;
public $siniestro;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($siniestro)
{
$this->siniestro = $siniestro;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('emails.contactanos');
}
}

我会这样做:

class ContactanosMailable extends Mailable
{
use Queueable, SerializesModels;
public $siniestro;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct($siniestro)
{
$this->siniestro = $siniestro;
// Set the subject property
$this->subject = $siniestro->siniestro;
// Or use the subject method
$this->subject($siniestro->siniestro);
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('emails.contactanos');
}
}

使用subject()方法看起来更干净,但两种方法都有效。

public function build(){

$from_email = "test@mail.com";
$subject = "Account Request";
return $this->from($from_email)->subject($subject)->view('emails.contactanos')
;
}

相关内容

  • 没有找到相关文章

最新更新