Home > Software engineering >  How to dynamically define the subject of emails in mailable - Laravel
How to dynamically define the subject of emails in mailable - Laravel

Time:05-10

Here is the piece of code. My idea is that each email comes out with one of the values/entry ​​from one of my tables.

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

[from this place I want to get my subject][1]


  [1]: https://i.stack.imgur.com/D0PNO.png

 this is all the code of my 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');

        

        
    }
}

If I can add something else just let me know, and I will.

I hope this can reach, it's getting complicated

CodePudding user response:

I would do it like that :

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

using the subject() method looks cleaner but both works

CodePudding user response:

public function build(){
     
        $from_email = "[email protected]";
        $subject = "Account Request";
        return $this->from($from_email)->subject($subject)->view('emails.user_request')
            ;
    }

  • Related