Displaying custom error when sending Laravel email

By | October 30, 2018
Questions:

The code below works perfectly when the provided email in the second send function is correct $cm1[’email’], however, when the user enters a not working email Laravel error page is displayed and the condition I wrote that redirect(‘/cm’) with the error message doesn’t work. So my question is how to make Laravel redirect the user to the page with error flash message when something wrong happens while sending the email instead of displaying the error.

     Mail::send('email_contact', ['data' => $cm1], function ($message)
    {

        $message->from('noreply@mydomain.com', 'No Reply');
        $message->to('rec@gmail.com');

    });

    Mail::send('email_rec', ['data' => $cm1],function ($message) use ($cm1) {

        $message->from('noreply@mydomain.com', 'No Reply');
        $message->to( $cm1['email']);

    });
    if( count(Mail::failures()) > 0 ) {
        return redirect('/cm')->with('flash_message','<strong> Something went wrong! </strong> The entered email seems to be invalid Received');
    }

    return redirect('/cm')->with('flash_message','<strong> Thank You! </strong> Your Inquiry Has Been Received');
Answers:

Leave a Reply

Your email address will not be published. Required fields are marked *