Initial commit

This commit is contained in:
2020-10-07 10:37:15 +02:00
commit ce5f440392
28157 changed files with 4429172 additions and 0 deletions

View File

@@ -0,0 +1,135 @@
<?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\SwiftmailerBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
/**
* A console command for retrieving information about mailers.
*
* @author Jérémy Romey <jeremy@free-agent.fr>
*/
class DebugCommand extends ContainerAwareCommand
{
/** @var SymfonyStyle */
private $io;
/**
* @see Command
*/
protected function configure()
{
$this
->setName('debug:swiftmailer')
->setAliases(array(
'swiftmailer:debug',
))
->setDefinition(array(
new InputArgument('name', InputArgument::OPTIONAL, 'A mailer name'),
))
->setDescription('Displays current mailers for an application')
->setHelp(<<<EOF
The <info>%command.name%</info> displays the configured mailers:
<info>php %command.full_name% mailer-name</info>
EOF
)
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->io = new SymfonyStyle($input, $output);
$name = $input->getArgument('name');
if ($name) {
$this->outputMailer($name);
} else {
$this->outputMailers();
}
}
protected function outputMailers($routes = null)
{
$this->io->title('Configured SwiftMailer Mailers');
$tableHeaders = array('Name', 'Transport', 'Spool', 'Delivery', 'Single Address');
$tableRows = array();
$mailers = $this->getContainer()->getParameter('swiftmailer.mailers');
foreach ($mailers as $name => $mailer) {
$mailer = $this->getContainer()->get($mailer);
$transport = $this->getContainer()->getParameter(sprintf('swiftmailer.mailer.%s.transport.name', $name));
$spool = $this->getContainer()->getParameter(sprintf('swiftmailer.mailer.%s.spool.enabled', $name)) ? 'YES' : 'NO';
$delivery = $this->getContainer()->getParameter(sprintf('swiftmailer.mailer.%s.delivery.enabled', $name)) ? 'YES' : 'NO';
$singleAddress = $this->getContainer()->getParameter(sprintf('swiftmailer.mailer.%s.single_address', $name));
if ($this->isDefaultMailer($name)) {
$name = sprintf('%s (default mailer)', $name);
}
$tableRows[] = array($name, $transport, $spool, $delivery, $singleAddress);
}
$this->io->table($tableHeaders, $tableRows);
}
/**
* @throws \InvalidArgumentException When route does not exist
*/
protected function outputMailer($name)
{
try {
$service = sprintf('swiftmailer.mailer.%s', $name);
$mailer = $this->getContainer()->get($service);
} catch (ServiceNotFoundException $e) {
throw new \InvalidArgumentException(sprintf('The mailer "%s" does not exist.', $name));
}
$tableHeaders = array('Property', 'Value');
$tableRows = array();
$transport = $mailer->getTransport();
$spool = $this->getContainer()->getParameter(sprintf('swiftmailer.mailer.%s.spool.enabled', $name)) ? 'YES' : 'NO';
$delivery = $this->getContainer()->getParameter(sprintf('swiftmailer.mailer.%s.delivery.enabled', $name)) ? 'YES' : 'NO';
$singleAddress = $this->getContainer()->getParameter(sprintf('swiftmailer.mailer.%s.single_address', $name));
$this->io->title(sprintf('Configuration of the Mailer "%s"', $name));
if ($this->isDefaultMailer($name)) {
$this->io->comment('This is the default mailer');
}
$tableRows[] = array('Name', $name);
$tableRows[] = array('Service', $service);
$tableRows[] = array('Class', get_class($mailer));
$tableRows[] = array('Transport', sprintf('%s (%s)', sprintf('swiftmailer.mailer.%s.transport.name', $name), get_class($transport)));
$tableRows[] = array('Spool', $spool);
if ($this->getContainer()->hasParameter(sprintf('swiftmailer.spool.%s.file.path', $name))) {
$tableRows[] = array('Spool file', $this->getContainer()->getParameter(sprintf('swiftmailer.spool.%s.file.path', $name)));
}
$tableRows[] = array('Delivery', $delivery);
$tableRows[] = array('Single Address', $singleAddress);
$this->io->table($tableHeaders, $tableRows);
}
private function isDefaultMailer($name)
{
return $this->getContainer()->getParameter('swiftmailer.default_mailer') === $name || 'default' === $name;
}
}

View File

@@ -0,0 +1,139 @@
<?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\SwiftmailerBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
/**
* A console command for creating and sending simple emails.
*
* @author Gusakov Nikita <dev@nkt.me>
*/
class NewEmailCommand extends ContainerAwareCommand
{
/** @var SymfonyStyle */
private $io;
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('swiftmailer:email:send')
->setDescription('Send simple email message')
->addOption('from', null, InputOption::VALUE_REQUIRED, 'The from address of the message')
->addOption('to', null, InputOption::VALUE_REQUIRED, 'The to address 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('mailer', null, InputOption::VALUE_REQUIRED, 'The mailer name', 'default')
->addOption('content-type', null, InputOption::VALUE_REQUIRED, 'The body content type of the message', 'text/html')
->addOption('charset', null, InputOption::VALUE_REQUIRED, 'The body charset of the message', 'UTF8')
->addOption('body-source', null, InputOption::VALUE_REQUIRED, 'The source where body come from [stdin|file]', 'stdin')
->setHelp(<<<EOF
The <info>%command.name%</info> command creates and sends a simple email message.
<info>php %command.full_name% --mailer=custom_mailer --content-type=text/xml</info>
You can get body of message from a file:
<info>php %command.full_name% --body-source=file --body=/path/to/file</info>
EOF
);
}
/**
* {@inheritdoc}
*/
protected function initialize(InputInterface $input, OutputInterface $output)
{
$this->io = new SymfonyStyle($input, $output);
$this->io->title('SwiftMailer\'s Interactive Email Sender');
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$mailerServiceName = sprintf('swiftmailer.mailer.%s', $input->getOption('mailer'));
if (!$this->getContainer()->has($mailerServiceName)) {
throw new \InvalidArgumentException(sprintf('The mailer "%s" does not exist.', $input->getOption('mailer')));
}
switch ($input->getOption('body-source')) {
case 'file':
$filename = $input->getOption('body');
$content = file_get_contents($filename);
if ($content === false) {
throw new \Exception(sprintf('Could not get contents from "%s".', $filename));
}
$input->setOption('body', $content);
break;
case 'stdin':
break;
default:
throw new \InvalidArgumentException('Body-input option should be "stdin" or "file".');
}
$message = $this->createMessage($input);
$mailer = $this->getContainer()->get($mailerServiceName);
$sentMessages = $mailer->send($message);
$this->io->success(sprintf('%s emails were successfully sent.', $sentMessages));
}
/**
* {@inheritdoc}
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
foreach ($input->getOptions() as $option => $value) {
if ($value === null) {
$input->setOption($option, $this->io->ask(sprintf('%s', ucfirst($option))));
}
}
}
/**
* {@inheritdoc}
*/
public function isEnabled()
{
return $this->getContainer()->has('mailer');
}
/**
* Creates new message from input options.
*
* @param InputInterface $input An InputInterface instance
*
* @return \Swift_Message New message
*/
private function createMessage(InputInterface $input)
{
$message = new \Swift_Message(
$input->getOption('subject'),
$input->getOption('body'),
$input->getOption('content-type'),
$input->getOption('charset')
);
$message->setFrom($input->getOption('from'));
$message->setTo($input->getOption('to'));
return $message;
}
}

View File

@@ -0,0 +1,113 @@
<?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\SwiftmailerBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
/**
* Send Emails from the spool.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Clément JOBEILI <clement.jobeili@gmail.com>
* @author Toni Uebernickel <tuebernickel@gmail.com>
*/
class SendEmailCommand extends ContainerAwareCommand
{
/** @var SymfonyStyle */
private $io;
protected function configure()
{
$this
->setName('swiftmailer:spool:send')
->setDescription('Sends emails from the spool')
->addOption('message-limit', null, InputOption::VALUE_REQUIRED, 'The maximum number of messages to send.')
->addOption('time-limit', null, InputOption::VALUE_REQUIRED, 'The time limit for sending messages (in seconds).')
->addOption('recover-timeout', null, InputOption::VALUE_REQUIRED, 'The timeout for recovering messages that have taken too long to send (in seconds).')
->addOption('mailer', null, InputOption::VALUE_REQUIRED, 'The mailer name.')
->addOption('transport', null, InputOption::VALUE_REQUIRED, 'The service of the transport to use to send the messages.')
->setHelp(<<<EOF
The <info>%command.name%</info> command sends all emails from the spool.
<info>php %command.full_name% --message-limit=10 --time-limit=10 --recover-timeout=900 --mailer=default</info>
EOF
)
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->io = new SymfonyStyle($input, $output);
$name = $input->getOption('mailer');
if ($name) {
$this->processMailer($name, $input, $output);
} else {
$mailers = array_keys($this->getContainer()->getParameter('swiftmailer.mailers'));
foreach ($mailers as $name) {
$this->processMailer($name, $input, $output);
}
}
}
private function processMailer($name, InputInterface $input, OutputInterface $output)
{
if (!$this->getContainer()->has(sprintf('swiftmailer.mailer.%s', $name))) {
throw new \InvalidArgumentException(sprintf('The mailer "%s" does not exist.', $name));
}
$this->io->text(sprintf('<info>[%s]</info> Processing <info>%s</info> mailer spool... ', date('Y-m-d H:i:s'), $name));
if ($this->getContainer()->getParameter(sprintf('swiftmailer.mailer.%s.spool.enabled', $name))) {
$mailer = $this->getContainer()->get(sprintf('swiftmailer.mailer.%s', $name));
$transport = $mailer->getTransport();
if ($transport instanceof \Swift_Transport_LoadBalancedTransport) {
foreach ($transport->getTransports() as $eachTransport) {
$this->recoverSpool($name, $eachTransport, $input, $output);
}
} else {
$this->recoverSpool($name, $transport, $input, $output);
}
} else {
$this->io->warning('There are no emails to send because the spool is disabled.');
}
}
private function recoverSpool($name, \Swift_Transport $transport, InputInterface $input, OutputInterface $output)
{
if ($transport instanceof \Swift_Transport_SpoolTransport) {
$spool = $transport->getSpool();
if ($spool instanceof \Swift_ConfigurableSpool) {
$spool->setMessageLimit($input->getOption('message-limit'));
$spool->setTimeLimit($input->getOption('time-limit'));
}
if ($spool instanceof \Swift_FileSpool) {
if (null !== $input->getOption('recover-timeout')) {
$spool->recover($input->getOption('recover-timeout'));
} else {
$spool->recover();
}
}
$transportService = $input->getOption('transport') ?: sprintf('swiftmailer.mailer.%s.transport.real', $name);
$sent = $spool->flushQueue($this->getContainer()->get($transportService));
$this->io->text(sprintf('<comment>%d</comment> emails sent', $sent));
}
}
}