Error sending email form

0

I have the following problem I made a form of sending email but it gives an error follows the image of the error and the code obs: I am using laravel HTML:

<div class="col-md-6 block light text-center wow fadeInRightBig" data-wow-duration="500ms" data-wow-delay="300ms">
            <div class="center">
                <p class="title">Preencha o formulário para</p>
                <h2>Entrar em Contato</h2>
                @if (count($errors) > 0)
                    <div class="alert alert-danger">
                        <ul>
                            @foreach ($errors->all() as $error)
                                <li>{{ $error }}</li>
                            @endforeach
                        </ul>
                    </div>
                @endif

                @if (session('message'))
                    <div class="alert alert-success">
                        {!! session('message')  !!}
                    </div>
                @endif
                {!! Form::open(array('action' => 'FormController@postContato', 'role' => 'form', 'class'=>'form-inline')) !!}
                {!! Form::text('nome', null, array('placeholder'=>'Digite nome', 'class'=>'form-control')) !!}
                {!! Form::text('email', null, array('placeholder'=>'Digite e-mail', 'class'=>'form-control')) !!}
                {!! Form::text('telefone', null, array('placeholder'=>'Digite seu Telefone', 'class'=>'form-control')) !!}
                {!! Form::text('assunto', null, array('placeholder'=>'Assunto', 'class'=>'form-control')) !!}
                {!! Form::textarea('mensagem', null, array('placeholder'=>'Mensagem', 'class'=>'form-control', 'rows'=>'5')) !!}
                {!! Form::submit('Enviar Mensagem', array('class' => 'btn-black btn-blue bounce-green'), array('id' => 'btn_submit')) !!}
                {!! Form::close() !!}
            </div>
        </div>
    </div>

The body of the message:

  • Name: {!! $ name !!}
  •     
  • Email: {!! $ email !!}
  •     
  • Phone: {!! $ phone !!}
  •     
  • Subject: {!! $ subject !!}
  •     
  • Message: {!! $ message !!}

o Laravel:     

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Validator;
use Mail;
use App\Http\Requests;

class FormController extends Controller
{
    public function contato() {
        return view('contato');
    }

    public function postContato(Request $request) {
        $rules = array( 'nome' => 'required', 'email' => 'required|email', 'telefone' => 'required', 'assunto' => 'required', 'mensagem' => 'required' );
        $errors = [
            'required'    => 'O campo :attribute é obrigatório.',
            'email'    => 'Digite um email válido.',
        ];
        $validation = Validator::make($request->all(), $rules, $errors);
        $data = array();
        $data['nome'] = $request->input("nome");
        $data['email'] = $request->input("email");
        $data['telefone'] = $request->input("telefone");
        $data['assunto'] = $request->input("assunto");
        $data['mensagem'] = $request->input("mensagem");


        if($validation->passes()) {
                Mail::send('emails.contato', $data, function($message) {
                    $message->from($request->input("email"), $request->input("nome"));
                    $message->to('[email protected]') ->subject('TorreForte');

                });
            return redirect('contato')->with('message', 'Mensagem enviada com sucesso!');
        }
        return redirect('contato')->withErrors($validation);
    }
}

The error:

    
asked by anonymous 24.08.2016 / 19:59

1 answer

2

From a anonymous functions and php function scope.

The problem is that the function that you pass as a parameter in the function Mail::send() scope is different, and variable $request , that you use it in, does not exist.

To work you need to tell php to "inject" the variable $request into the scope of the function, this is done with use .

Mail::send('emails.contato', $data, function($message) use($request) {
    seu codigo....
}
    
24.08.2016 / 20:10