-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use configured mail transport instead of MAILER_DSN
- Loading branch information
Showing
5 changed files
with
90 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters