Back to symfony.com

Demo 9 of 10 · 2 min

Click any paragraph or press to highlight its code

Receiving webhooks

Mailgun wants to tell you an email bounced; Stripe, that a payment succeeded. With these 4 lines of config, Symfony exposes a /webhook/mailgun endpoint and verifies the request signature for you. You write zero controller code and zero crypto code.

The payload is parsed into a typed remote event and handed to your consumer class. This attribute is the only wiring needed.

Then react with plain PHP. Parsers for the mailer and SMS providers are built in, and writing your own parser class (for Stripe, GitHub, ...) also lets Symfony handle the endpoint and the signature checks for it.

framework:    webhook:        routing:            mailgun:                service: 'mailer.webhook.request_parser.mailgun'                secret: '%env(MAILGUN_WEBHOOK_SECRET)%'
<?phpnamespace App\Webhook;use Symfony\Component\RemoteEvent\Attribute\AsRemoteEventConsumer;use Symfony\Component\RemoteEvent\Consumer\ConsumerInterface;use Symfony\Component\RemoteEvent\Event\Mailer\MailerDeliveryEvent;use Symfony\Component\RemoteEvent\RemoteEvent;#[AsRemoteEventConsumer('mailgun')]class EmailEventConsumer implements ConsumerInterface{    public function consume(RemoteEvent $event): void    {        if ($event instanceof MailerDeliveryEvent) {            // a bounced email: flag the address in your database            // and stop sending to it        }    }}