Trying to get Property 'email' of non-object

Asked

Viewed 935 times

0

I am trying to send an email from a contact form. But it returns the error "Trying to get Property 'email' of non-object".

Controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Mail\ContatoEmail;
use Illuminate\Support\Facades\Mail;
use App\Http\Requests\ContactFormRequest;

class MailController extends Controller {

  public function create()
    {
        return view('home');
    }

    public function store(Request $request)
    {
      $contact = [];

      $contact['nome'] = $request->get('nome');
      $contact['email'] = $request->get('email');
      $contact['msg'] = $request->get('msg');

      Mail::to(config('mail.support.address'))->send(new ContatoEmail($contact));

      flash('Sua mensagem foi enviada com sucesso!')->success();

      return redirect()->route('home');
    }

}

View

{!! Form::open(['route' => 'home.store', 'class' => 'form']) !!}

    <div class="form-group">
      {!! Form::label('nome', 'Nome') !!}
      {!! Form::text('nome', null, ['class' => 'form-control']) !!}
    </div>

    <div class="form-group">
      {!! Form::label('email', 'E-mail') !!}
      {!! Form::text('email', null, ['class' => 'form-control']) !!}
    </div>

    <div class="form-group">
      {!! Form::label('msg', 'Descreva seu projeto') !!}
      {!! Form::textarea('msg', null, ['class' => 'form-control']) !!}
    </div>

    <div class="form-group">
      {!! Form::submit('Enviar', ['class' => 'btn btn-primary btn-lg btn-block']) !!}
    </div>


    {!! Form::close() !!}

App Mail

<?php

namespace App\Mail;

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

class ContatoEmail extends Mailable
{
  use Queueable, SerializesModels;

  public $contact;

  /**
   * Create a new message instance.
   *
   * @return void
   */
  public function __construct($contact)
  {
      $this->contact = $contact;
  }

  /**
   * Build the message.
   *
   * @return $this
   */
  public function build()
  {
      return $this
          ->to(config('mail.support.address'))
          ->subject('Pedido orçamento site')
          ->view('emails.contact');
  }
}

Route

Route::get('home', 'MailController@create')->name('contact');
Route::post('home', 'MailController@store')->name('home.store');

Error image inserir a descrição da imagem aqui

1 answer

-2

I never used the E-mail Trigger, but this one worked for me

 $dados = array('email' => 'destinatário', 'assunto' => 'assunto do email', 'from' => remetente' );
 Mail::send('sua_view/mensagem', ['' => ''], function($message) use ($dados) {
                $message->to($dados['email'], 'Nome da Pessoa')->subject($dados['assunto']);
                $message->from($dados['remetente'],'Descrição do email');

Browser other questions tagged

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