Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Laravel 5, how can I find out if a Mailgun email was sent successfully?

I'm using the native Mailgun driver in Laravel 5 to send an email

\Mail::send('emails.notification', $data, function($message) use ($data)
{
  $message->to('[email protected]', 'Joe Bob')->subject("Headline here");
});

That works well and the emails are being received, but I would like to know how I can get a response from Mailgun letting me know that the email was sent.

How can I go about getting that information?

like image 351
zeckdude Avatar asked Sep 25 '22 04:09

zeckdude


1 Answers

To know the status of the email you can use Mailgun webhooks. From the Mailgun admin area you can specify a webhook url that Mailgun will post to after they have processed your email.

Ensure that whatever url you use as a webhook exists in your application and has a route and method in whichever controller you are using. Once your application receives the posted data you can parse the data sent and determine the status of the email. You can then update your database or whatever you need to do.

To identify which email you will need to add some custom headers in the email so you can identify it later on.

see this answer for more details on how to do that: Using Laravel's Mailgun driver, how do you (gracefully) send custom data and tags with your email?

The webhook url needs to be live for it to work, so if you are testing locally with laravell homestead you can take a look at ngrok, which allows you to tunnel your local environment to a url which will work for testing purposes.

like image 111
Andrew Bibby Avatar answered Sep 29 '22 07:09

Andrew Bibby