How to make Laravel app can interact with user by email using Webhook

Photo by Sergey Zolkin on Unsplash

I hope this post will help you if you need your app can interact with user by email, such as email approval, review from email etc. But how our app can get the content of the email that user replied to? In this case we will use a webhook.

Webhook

Webhook in web development is a method of augmenting or altering the behavior of a web page or web application with custom callbacks. These callbacks may be maintained, modified, and managed by third-party users and developers who may not necessarily be affiliated with the originating website or application.
— Wikipedia

How to use webhook?
In this, we need Mailgun to provides our webhook or if you know anything else you can leave a comment below.
Register into Mailgun

Register mailgun

Then open your email that you used to register and verify your email. Next step we will set up our with credential from mailgun.

MAIL_MAILER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=xxxxxxxxxxx
MAIL_PASSWORD=xxxxxxxxxxx
MAIL_ENCRYPTION=SSL
MAIL_FROM_ADDRESS="valid sender email address"
MAIL_FROM_NAME="${APP_NAME}"
MAILGUN_DOMAIN=xxxxxxxxxx

In this case I will use smtp and default domain from mailgun. Please feel free, if you want to use anything else. If you want to test emailing feature you can add a recipient email to the “Authorized Recipients”.

Go to webhook panel and add your webhook url.

Note : you can’t add a local url, in this I deploy my project into heroku for testing purpose.

Then create a webhook controller, you can name it anything. Create a function inside your controller.

public function emailWebHook(Request $request)
{
// Testing
print_r("Hello World!");
print_r($request->all());
}

after create a controller create a route for the webhook, in this case I will separate from the main route, so I create

<?phpuse App\Http\Controllers\WebHookController;Route::post('/email/webhook', [WebHookController::class, 'emailWebHook']);

Go to route service provider and add the webhook route in boot method.

Route::middleware('api')
->namespace($this->namespace)
->group(base_path('routes/webhook.php'));
RouteServiceProvider

You can do test for your webhook via mailgun.

Test webhook
Result

Or you can try to send an email from your app to your active email and try to reply it. Note : Make sure your active email has been added into the “Authorized Recipients”.

Then you can find the result in logs panel. Free version of mailgun can only show logs with 1 day retention period, you must wait till tomorrow to check the logs of today :D.

If you find it’s already successfull, now you can made your controller to do what you need such as save to database or trigger any events in your app.

That’s all that I can share in this post, hopefully this post can be useful. Share with other if you find this post is useful. Please feel free to leave a comment if you have any question or you want to discuss. See ya

Backend Developer | Laravel