Error sending email form

Asked

Viewed 80 times

0

I am with 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 THE 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 !!}
  • Telephone: {!! $telephone !!}
  • Asssunto: {!! $subject matter !!}
  • Message: {!! $message!!}

the 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 mistake: inserir a descrição da imagem aqui

  • What is line 33? you can put a comment (in the code) indicating it?

1 answer

2


Da a researched about Anonimas functions and php function scope.

The problem is that the function Voce passes as parameter in the function Mail::send() the scope is different, and variable $request, that you use inside, there is no.

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

Mail::send('emails.contato', $data, function($message) use($request) {
    seu codigo....
}
  • opa amigo agora o erro mudar Connection could not be established with host mailtrap.io [Connection refused #111]

  • 1

    ai has to check the email data that Windows has configrou there in app/config/email.php, by the way you set up to send via an external smpt and not with the default mail function of php, probably some data from Tools is wrong.

  • face you know too much see even by help amooo you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.