4

I'm pretty new with php and Laravel and I'm trying to send emails, but I have some problems with my mail blade template.

In my controller, I have my request received from my form and storing my informations inside an array like this...

$data = array(
    'name' => $request->name,
    'mail'=> $request->mail,
    'message'=> $request->message,
    'category'=> $request->category,
    'company'=>$request->company
);

if I do a print_r of my variable $data, it print something like this

Array ( [name] => Boby [mail] => [email protected] [message] => call me back [category] => 2 [company] => Test )

I then send my email using this part of code

Mail::send('mail', $data, function($message) use ($fromEmail, $fromName)
{   
    $message->from($fromEmail, $fromName);
    $message->to('[email protected]', 'myname')->subject('subject');
 );

but when it arrive at the blade view, I'm getting this error...

    ErrorException (E_ERROR)
Use of undefined constant name - assumed 'name' (this will throw an Error in a future version of PHP) (View: /home/dave/NetBeansProjects/InspectionBelaire/resources/views/mail.blade.php)

Here is my full post and blade view

MailController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Routing\Controller as BaseController;
use Illuminate\Http\Request;
use Mail;

class MailController extends BaseController
{
    public function send(Request $request) {
            $fromEmail = $request->mail;
            $fromName = $request->name;

            $data = array(
                'name' => $request->name,
                'mail'=> $request->mail,
                'message'=> $request->message,
                'category'=> $request->category,
                'company'=>$request->company
            );


            Mail::send('mail', $data, function($message) use ($fromEmail, $fromName)
            {   
                $message->from($fromEmail, $fromName);
                $message->to('[email protected]', 'dotis')->subject('Nouvelle soumission en ligne');
            });
        }
}

mail.blade.php

<html>
<head>
    <meta charset="utf-8">
</head>
<body>

<div>
  {{name}}
  {{mail}}
  {{message}}
  {{category}}
  {{company}}
</div>

</body>
</html>
3
  • 2
    You don't have $ in front of your variables in your blade file. Commented May 25, 2018 at 19:48
  • use $data['name']. Commented May 25, 2018 at 19:50
  • Any code between the {{ .. }} tags in your Blade file is interpreted as PHP code to be echoed. So variables need to have the $ character before variable names as in PHP. So {{ $name }} is compiled to <?php echo $name; }}, that's why <?php echo name ?> throws an error because it's not valid PHP code (and the interpreter when evaluating echo name snippet assumes that identifiers without a $ are constants). Commented May 25, 2018 at 19:50

1 Answer 1

2

Laravel uses the Blade templating engine and any code in between {{ }} is basically run as follows:

<?php

echo [whatever you entered];

?>

So in your case, all you need to do is modify your blade view:

<html>
    <head>
        <meta charset="utf-8">
    </head>
    <body>

        <div>
          {{ $name }}
          {{ $mail }}
          {{ $message }}
          {{$category }}
          {{ $company }}
        </div>

    </body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.