dan3460's avatar

Mail send not working

I have look around on google but i think my problem is different of just misspelled or wrong setup file. Right now i have the .env setup for mailtrap, using Tinker i can send a simple email using Email::raw(). So the configuration is working. I have the following method to send an email:

    public function sendForm()
    {
        Mail::send('emails.contact',[
            'name'=>request('customerName'),
            'email'=>request('customerEmail'),
            'phone'=>request('customerPhone'),
            'msg'=>request('customerMessage'),
            'pref'=>request('commPreference')
        ], function($msg){
            $msg->from('[email protected]');
            $msg->to('[email protected]', 'Admin')->subject('Web Site Request');
        });
  
        //return redirect('/')->with('message', 'We will get back to you very soon....Thanks');
    }

I commented the return to make sure that i was hitting the method, i'm getting a blank page. So i'm hitting the method. i typed the mail command in tinker, replacing the requests with hard coded data and it worked. How i can troubleshoot that the method is actually calling the mail function?

0 likes
5 replies
Snapey's avatar

i would start with adding at the top of the method

dd([
            'name'=>request('customerName'),
            'email'=>request('customerEmail'),
            'phone'=>request('customerPhone'),
            'msg'=>request('customerMessage'),
            'pref'=>request('commPreference')
        ]);

this would show a) you hit the function, b) you get all the fields from the form

GertjanRoke's avatar

You could try to set a return before the Mail::send part. If I am correct Laravel will return the html from the mail template filled with your variables.

Maybe you can see what goes wrong there?

dan3460's avatar

getting the data:

array:5 [▼
  "name" => "Test"
  "email" => "[email protected]"
  "phone" => "123456"
  "msg" => "This is the message"
  "pref" => "phone"
]
ejdelmonico's avatar
Level 53

I assume you have guzzlehttp/guzzle in your composer.json in order to send mail. And, I assume you import the Mail facade. Then, change the method to send(), which overrides the provided send method. You can check to make sure that Mailtrap is really working by using Postman to send the email with the data your provided by hitting your route on localhost from Postman.

dan3460's avatar

@ejdelmonico, i swear that it was working before (i made the contact form about 2 weeks ago). I did not have guzzle installed. Embarrassing. Didn't have to change anything else, just install guzzle.

Thanks everyone.

Please or to participate in this conversation.