Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add a mailer:test command #47040

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
use Symfony\Component\Mailer\Bridge\Postmark\Transport\PostmarkTransportFactory;
use Symfony\Component\Mailer\Bridge\Sendgrid\Transport\SendgridTransportFactory;
use Symfony\Component\Mailer\Bridge\Sendinblue\Transport\SendinblueTransportFactory;
use Symfony\Component\Mailer\Command\MailerTestCommand;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mercure\HubRegistry;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
Expand Down Expand Up @@ -386,6 +387,10 @@ public function load(array $configs, ContainerBuilder $container)
$this->registerMailerConfiguration($config['mailer'], $container, $loader);
}

if (!$this->mailerConfigEnabled || !class_exists(MailerTestCommand::class)) {
$container->removeDefinition('console.command.mailer_test');
}

$propertyInfoEnabled = $this->isConfigEnabled($container, $config['property_info']);
$this->registerHttpCacheConfiguration($config['http_cache'], $container, $config['http_method_override']);
$this->registerEsiConfiguration($config['esi'], $container, $loader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

use Symfony\Component\Mailer\Command\MailerTestCommand;
use Symfony\Component\Mailer\EventListener\EnvelopeListener;
use Symfony\Component\Mailer\EventListener\MessageListener;
use Symfony\Component\Mailer\EventListener\MessageLoggerListener;
Expand Down Expand Up @@ -72,5 +73,11 @@
->set('mailer.message_logger_listener', MessageLoggerListener::class)
->tag('kernel.event_subscriber')
->tag('kernel.reset', ['method' => 'reset'])

->set('console.command.mailer_test', MailerTestCommand::class)
->args([
service('mailer.transports'),
])
->tag('console.command')
;
};
5 changes: 5 additions & 0 deletions src/Symfony/Component/Mailer/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.2
---

* Add a `mailer:test` command

6.1
---

Expand Down
75 changes: 75 additions & 0 deletions src/Symfony/Component/Mailer/Command/MailerTestCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?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 Symfony\Component\Mailer\Command;

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\Mailer\Transport\TransportInterface;
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
{
public function __construct(private TransportInterface $transport)
{
$this->transport = $transport;

parent::__construct();
}

protected function configure()
{
$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
);
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$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);
}

$this->transport->send($message);

return 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?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 Symfony\Component\Mailer\Tests\Command;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Mailer\Command\MailerTestCommand;
use Symfony\Component\Mailer\Transport\TransportInterface;
use Symfony\Component\Mime\Email;

class MailerTestCommandTest extends TestCase
{
public function testSendsEmail()
{
$from = '[email protected]';
$to = '[email protected]';
$subject = 'Foobar';
$body = 'Lorem ipsum dolor sit amet.';

$mailer = $this->createMock(TransportInterface::class);
$mailer
->expects($this->once())
->method('send')
->with(self::callback(static function (Email $message) use ($from, $to, $subject, $body): bool {
return
$message->getFrom()[0]->getAddress() === $from &&
$message->getTo()[0]->getAddress() === $to &&
$message->getSubject() === $subject &&
$message->getTextBody() === $body
;
}))
;

$tester = new CommandTester(new MailerTestCommand($mailer));
$tester->execute([
'to' => $to,
'--from' => $from,
'--subject' => $subject,
'--body' => $body,
]);
}

public function testUsesCustomTransport()
{
$transport = 'foobar';

$mailer = $this->createMock(TransportInterface::class);
$mailer
->expects($this->once())
->method('send')
->with(self::callback(static function (Email $message) use ($transport): bool {
return $message->getHeaders()->getHeaderBody('X-Transport') === $transport;
}))
;

$tester = new CommandTester(new MailerTestCommand($mailer));
$tester->execute([
'to' => '[email protected]',
'--transport' => $transport,
]);
}
}