Support the ongoing development of Laravel.io →
posted 7 years ago

Hey, guys so I have everything set up properly my emails are sending and everything but there is one simple problem I'm that's annoying me.

ContactController.php

public function xdcSendEmail(){
    $post = (object) $_POST;
    if(Mail::to($post->email)->send(new OrderReceived($post))){
        echo "Thank you";
    } else {
        echo "Sorry we could not send your order at this time please try again.";
    }
}

My goal is if the email goes through, display "Thank you". Instead, it displays "Sorry we could not send...." although the email sends successfully which means the first statement is true. Is there any other way of getting "Thank You" to show if the email goes through?

Last updated 2 years ago.
0

What is Mail returning in that IF statement?

0

Hey, @GhostMech thanks for your reply, I found a solution online.

Mail::send()

Method does not return anything so in my case when I'm using it inside the if () statement it never returns anything true so by default it returns false. That's why I am getting the else () statement returned.

So in order to get something like this to work, you would have to use an if() statement like this

$post = (object) $_POST;
Mail::to($post->email)->send(new OrderReceived($post));

if (count(Mail::failures() < 0)) {
    echo "Thank you mail sent";
} else {
    echo "There was an error";
}
Mail::failures

Returns an array of failed recipients

Just in case anyone else runs into this problem.

Last updated 7 years ago.
0

Cool. Thanks for sharing. I always inspect what my IF statements return. Some PHP functions don't always return boolean, so you have to evaluate with something like if ($var === true), or something like that.

Glad you got it solved.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.