From 1e5c55514972495edfda2070753d8259c5ed2323 Mon Sep 17 00:00:00 2001 From: Christian Spoo Date: Thu, 28 Mar 2024 10:53:52 +0100 Subject: [PATCH] Use configured mail transport instead of MAILER_DSN --- .env | 2 +- config/services.yaml | 3 ++ examples/config.yaml | 2 +- src/Command/MailerTestCommand.php | 81 +++++++++++++++++++++++++++++++ src/Services/UserStorage.php | 7 +-- 5 files changed, 90 insertions(+), 5 deletions(-) create mode 100644 src/Command/MailerTestCommand.php diff --git a/.env b/.env index d2e7ef0..c5d1f43 100644 --- a/.env +++ b/.env @@ -20,5 +20,5 @@ APP_SECRET=7d51ea8178822362dc294317d8223d23 #TRUSTED_HOSTS='^localhost|example\.com$' ###< symfony/framework-bundle ### ###> symfony/mailer ### -MAILER_DSN=sendmail://localhost +MAILER_DSN=null://null ###< symfony/mailer ### diff --git a/config/services.yaml b/config/services.yaml index 0086b1d..73e6296 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -20,6 +20,9 @@ services: resource: '../src/*' exclude: '../src/{DependencyInjection,Entity,Migrations,Model,Platform/Platform.php,Tests,Kernel.php}' + console.command.mailer_test: + class: Mfc\PasswordManager\Command\MailerTestCommand + # Symfony\Component\Mailer\MailerInterface: # factory: ['@Mfc\PasswordManager\Services\Mail\MailerFactory', 'buildMailer'] # lazy: true diff --git a/examples/config.yaml b/examples/config.yaml index 50a18cc..12a76cd 100644 --- a/examples/config.yaml +++ b/examples/config.yaml @@ -1,6 +1,6 @@ mfc_password_manager: mail: - dsn: "smtp://srelay-int.web-factory.de" + dsn: "smtp://srelay-ext.web-factory.de:25?verify_peer=false" database: default_credentials: username: db_pusher_user diff --git a/src/Command/MailerTestCommand.php b/src/Command/MailerTestCommand.php new file mode 100644 index 0000000..74aa210 --- /dev/null +++ b/src/Command/MailerTestCommand.php @@ -0,0 +1,81 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Mfc\PasswordManager\Command; + +use Mfc\PasswordManager\Services\ConfigurationService; +use Mfc\PasswordManager\Services\Mail\MailerFactory; +use Symfony\Component\Console\Attribute\AsCommand; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Mime\Email; + +/** + * A console command to test Mailer transports. + */ +#[AsCommand(name: 'mailer:test', description: 'Test Mailer transports by sending an email')] +final class MailerTestCommand extends Command +{ + use ConfigDirectoryTrait; + + public function __construct( + private readonly ConfigurationService $configurationService, + private readonly MailerFactory $mailerFactory + ) { + parent::__construct(); + } + + protected function configure(): void + { + $this + ->addArgument('to', InputArgument::REQUIRED, 'The recipient of the message') + ->addOption('from', null, InputOption::VALUE_OPTIONAL, 'The sender of the message', 'from@example.org') + ->addOption('subject', null, InputOption::VALUE_OPTIONAL, 'The subject of the message', 'Testing transport') + ->addOption('body', null, InputOption::VALUE_OPTIONAL, 'The body of the message', 'Testing body') + ->addOption('transport', null, InputOption::VALUE_OPTIONAL, 'The transport to be used') + ->setHelp(<<<'EOF' +The %command.name% command tests a Mailer transport by sending a simple email message: + +php %command.full_name% to@example.com + +You can also specify a specific transport: + + php %command.full_name% to@example.com --transport=transport_name + +Note that this command bypasses the Messenger bus if configured. + +EOF + ) + ->configureConfigDirectory(); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $this->loadConfiguration($this->configurationService, $input); + + $message = (new Email()) + ->to($input->getArgument('to')) + ->from($input->getOption('from')) + ->subject($input->getOption('subject')) + ->text($input->getOption('body')); + if ($transport = $input->getOption('transport')) { + $message->getHeaders()->addTextHeader('X-Transport', $transport); + } + + $mailer = $this->mailerFactory->buildMailer(); + $mailer->send($message); + + return 0; + } +} diff --git a/src/Services/UserStorage.php b/src/Services/UserStorage.php index 5ff3c4b..f03c808 100644 --- a/src/Services/UserStorage.php +++ b/src/Services/UserStorage.php @@ -11,11 +11,11 @@ use Mfc\PasswordManager\Security\Encoder\Md5PasswordEncoder; use Mfc\PasswordManager\Security\Encoder\PasswordEncoderInterface; use Mfc\PasswordManager\Security\Encoder\Sha1PasswordEncoder; +use Mfc\PasswordManager\Services\Mail\MailerFactory; use Psr\Log\LoggerInterface; use Symfony\Bridge\Twig\Mime\NotificationEmail; use Symfony\Component\Finder\Finder; use Symfony\Component\Finder\SplFileInfo; -use Symfony\Component\Mailer\MailerInterface; use Symfony\Component\Mime\Address; use Symfony\Component\Serializer\SerializerInterface; use Symfony\Component\Yaml\Yaml; @@ -45,8 +45,8 @@ class UserStorage */ public function __construct( private ConfigurationService $configurationService, + private readonly MailerFactory $mailerFactory, private readonly SerializerInterface $serializer, - private readonly MailerInterface $mailer, private readonly LoggerInterface $logger ) { } @@ -214,7 +214,8 @@ private function sendPlainPasswordToUser(User $user, string $plainPassword): voi ) ->importance(NotificationEmail::IMPORTANCE_HIGH); - $this->mailer->send($email); + $mailer = $this->mailerFactory->buildMailer(); + $mailer->send($email); } public function enableDryRun(): self