Laravel - How to Set Subject in Mail?

By Hardik Savani April 16, 2024 Category : Laravel

Hi Dev,

Now, let's see a tutorial of laravel mail change subject. if you want to see an example of laravel mail set subject then you are the right place. I would like to show you how to set subject in email laravel. you can understand a concept of set subject in laravel mail. Let's see below the example laravel set subject mail.

If you want to change the subject then I will let you know how to set the subject in laravel mail. you can use this example with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version.

Yesterday, i write following tutorial about send email with multiple attachment, you can follow this tutorial here: Laravel Send Email with Multiple Attachment Example.

Laravel provides subject() where we can change the subject of the email.

We will change the following code on MyTestMail class as follows and you will see how to change from name and address with output.

Solution:

public function build()

{

return $this->view('emails.myTestMail')

->subject('Your Subject Here');

}

let's see MyTestMail.php file code as like bellow:

app/Mail/MyTestMail.php

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;

use Illuminate\Contracts\Queue\ShouldQueue;

use Illuminate\Mail\Mailable;

use Illuminate\Queue\SerializesModels;

class MyTestMail extends Mailable

{

use Queueable, SerializesModels;

public $details;

/**

* Create a new message instance.

*

* @return void

*/

public function __construct($details)

{

$this->details = $details;

}

/**

* Build the message.

*

* @return $this

*/

public function build()

{

$this->subject('Mail from ItSolutionStuff.com')

->view('emails.myTestMail');

return $this;

}

}

Output:

I hope it can help you...

Tags :
Shares