Skip to content
Advertisement

How to pass variables into Blade using Laravel’s Mail functions?

I have this class that takes care of emails.

public function mail($emails, $message)
    {

        Mail::queue('emails.notification', ['message' => $message], function($m) use ($emails) {
            $m->to($emails);
            $m->subject('Notification....');

        });
    }

the $massage parameter is called from the a this class,

private function report(Notification $notification, $resCode)
    {
        if(empty($resCode)){
            $resCode = "no response found";
        }

        $this->reporter->slack($notification->website_url . ':' . '  is down' . ' this is the status code!' . ' @- ' .$resCode,
             $notification->slack_channel);

        $this->reporter->mail($notification->email,$notification->website_url.' is down '. ' this is the status Code: '. $resCode);


    }

Now i want to display the message the message in the emails.notification.blade and i tried this way

@extends('layout')
@section('header')
@stop
@section('content')
        <h1>The Web site is down!</h1>
               {{ $message }}


@stop
@section('footer')
@stop


  [ErrorException]
  htmlspecialchars() expects parameter 1 to be string, object given

this is the exception that I got. anyone with a better suggestion?

Advertisement

Answer

In your controller, the variables are passed as an associative array to Mail::send():

$data = ['emailId' => $emailId, 'mailBody' => $mailBody];
    
Mail::send(
    'mail.emailForgotPassword',
    $data,
    function ($message) use ($data) {
        $message->from('xyz.com', 'abc');
        $message->to($data['emailId'])->subject('Your Subject');
    }
);

and in your Blade template, you can reference the variables by using {{$mailBody}} or {{$emailId}} .

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement