Home > Software design >  How to set a subject in Laravel Mailable
How to set a subject in Laravel Mailable

Time:11-25

I have the following working but the subject of the email is assigned the content. The content is set correctly. I am confused on how to set the subject? I have seen examples where the subject is set in the mailable, for example: return $this->subject($subject)->view('pages.mail.julian_email', ['content' => $this->content]). But the subject is not set in the view? Only the content? Is it possible to set the subject in the view?

Mailable:

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class JulianEmail extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
    
     */
     
     
    public $content;
    public $name;
    public $subject;
    public $email;
    
     
     
    public function __construct($content)
    {

     $this->content = $content;
     //$this->name = $name;
     //$this->subject = $subject;
     //$this->pdf = $pdf;
     //$this->pdf_name = $pdf_name;
     
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('pages.mail.julian_email', ['content' => $this->content]);
    }
}

View:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <style>
        p {
            font-size: 14px;
        }

        .signature {
            font-style: italic;
        }
    </style>
</head>
<body>
<p>{{ $content }}</p>
 </body>
</html>

Controller:

public function email_zone(Request $request){
    
    //$customers = implode(', ',$request->CustContactEmail);
    $customers="[email protected]";
    
    $user = Auth::user();
    
    if(isset($user->UserID) and !empty($user->smtp_password)){
    $subject=$request->EmailSubject;
    $content=$request->EmailBody;
    
    \Mail::to($user->email)
    ->cc($customers)
    ->send(new JulianEmail($content));
  }
  else return redirect()->route('zone_calendar')->with('status','User is not setup to send email. Contact Webmaster')->with('code','red');
    
}

CodePudding user response:

The update below fixed the issue.

public function __construct($content, $subject)
        {
        $this->content = $content;
        $this->subject = $subject;
        }
    
      
        public function build()
        {
            return $this->view('pages.mail.julian_email', ['content' => $this->content])
            ->subject($this->subject);
        }

CodePudding user response:

You can set the subject in your controller:

\Mail::to($user->email)
    ->cc($customers)
    ->send(new JulianEmail($content))
    ->subject($subject); // This line will do it

However, it's not MVC to set the subject in the view. The view only forms the body of the email.

  • Related