How To Set Mail Subject In Laravel Mail?

Hello artisan,

In this tutorial, I am going to show you how we can set mail subject before sending in laravel. You know that when we sent a mail to users then it it very important to set perfect subject for your email. In this example, I will show you how we can set that subject with our mail in laravel application.

We can set the subject in laravel mail with the subject() helper which we can chain this method with the view() helper before rendering. So, let's see a example source code of laravel mail change subject. If you want to see an example of laravel mail set subject then you are in the right place. See the below code:

 

app/Mail/MyTestMail.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 codecheef.org')
                    ->view('emails.myTestMail');
  
        return $this;
    }
}

 

Read also: How to Send Email with Dynamic Sender Address in Laravel?

 

Hope it can help you.

 

#laravel #laravel-9x #laravel-mail