Inbound mail using Mailgun and laravel

Inbound mail using Mailgun and laravel

Many people ask about send e-mails using their own application, no one is interested to open mail application and send the e-mail using that.

But thanks to current technology which allows us to have the solution for every problem.

Many mail service provider like Mailgun provides the facility of Inbound Routing, which provides the solution which we need.

To go further, we need to understand inbound routing:

Let’s start with an example;

Have you ever tried how ticket mail system work?

When you purchase anything and if you don’t like then we used to send an e-mail to that company or the platform where you have placed an order. When we send them this query message we get an automatic system generated mail. This is nothing but Inbound Routing.

Mailgun provides such feature, by using that we can build the inbound routing.

Step by step procedure to implement inbound mail using Mailgun and Laravel:

Step 1: Setup the Mailgun account

You need to have a Mailgun account with DNS configured for inbound mail. You will also need to setup MX DNS records to receive inbound mail.

Once you set up the account you will get the API Key and API secret which we need to use in our application

 Step 2: Create Project

Let’s create laravel project and walk through setting up the webhooks on Mailgun and related core code in laravel.

We will use the php-imap package to send mail using Mailgun api,

In the first step, you need to create laravel project using command, before running this command,  confirm that you already installed composer on your system.

To create laravel project use below command

composer create-project laravel/laravel inboundmail --preferdist

After creating the project we need to install php-imap package, for that go to the project directory and run below command

composer require php-imap/php-imap

After successfully installing everything we need to configure Mailgun with laravel. To use Mailgun as the email provider, you need to update MAIL_DRIVER and add the Mailgun domain and secret values from your account:

            MAIL_DRIVER=Mailgun
            MAILGUN_DOMAIN=mg.example.com
            MAILGUN_SECRET=horizon

And that’s it for the setup. It’s time to write a Webhook route and quickly try it out!

Step 3: Setting up the webhook route in laravel

We need to create one webhook which will receive the email details when someone will mail to our specific email address.

Mailgun provides us data in JSON format so we need to define one route which doesn’t require any token.

For this, we need to add one route in routes/api.php as like below

Route::post('inbound-mail',function(Request $request){
            Log::info($request->all());
            return response()->json(['status' => 'ok']);
});

The above code will log the information which has been received from Mailgun.

 Step 4: Route setup on Mailgun account

We need to tell Mailgun to send mail details on a route which we just created.  For that, you need to go Mailgun account and have to create a new route for our application.

In recipient, you need to add mail from which you want to send the email.

Next, the store and notify rule will store the message and send the JSON payload to the webhook defined. You can specify multiple retrieval URLs with a comma in ‘store and notify box’!

Step 5: Now it’s time to receive email details from Maailgun

To receive the email details through webhook, we need to send mail using email id which we defined on route.

When sending the mail we will receive one json data on URL which we have added on webhook as below.

{
            'Content-Type' : 'multipart/alternative; boundary="001a113bcdb4e48b500565db69be"',
            'Subject' : 'Mailgun setup',
            'body-plain' => 'Hi This is mail test',
}

Finally, our Inbound mail using Mailgun and laravel is done.

Happy Coding!