Start using MailHooks in your Symfony app

Most of us, Symfony devs, are using Email delivery service like Mailjet 🇫🇷 (Sparkpost or , MailGun etc…).

Those email services are time-saver for our teams, providing scalability to our apps and benefits of their delivery expertise.

Let’s focus on two metrics:

Naturally, thoses services have a cost. Pricing are mostly based on emails sent per month or contacts in marketing campaigns.

Bounce rate, spam complaint, policy rejection or open rate, thoses data have impacts on your IP reputation and emails volumes.

Let’s optimize these metrics by using the power of Mailhooks and MailHookBundle!

First, define our scenarios that will help us to have a better deliverability and growth hacking:

The user spam complains about one of our email? 
Remove it from your mailing list to avoid regular complain.

The user mail account’s quota has been exceeded and your mail is bounced? 
Change the notification channel from mail to mobile push or invite the contact to update his mail address with a private message.

A customer mail address is no longer available? 
Let’s warning the account manager related to this contact.

When we have define some scenarios, we wan’t to catch the events from our users email 🚀.

Now let’s configure SwmMailHookBundle to catch those MailHooks:

Add the package to your composer.json file

composer require scullwm/mailhookbundle

Add this to app/AppKernel.php

public function registerBundles()
$bundles = array(
new Swm\Bundle\MailHookBundle\SwmMailHookBundle(),
        return $bundles;

Now let’s configure it:

1) Edit app/config.yml

secretsalt: notSecret

This secret salt is simply used to obfuscate the mailhook endpoint like:{secretSalt}/{serviceName}/catch

2) Edit app/routing.yml

resource: "@SwmMailHookBundle/Controller/"
type: annotation
prefix: /

In fact, you can avoid this routing declaration and declare your own endpoint. Then you will have this kind of route:


Where servicename might be: 
- campainmonitor for
- mailgun for
- mailjet for
- mandrill for
- sendgrid for
- sparkpost for

Last step of configuration is on your email provider service. You have to configure the webhook url and which event you want.

Sparkpost configuration for example.

Perfect! Each Mailhook will now hit your route and dispatch different events in your app. You just have to implement your own listeners on those events name:

Event implements HookEventInterface where you can easily get Email.

Here’s a simple implementation:

Now, let’s catch them all!

Like what you read? Give Thomas P a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.