2

I am sending email via Mailgun from a Laravel 5.5 app like this...

    Mail::send('emails.sendmessage', $data, function($message) use ($data) {
        $message->to($data['email']);
        $message->from('me@example.com.com', 'Ueer');
        $message->subject('Sample Email');
    });

    /* Return Success Response */
    return Response::json(array(
        'error' => false,
        'status_code' => 200,
        'response' => 'success',
    ));

How can I catch Mailgun errors with this code? Sometimes MailGun returns an error message and I would like to return a different response if this happens

fightstarr20
  • 11,682
  • 40
  • 154
  • 278

1 Answers1

3

You can take a look at this question : Laravel 5 - How Do You Catch an Mail::send() Error?

Adding a try and catch should do the trick.

If you don't want to add a try/catch for whatever reason, I would recommend you to validate beforehand every parameters susceptile of throwing an error from Mail::send

  • When I added a backslash to my try-catch construct, then I could handle the error:`try{ ... } catch (\Exception $e) { ... }` – Debbie V Oct 18 '18 at 19:34