Skip to content

Commit

Permalink
Use configured mail transport instead of MAILER_DSN
Browse files Browse the repository at this point in the history
  • Loading branch information
tehplague committed Mar 28, 2024
1 parent 641bce5 commit 1e5c555
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -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 ###
3 changes: 3 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/config.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
81 changes: 81 additions & 0 deletions src/Command/MailerTestCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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', '[email protected]')
->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 <info>%command.name%</info> command tests a Mailer transport by sending a simple email message:
<info>php %command.full_name% [email protected]</info>
You can also specify a specific transport:
<info>php %command.full_name% [email protected] --transport=transport_name</info>
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;
}
}
7 changes: 4 additions & 3 deletions src/Services/UserStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
) {
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1e5c555

Please sign in to comment.