简体   繁体   中英

PHP Laravel Send Email

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] => boby@hotmail.com [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('myemail@gmail.com', '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('otid91@gmail.com', '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>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM