| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\Mailer\Command;
|
|
|
13 |
|
|
|
14 |
use Symfony\Component\Console\Attribute\AsCommand;
|
|
|
15 |
use Symfony\Component\Console\Command\Command;
|
|
|
16 |
use Symfony\Component\Console\Input\InputArgument;
|
|
|
17 |
use Symfony\Component\Console\Input\InputInterface;
|
|
|
18 |
use Symfony\Component\Console\Input\InputOption;
|
|
|
19 |
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
20 |
use Symfony\Component\Mailer\Transport\TransportInterface;
|
|
|
21 |
use Symfony\Component\Mime\Email;
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
* A console command to test Mailer transports.
|
|
|
25 |
*/
|
|
|
26 |
#[AsCommand(name: 'mailer:test', description: 'Test Mailer transports by sending an email')]
|
|
|
27 |
final class MailerTestCommand extends Command
|
|
|
28 |
{
|
|
|
29 |
public function __construct(private TransportInterface $transport)
|
|
|
30 |
{
|
|
|
31 |
$this->transport = $transport;
|
|
|
32 |
|
|
|
33 |
parent::__construct();
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
protected function configure()
|
|
|
37 |
{
|
|
|
38 |
$this
|
|
|
39 |
->addArgument('to', InputArgument::REQUIRED, 'The recipient of the message')
|
|
|
40 |
->addOption('from', null, InputOption::VALUE_OPTIONAL, 'The sender of the message', 'from@example.org')
|
|
|
41 |
->addOption('subject', null, InputOption::VALUE_OPTIONAL, 'The subject of the message', 'Testing transport')
|
|
|
42 |
->addOption('body', null, InputOption::VALUE_OPTIONAL, 'The body of the message', 'Testing body')
|
|
|
43 |
->addOption('transport', null, InputOption::VALUE_OPTIONAL, 'The transport to be used')
|
|
|
44 |
->setHelp(<<<'EOF'
|
|
|
45 |
The <info>%command.name%</info> command tests a Mailer transport by sending a simple email message:
|
|
|
46 |
|
|
|
47 |
<info>php %command.full_name% to@example.com</info>
|
|
|
48 |
|
|
|
49 |
You can also specify a specific transport:
|
|
|
50 |
|
|
|
51 |
<info>php %command.full_name% to@example.com --transport=transport_name</info>
|
|
|
52 |
|
|
|
53 |
Note that this command bypasses the Messenger bus if configured.
|
|
|
54 |
|
|
|
55 |
EOF
|
|
|
56 |
);
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
|
60 |
{
|
|
|
61 |
$message = (new Email())
|
|
|
62 |
->to($input->getArgument('to'))
|
|
|
63 |
->from($input->getOption('from'))
|
|
|
64 |
->subject($input->getOption('subject'))
|
|
|
65 |
->text($input->getOption('body'))
|
|
|
66 |
;
|
|
|
67 |
if ($transport = $input->getOption('transport')) {
|
|
|
68 |
$message->getHeaders()->addTextHeader('X-Transport', $transport);
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
$this->transport->send($message);
|
|
|
72 |
|
|
|
73 |
return 0;
|
|
|
74 |
}
|
|
|
75 |
}
|