Home > Software engineering >  Email in laravel shows variable instead of the value
Email in laravel shows variable instead of the value

Time:11-23

I'm not sure what im missing here. So im working on emailing the client whenever there's an update however it doesnt show the value of variable or array passed here. So there's 3 files. First we start in the controller.

$details = [
        'title'=> "[Helpdesk] Your ticket #{$ids[0]} has an update.",
        'body' => 'Your ticket request status has been updated.',
        'first_name' => $first_name,
        'last_name' => $last_name,
        'ticket_id' => $ids[0],
        'status' => $status,
        'agent_name' => $agent_name,
    ];
    

    //unrelated codes

    else if($status == 'open' || $status == 'assigned')
        {
            
            \Mail::to($email)->send(new \App\Mail\OpenAssignTicket($details));
        }

Now this is in the OpenAssignTicket file.

<?php

namespace App\Mail;

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

class OpenAssignTicket 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()
{
    return $this->subject('Mail from Support')->view('emails.ticketAssignedOpen');
}
}

And finally this is in the resource view file.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Support</title>
</head>
<body>
<h1>{{$details['title']}}</h1>
<p>Dear {{$details['first_name'][0]}},</p>
<br>
<p>You ticket {{$details['ticket_id']}} has been updated to {{$details['status']}}. </p>
<br>
</p>Our Agent {{$details['agent_name'][0]}} is currently working on your request. We will get 
 back to you as soon as possible.</p>


<p>Thank you.</p>
<br>
<br>
<br>
<p>From,</p>
<br>
<p>Support</p>

And in the email received it's exactly like that with the $details when it's supposed to show the value.

This is in another email that works.

Same controller file.

else{
       \Mail::to($email)->send(new \App\Mail\NewTicketMail($details));
      }

NewTicketMail file in the App/Mail <?php

 namespace App\Mail;

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

 class NewTicketMail 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()
{
    return $this->subject('Mail from VeecoTech Support')->view('emails.new_ticket_mail');
}
}

And finally resource view file.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Support</title>
</head>
<body>
<h1>{{$details['title']}}</h1>
<p>Dear {{$details['first_name'][0]}},</p>
<br>
<p>You ticket {{$details['ticket_id']}} has been updated to {{$details['status']}}.</p>


<p>Thank you.</p>
<br>
<br>
<br>
<p>From,</p>
<br>
<p>Support</p>
</body>
</html>

The 2nd email works where values are shown.

CodePudding user response:

The reason of described behavior is missing .blade suffix in the name of the view file.

  • Related