Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Webhook] Added component documentation for use in combination with Mailer #19249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 1 index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Topics
translation
validation
web_link
webhook
workflow

Components
Expand Down
8 changes: 8 additions & 0 deletions 8 mailer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ native ``native://default`` Mailer uses the sendmail
It's highly recommended to NOT use ``native://default`` as you cannot control
how sendmail is configured (prefer using ``sendmail://default`` if possible).

.. _mailer_3rd_party_transport:

Using a 3rd Party Transport
~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -277,6 +279,12 @@ party provider:
# .env
MAILER_DSN=smtp://KEY:DOMAIN@smtp.eu.mailgun.org.com:25

.. tip::

Some third party mailers, when using the API, support status callback
via webhooks. See the :doc:`Webhook documentation </webhook>` for more
details.

High Availability
~~~~~~~~~~~~~~~~~

Expand Down
5 changes: 5 additions & 0 deletions 5 reference/attributes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ Messenger

* :ref:`AsMessageHandler <messenger-handler>`

RemoteEvent
~~~~~~~~~~~

* :ref:`AsRemoteEventConsumer <webhook>`

Routing
~~~~~~~

Expand Down
10 changes: 10 additions & 0 deletions 10 reference/configuration/framework.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3625,6 +3625,16 @@ enabled

Adds a `Link HTTP header`_ to the response.

webhook
~~~~~~~

.. versionadded:: 6.3

The Webhook configuration was introduced in Symfony 6.3.

The ``webhook`` option (and its children) are used to configure the webhooks
defined in your application. Read more about the options in the :ref:`Webhook documentation <webhook>`.

workflows
~~~~~~~~~

Expand Down
132 changes: 132 additions & 0 deletions 132 webhook.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
Webhook
=======

.. versionadded:: 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
------------

.. code-block:: terminal

$ 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 webhook type.
Any type name can be used as long as it's 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
:ref:`Mailer component <mailer_3rd_party_transport>`.

The Webhook component routing needs to be defined:

.. configuration-block::

.. code-block:: yaml

# config/packages/framework.yaml
framework:
webhook:
routing:
mailer_mailgun:
service: 'mailer.webhook.request_parser.mailgun'
secret: '%env(MAILER_MAILGUN_SECRET)%'

.. code-block:: xml

<!-- 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>

.. code-block:: php

// 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:

.. code-block:: env

MAILER_MAILGUN_SECRET=your_secret

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

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')]
final readonly 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
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.