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

2uluyalo  于 2023-06-30  发布在  其他
关注(0)|答案(2)|浏览(117)

下面是一段代码。我的想法是,每封电子邮件出来的值/条目之一 从我的一张table上

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

    $this->subject = {{ $siniestro->siniestro }};
}

From this place I want to get my subject
这是我的邮件的所有代码:

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

vkc1a9a21#

我会这样做:

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()方法看起来更简洁,但两者都有效。

oalqel3c

oalqel3c2#

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

相关问题