Skip to content

Conversation

@nxtpge
Copy link

@nxtpge nxtpge commented Oct 26, 2025

Q A
Branch? 8.1
Bug fix? no
New feature? yes
Deprecations? no
Issues
License MIT

Until now, the Mailer component provided two ways to configure recipients globally:

  • a mailer.envelope.recipients option for static ones,
  • a MessageEvent event for dynamic ones.

As a new developer joining an existing Symfony project and assigned to work with the Mailer component, your first action would be to look at the config/packages/mailer.yaml file.

However, if the recipients were configured through an EventListener, you would need more time to be aware of its existence.

Today, in order to improve the developer experience, I propose to add a mailer.envelope.recipient_fetcher option that allows fetching global recipients from a service, and therefore from external sources (database, LDAP, etc.).

Example

config/packages/mailer.yaml
framework:
    mailer:
        envelope:
            recipients:
                - redirected@example.org
            recipient_fetcher: App\Mailer\AdminRecipientFetcher # Overwrites the "recipients" option's addresses
src/Mailer/AdminRecipientFetcher.php
<?php

namespace App\Mailer;

use App\Entity\User;
use App\Repository\UserRepository;
use Symfony\Component\Mailer\Envelope\RecipientFetcherInterface;
use Symfony\Component\Mime\Address;

class AdminRecipientFetcher implements RecipientFetcherInterface
{
   public function __construct(
       private UserRepository $userRepository,
   ) {
   }

   /**
    * @return array<Address|string>
    */
   public function fetchRecipients(): array
   {
       $admins = $this->userRepository->findAdmins();

       return $admins
           ->map(static fn (User $user): Address => new Address($user->getEmail()))
           ->toArray();
   }
}

Acknowledgements

  • @Spomky who inspired me with the idea of a "fetcher" (pull request),
  • @weaverryan who helped me to understand the meaning of the "autowire" and "autoconfigure" options (SymfonyCasts course),
  • api-platform/core and lexik/LexikJWTAuthenticationBundle which taught me how to check in the configuration if a class implements an interface,
  • the Symfony documentation which taught me how the validation rules worked in the configuration.

@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has a contribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 7.4 branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@nxtpge nxtpge force-pushed the mailer-recipient-fetcher branch from ef04c78 to 7213ed3 Compare October 26, 2025 20:00
@nxtpge
Copy link
Author

nxtpge commented Oct 26, 2025

For the failing checks, I do not understand why the RecipientFetcherInterface interface cannot be found while the problem does not occur in the other ones.

Copy link
Member

@GromNaN GromNaN left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the failing checks, I do not understand why the RecipientFetcherInterface interface cannot be found while the problem does not occur in the other ones.

The failing jobs run the tests after installing the dependencies of src/Symfmony/Bundle/FrameworkBundle/composer.json with lowest and highest versions constraint. Currently the new classes doesn't exist in symfony/mailer in any version (v6.4.0 and 8.0.x-dev 46faf03 are tested).

@nxtpge nxtpge force-pushed the mailer-recipient-fetcher branch from 7213ed3 to ac6a3c9 Compare October 27, 2025 05:58
@nxtpge
Copy link
Author

nxtpge commented Oct 27, 2025

The failing jobs run the tests after installing the dependencies of src/Symfmony/Bundle/FrameworkBundle/composer.json with lowest and highest versions constraint. Currently the new classes doesn't exist in symfony/mailer in any version (v6.4.0 and 8.0.x-dev 46faf03 are tested).

Thank you @GromNaN for giving me the explanation and the hints I needed to make the checks pass.

@nxtpge nxtpge changed the title [Mailer] Add recipient_fetcher option that allows fetching global recipients from a service [FrameworkBundle][Mailer] Add recipient_fetcher option that allows fetching global recipients from a service Nov 14, 2025
@nicolas-grekas nicolas-grekas modified the milestones: 7.4, 8.1 Nov 16, 2025
@nicolas-grekas
Copy link
Member

nicolas-grekas commented Nov 17, 2025

Rebase needed (note that the changelog entry should be moved to a 8.1 section and that the xsd file is gone in 8.x)

@nxtpge nxtpge force-pushed the mailer-recipient-fetcher branch 6 times, most recently from 0919ad1 to 159daa2 Compare November 17, 2025 18:22
@nxtpge nxtpge force-pushed the mailer-recipient-fetcher branch from 159daa2 to 3ef3991 Compare November 17, 2025 18:29
@nxtpge
Copy link
Author

nxtpge commented Nov 17, 2025

@nicolas-grekas Rebase and changes are done. Failing checks do not seem related.

@nxtpge nxtpge requested a review from GromNaN November 28, 2025 23:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants