-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Add Mailer send and debug commands #43687
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
127 changes: 127 additions & 0 deletions
127
src/Symfony/Bundle/FrameworkBundle/Command/MailerSendCommand.php
This file contains hidden or 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,127 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Command; | ||
|
||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use Symfony\Component\Mailer\MailerInterface; | ||
use Symfony\Component\Mime\Email; | ||
|
||
/** | ||
* A console command to send an email via Symfony Mailer. | ||
* | ||
* @author Fritz Michael Gschwantner <fmg@inspiredminds.at> | ||
*/ | ||
final class MailerSendCommand extends Command | ||
{ | ||
protected static $defaultName = 'mailer:send'; | ||
protected static $defaultDescription = 'Sends an email message'; | ||
|
||
private $io; | ||
private $mailer; | ||
fabpot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public function __construct(MailerInterface $mailer) | ||
{ | ||
parent::__construct(); | ||
|
||
$this->mailer = $mailer; | ||
} | ||
|
||
protected function configure() | ||
{ | ||
$this | ||
->addOption('from', null, InputOption::VALUE_REQUIRED, 'The sender of the message') | ||
->addOption('to', null, InputOption::VALUE_REQUIRED, 'The recipient of the message') | ||
->addOption('subject', null, InputOption::VALUE_REQUIRED, 'The subject of the message') | ||
->addOption('body', null, InputOption::VALUE_REQUIRED, 'The body of the message') | ||
->addOption('transport', null, InputOption::VALUE_OPTIONAL, 'The transport to be used') | ||
->addOption('content-type', null, InputOption::VALUE_REQUIRED, 'The body content type of the message', 'text/plain') | ||
->addOption('charset', null, InputOption::VALUE_REQUIRED, 'The body charset of the message', 'utf-8') | ||
->addOption('body-source', null, InputOption::VALUE_REQUIRED, 'The source of the body [stdin|file]', 'stdin') | ||
->setHelp( | ||
<<<EOF | ||
The <info>%command.name%</info> command creates and sends a simple email message. | ||
|
||
<info>php %command.full_name% --transport=custom_transport --content-type=text/html</info> | ||
|
||
You can get the body of a message from a file: | ||
<info>php %command.full_name% --body-source=file --body=/path/to/file</info> | ||
|
||
EOF | ||
); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
switch ($input->getOption('body-source')) { | ||
case 'file': | ||
$filename = $input->getOption('body'); | ||
$content = file_get_contents($filename); | ||
if (false === $content) { | ||
throw new \Exception(sprintf('Could not get contents from "%s".', $filename)); | ||
} | ||
$input->setOption('body', $content); | ||
break; | ||
case 'stdin': | ||
break; | ||
default: | ||
throw new \InvalidArgumentException('body-source option should be "stdin" or "file".'); | ||
} | ||
|
||
$this->mailer->send($this->createMessage($input)); | ||
$this->io->success('Email was successfully sent.'); | ||
|
||
return Command::SUCCESS; | ||
fabpot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
protected function initialize(InputInterface $input, OutputInterface $output) | ||
{ | ||
$this->io = new SymfonyStyle($input, $output); | ||
$this->io->title('Symfony Mailer\'s Interactive Email Sender'); | ||
} | ||
|
||
protected function interact(InputInterface $input, OutputInterface $output) | ||
{ | ||
foreach ($input->getOptions() as $option => $value) { | ||
if (null === $value && 'transport' !== $option) { | ||
$input->setOption($option, $this->io->ask(sprintf('%s', ucfirst($option)))); | ||
} | ||
} | ||
} | ||
|
||
private function createMessage(InputInterface $input): Email | ||
{ | ||
$contentType = $input->getOption('content-type'); | ||
|
||
if (!\in_array($contentType, ['text/plain', 'text/html'], true)) { | ||
throw new \InvalidArgumentException(sprintf('Invalid content-type "%s", only "text/plain" and "text/html" allowed.', $contentType)); | ||
} | ||
|
||
$type = 'text/html' === $contentType ? 'html' : 'text'; | ||
|
||
$message = (new Email()) | ||
->subject($input->getOption('subject')) | ||
->from($input->getOption('from')) | ||
->to($input->getOption('to')) | ||
->{$type}($input->getOption('body'), $input->getOption('charset')) | ||
; | ||
|
||
if ($transport = $input->getOption('transport')) { | ||
$message->getHeaders()->addTextHeader('X-Transport', $transport); | ||
} | ||
|
||
return $message; | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
81 changes: 81 additions & 0 deletions
81
src/Symfony/Bundle/FrameworkBundle/Tests/Command/MailerSendCommandTest.php
This file contains hidden or 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 <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Tests\Command; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Bundle\FrameworkBundle\Command\MailerSendCommand; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
use Symfony\Component\Mailer\MailerInterface; | ||
use Symfony\Component\Mime\Email; | ||
|
||
/** | ||
* @author Fritz Michael Gschwantner <fmg@inspiredminds.at> | ||
*/ | ||
class MailerSendCommandTest extends TestCase | ||
{ | ||
public function testSendsEmail() | ||
{ | ||
$from = 'from@example.com'; | ||
$to = 'to@example.com'; | ||
$subject = 'Foobar'; | ||
$body = 'Lorem ipsum dolor sit amet.'; | ||
|
||
$mailer = $this->createMock(MailerInterface::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 | ||
; | ||
})) | ||
; | ||
|
||
$command = new MailerSendCommand($mailer); | ||
|
||
$tester = new CommandTester($command); | ||
$tester->execute([ | ||
'--from' => $from, | ||
'--to' => $to, | ||
'--subject' => $subject, | ||
'--body' => $body, | ||
]); | ||
} | ||
|
||
public function testUsesCustomTransport() | ||
{ | ||
$transport = 'foobar'; | ||
|
||
$mailer = $this->createMock(MailerInterface::class); | ||
$mailer | ||
->expects($this->once()) | ||
->method('send') | ||
->with(self::callback(static function (Email $message) use ($transport): bool { | ||
return $message->getHeaders()->getHeaderBody('X-Transport') === $transport; | ||
})) | ||
; | ||
|
||
$command = new MailerSendCommand($mailer); | ||
|
||
$tester = new CommandTester($command); | ||
$tester->execute([ | ||
'--from' => 'from@example.com', | ||
'--to' => 'to@example.com', | ||
'--subject' => 'Foobar', | ||
'--body' => 'Lorem ipsum dolor sit amet.', | ||
'--transport' => $transport, | ||
]); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.