From c6a7fe6101319a3d2167013f46aa9bda56de5bc3 Mon Sep 17 00:00:00 2001 From: Antoine Lamirault Date: Sun, 4 May 2025 14:21:38 +0200 Subject: [PATCH] [Mailer][Mime] Refactor S/MIME encryption handling in SMimeEncryptionListener --- mailer.rst | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/mailer.rst b/mailer.rst index d368048aec8..a562263f24c 100644 --- a/mailer.rst +++ b/mailer.rst @@ -1532,7 +1532,7 @@ encrypter that automatically applies to all outgoing messages: framework: mailer: smime_encrypter: - certificate: '%kernel.project_dir%/var/certificates/smime.crt' + repository: app.my_smime_encrypter .. code-block:: xml @@ -1549,7 +1549,7 @@ encrypter that automatically applies to all outgoing messages: - %kernel.project_dir%/var/certificates/smime.crt + app.my_smime_encrypter @@ -1563,10 +1563,35 @@ encrypter that automatically applies to all outgoing messages: return static function (FrameworkConfig $framework): void { $mailer = $framework->mailer(); $mailer->smimeEncrypter() - ->certificate('%kernel.project_dir%/var/certificates/smime.crt') + ->repository('app.my_smime_encrypter') ; }; +The ``repository`` option must be a service ID that implements +:class:`Symfony\\Component\\Mailer\\EventListener\\SmimeCertificateRepositoryInterface`:: + + namespace App\Security; + + use Symfony\Component\DependencyInjection\Attribute\Autowire; + use Symfony\Component\Mailer\EventListener\SmimeCertificateRepositoryInterface; + + class LocalFileCertificateRepository implements SmimeCertificateRepositoryInterface + { + public function __construct( + #[Autowire(param: 'kernel.project_dir')] + private readonly string $projectDir + ){} + + public function findCertificatePathFor(string $email): ?string + { + $hash = hash('sha256', strtolower(trim($email))); + $path = sprintf('%s/storage/%s.crt', $this->projectDir, $hash); + + return file_exists($path) ? $path : null; + } + } + + .. versionadded:: 7.3 Global message encryption configuration was introduced in Symfony 7.3.