Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by
  1. Home
  2. Documentation
  3. Webhook

Webhook

Warning: You are browsing the documentation for Symfony 6.3, which is no longer maintained.

Read the updated version of this page for Symfony 7.1 (the current stable version).

6.3

The Webhook component was introduced in Symfony 6.3.

The Webhook component is used to respond to remote webhooks to trigger actions in your application. This document focuses on using webhooks to listen to remote events in other Symfony components.

Installation

1
$ composer require symfony/webhook

Usage in Combination with the Mailer Component

When using a third-party mailer, you can use the Webhook component to receive webhook calls from the third-party mailer.

In this example Mailgun is used with 'mailer_mailgun' as the webhook type. Any type name can be used as long as it is unique. Make sure to use it in the routing configuration, the webhook URL and the RemoteEvent consumer.

Install the third-party mailer as described in the documentation of the Mailer component.

The Webhook component routing needs to be defined:

1
2
3
4
5
6
7
# config/packages/framework.yaml
framework:
    webhook:
        routing:
            mailer_mailgun:
                service: 'mailer.webhook.request_parser.mailgun'
                secret: '%env(MAILER_MAILGUN_SECRET)%'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!-- config/packages/framework.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:framework="http://symfony.com/schema/dic/symfony"
           xsi:schemaLocation="http://symfony.com/schema/dic/services
                https://symfony.com/schema/dic/services/services-1.0.xsd
                http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
    <framework:config>
        <framework:webhook enabled="true">
            <framework:routing type="mailer_mailgun">
                <framework:service>mailer.webhook.request_parser.mailgun</framework:service>
                <framework:secret>%env(MAILER_MAILGUN_SECRET)%</framework:secret>
            </framework:routing>
        </framework:webhook>
    </framework:config>
</container>
1
2
3
4
5
6
7
8
9
10
11
// config/packages/framework.php
use App\Webhook\MailerWebhookParser;
use Symfony\Config\FrameworkConfig;
return static function (FrameworkConfig $frameworkConfig): void {
    $webhookConfig = $frameworkConfig->webhook();
    $webhookConfig
        ->routing('mailer_mailgun')
        ->service('mailer.webhook.request_parser.mailgun')
        ->secret('%env(MAILER_MAILGUN_SECRET)%')
    ;
};

Currently, the following third-party mailer services support webhooks:

Mailer service Parser service name
Mailgun mailer.webhook.request_parser.mailgun
Postmark mailer.webhook.request_parser.postmark

Set up the webhook in the third-party mailer. For Mailgun, you can do this in the control panel. As URL, make sure to use the /webhook/mailer_mailgun path behind the domain you're using.

Mailgun will provide a secret for the webhook. Add this secret to your .env file:

1
MAILER_MAILGUN_SECRET=your_secret

With this done, you can now add a RemoteEvent consumer to react to the webhooks:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
use Symfony\Component\RemoteEvent\Attribute\AsRemoteEventConsumer;
use Symfony\Component\RemoteEvent\Consumer\ConsumerInterface;
use Symfony\Component\RemoteEvent\Event\Mailer\MailerDeliveryEvent;
use Symfony\Component\RemoteEvent\Event\Mailer\MailerEngagementEvent;
use Symfony\Component\RemoteEvent\RemoteEvent;

#[AsRemoteEventConsumer('mailer_mailgun')]
class WebhookListener implements ConsumerInterface
{
    public function consume(RemoteEvent $event): void
    {
        if ($event instanceof MailerDeliveryEvent) {
            $this->handleMailDelivery($event);
        } elseif ($event instanceof MailerEngagementEvent) {
            $this->handleMailEngagement($event);
        } else {
            // This is not an email event
            return;
        }
    }

    private function handleMailDelivery(MailerDeliveryEvent $event): void
    {
        // Handle the mail delivery event
    }

    private function handleMailEngagement(MailerEngagementEvent $event): void
    {
        // Handle the mail engagement event
    }
}

Usage in Combination with the Notifier Component

The usage of the Webhook component when using a third-party transport in the Notifier is very similar to the usage with the Mailer.

Currently, the following third-party SMS transports support webhooks:

SMS service Parser service name
Twilio notifier.webhook.request_parser.twilio

6.3

The support for Twilio was introduced in Symfony 6.3.

For SMS transports, an additional SmsEvent is available in the RemoteEvent consumer:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
use Symfony\Component\RemoteEvent\Attribute\AsRemoteEventConsumer;
use Symfony\Component\RemoteEvent\Consumer\ConsumerInterface;
use Symfony\Component\RemoteEvent\Event\Sms\SmsEvent;
use Symfony\Component\RemoteEvent\RemoteEvent;

#[AsRemoteEventConsumer('notifier_twilio')]
class WebhookListener implements ConsumerInterface
{
    public function consume(RemoteEvent $event): void
    {
        if ($event instanceof SmsEvent) {
            $this->handleSmsEvent($event);
        } else {
            // This is not an sms event
            return;
        }
    }

    private function handleSmsEvent(SmsEvent $event): void
    {
        // Handle the sms event
    }
}
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version

    Symfony 6.3 is backed by

    Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

    Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

    Put the code quality back at the heart of your project

    Put the code quality back at the heart of your project

    Version:

    Table of Contents

    • Installation
    • Usage in Combination with the Mailer Component
    • Usage in Combination with the Notifier Component

    Symfony footer

    Avatar of sparrowek, a Symfony contributor

    Thanks sparrowek for being a Symfony contributor

    1 commit • 2 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • What is Symfony?
      • Symfony at a Glance
      • Symfony Components
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • Symfony Community
      • SymfonyConnect
      • Events & Meetups
      • Projects using Symfony
      • Contributors
      • Symfony Jobs
      • Backers
      • Code of Conduct
      • Downloads Stats
      • Support
    • Blog

      • All Blog Posts
      • A Week of Symfony
      • Case Studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Living on the edge
      • Releases
      • Security Advisories
      • Symfony Insight
      • Twig
      • SensioLabs Blog
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Powered by

    Follow Symfony