Skip to content
This repository was archived by the owner on Feb 6, 2022. It is now read-only.

Commit cf5de43

Browse files
Added mailers
1 parent 5573e11 commit cf5de43

31 files changed

+1087
-300
lines changed

Command/DebugCommand.php

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Bundle\SwiftmailerBundle\Command;
13+
14+
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
15+
use Symfony\Component\Console\Input\InputArgument;
16+
use Symfony\Component\Console\Input\InputInterface;
17+
use Symfony\Component\Console\Output\OutputInterface;
18+
use Symfony\Component\Routing\RouterInterface;
19+
use Symfony\Component\DependencyInjection\Alias;
20+
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
21+
22+
/**
23+
* A console command for retrieving information about mailers
24+
*
25+
* @author Jérémy Romey <[email protected]>
26+
*/
27+
class DebugCommand extends ContainerAwareCommand
28+
{
29+
/**
30+
* @see Command
31+
*/
32+
protected function configure()
33+
{
34+
$this
35+
->setName('swiftmailer:debug')
36+
->setDefinition(array(
37+
new InputArgument('name', InputArgument::OPTIONAL, 'A mailer name'),
38+
))
39+
->setDescription('Displays current mailers for an application')
40+
->setHelp(<<<EOF
41+
The <info>%command.name%</info> displays the configured mailers:
42+
43+
<info>php %command.full_name% mailer-name</info>
44+
EOF
45+
)
46+
;
47+
}
48+
49+
/**
50+
* @see Command
51+
*/
52+
protected function execute(InputInterface $input, OutputInterface $output)
53+
{
54+
$name = $input->getArgument('name');
55+
56+
if ($name) {
57+
$this->outputMailer($output, $name);
58+
} else {
59+
$this->outputMailers($output);
60+
}
61+
}
62+
63+
protected function outputMailers(OutputInterface $output, $routes = null)
64+
{
65+
$output->writeln($this->getHelper('formatter')->formatSection('swiftmailer', 'Current mailers'));
66+
67+
$maxName = strlen('name');
68+
$maxTransport = strlen('transport');
69+
$maxSpool = strlen('spool');
70+
$maxDelivery = strlen('delivery');
71+
$maxSingleAddress = strlen('single address');
72+
73+
$mailers = $this->getContainer()->getParameter('swiftmailer.mailers');
74+
foreach ($mailers as $name => $mailer) {
75+
$mailer = $this->getContainer()->get($mailer);
76+
$transport = $this->getContainer()->getParameter(sprintf('swiftmailer.mailer.%s.transport.name', $name));
77+
$spool = $this->getContainer()->getParameter(sprintf('swiftmailer.mailer.%s.spool.enabled', $name)) ? 'YES' : 'NO';
78+
$delivery = $this->getContainer()->getParameter(sprintf('swiftmailer.mailer.%s.delivery.enabled', $name)) ? 'YES' : 'NO';
79+
$singleAddress = $this->getContainer()->getParameter(sprintf('swiftmailer.mailer.%s.single_address', $name));
80+
81+
if ($this->isDefaultMailer($name)) {
82+
$name = sprintf('%s (default mailer)', $name);
83+
}
84+
$maxName = max($maxName, strlen($name));
85+
$maxTransport = max($maxTransport, strlen($transport));
86+
$maxSpool = max($maxSpool, strlen($spool));
87+
$maxDelivery = max($maxDelivery, strlen($delivery));
88+
$maxSingleAddress = max($maxSingleAddress, strlen($singleAddress));
89+
}
90+
$format = '%-'.$maxName.'s %-'.$maxTransport.'s %-'.$maxSpool.'s %-'.$maxDelivery.'s %-'.$maxSingleAddress.'s';
91+
92+
$format1 = '%-'.($maxName + 19).'s %-'.($maxTransport + 19).'s %-'.($maxSpool + 19).'s %-'.($maxDelivery + 19).'s %-'.($maxSingleAddress + 19).'s';
93+
$output->writeln(sprintf($format1, '<comment>Name</comment>', '<comment>Transport</comment>', '<comment>Spool</comment>', '<comment>Delivery</comment>', '<comment>Single Address</comment>'));
94+
foreach ($mailers as $name => $mailer) {
95+
$mailer = $this->getContainer()->get($mailer);
96+
$transport = $this->getContainer()->getParameter(sprintf('swiftmailer.mailer.%s.transport.name', $name));
97+
$spool = $this->getContainer()->getParameter(sprintf('swiftmailer.mailer.%s.spool.enabled', $name)) ? 'YES' : 'NO';
98+
$delivery = $this->getContainer()->getParameter(sprintf('swiftmailer.mailer.%s.delivery.enabled', $name)) ? 'YES' : 'NO';
99+
$singleAddress = $this->getContainer()->getParameter(sprintf('swiftmailer.mailer.%s.single_address', $name));
100+
if ($this->isDefaultMailer($name)) {
101+
$name = sprintf('%s (default mailer)', $name);
102+
}
103+
$output->writeln(sprintf($format, $name, $transport, $spool, $delivery, $singleAddress));
104+
}
105+
}
106+
107+
/**
108+
* @throws \InvalidArgumentException When route does not exist
109+
*/
110+
protected function outputMailer(OutputInterface $output, $name)
111+
{
112+
try {
113+
$service = sprintf('swiftmailer.mailer.%s', $name);
114+
$mailer = $this->getContainer()->get($service);
115+
} catch (ServiceNotFoundException $e) {
116+
throw new \InvalidArgumentException(sprintf('The mailer "%s" does not exist.', $name));
117+
}
118+
119+
$transport = $mailer->getTransport();
120+
$spool = $this->getContainer()->getParameter(sprintf('swiftmailer.mailer.%s.spool.enabled', $name)) ? 'YES' : 'NO';
121+
$delivery = $this->getContainer()->getParameter(sprintf('swiftmailer.mailer.%s.delivery.enabled', $name)) ? 'YES' : 'NO';
122+
$singleAddress = $this->getContainer()->getParameter(sprintf('swiftmailer.mailer.%s.single_address', $name));
123+
124+
$output->writeln($this->getHelper('formatter')->formatSection('swiftmailer', sprintf('Mailer "%s"', $name)));
125+
if ($this->isDefaultMailer($name)) {
126+
$output->writeln('This is the default mailer');
127+
}
128+
129+
$output->writeln(sprintf('<comment>Name</comment> %s', $name));
130+
$output->writeln(sprintf('<comment>Service</comment> %s', $service));
131+
$output->writeln(sprintf('<comment>Class</comment> %s', get_class($mailer)));
132+
$output->writeln(sprintf('<comment>Transport</comment> %s (%s)', sprintf('swiftmailer.mailer.%s.transport.name', $name), get_class($transport)));
133+
$output->writeln(sprintf('<comment>Spool</comment> %s', $spool));
134+
if ($this->getContainer()->hasParameter(sprintf('swiftmailer.spool.%s.file.path', $name))) {
135+
$output->writeln(sprintf('<comment>Spool file</comment> %s', $this->getContainer()->getParameter(sprintf('swiftmailer.spool.%s.file.path', $name))));
136+
}
137+
$output->writeln(sprintf('<comment>Delivery</comment> %s', $delivery));
138+
$output->writeln(sprintf('<comment>Single Address</comment> %s', $singleAddress));
139+
}
140+
141+
private function isDefaultMailer($name)
142+
{
143+
return ($this->getContainer()->getParameter('swiftmailer.default_mailer') == $name || 'default' == $name) ? true : false;
144+
}
145+
}

Command/SendEmailCommand.php

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ protected function configure()
3535
->addOption('message-limit', 0, InputOption::VALUE_OPTIONAL, 'The maximum number of messages to send.')
3636
->addOption('time-limit', 0, InputOption::VALUE_OPTIONAL, 'The time limit for sending messages (in seconds).')
3737
->addOption('recover-timeout', 0, InputOption::VALUE_OPTIONAL, 'The timeout for recovering messages that have taken too long to send (in seconds).')
38+
->addOption('mailer', null, InputOption::VALUE_OPTIONAL, 'The mailer name.')
3839
->setHelp(<<<EOF
3940
The <info>swiftmailer:spool:send</info> command sends all emails from the spool.
4041
41-
<info>php app/console swiftmailer:spool:send --message-limit=10 --time-limit=10 --recover-timeout=900</info>
42+
<info>php app/console swiftmailer:spool:send --message-limit=10 --time-limit=10 --recover-timeout=900 --mailer=default</info>
4243
4344
EOF
4445
)
@@ -50,25 +51,46 @@ protected function configure()
5051
*/
5152
protected function execute(InputInterface $input, OutputInterface $output)
5253
{
53-
$mailer = $this->getContainer()->get('mailer');
54-
$transport = $mailer->getTransport();
55-
56-
if ($transport instanceof \Swift_Transport_SpoolTransport) {
57-
$spool = $transport->getSpool();
58-
if ($spool instanceof \Swift_ConfigurableSpool) {
59-
$spool->setMessageLimit($input->getOption('message-limit'));
60-
$spool->setTimeLimit($input->getOption('time-limit'));
54+
$name = $input->getOption('mailer');
55+
if ($name) {
56+
$this->processMailer($name, $input, $output);
57+
} else {
58+
$mailers = array_keys($this->getContainer()->getParameter('swiftmailer.mailers'));
59+
foreach ($mailers as $name) {
60+
$this->processMailer($name, $input, $output);
6161
}
62-
if ($spool instanceof \Swift_FileSpool) {
63-
if (null !== $input->getOption('recover-timeout')) {
64-
$spool->recover($input->getOption('recover-timeout'));
65-
} else {
66-
$spool->recover();
62+
}
63+
}
64+
65+
private function processMailer($name, $input, $output)
66+
{
67+
if (!$this->getContainer()->has(sprintf('swiftmailer.mailer.%s', $name))) {
68+
throw new \InvalidArgumentException(sprintf('The mailer "%s" does not exist.', $name));
69+
}
70+
71+
$output->write(sprintf('Processing <info>%s</info> mailer... ', $name));
72+
if ($this->getContainer()->getParameter(sprintf('swiftmailer.mailer.%s.spool.enabled', $name))) {
73+
$mailer = $this->getContainer()->get(sprintf('swiftmailer.mailer.%s', $name));
74+
$transport = $mailer->getTransport();
75+
if ($transport instanceof \Swift_Transport_SpoolTransport) {
76+
$spool = $transport->getSpool();
77+
if ($spool instanceof \Swift_ConfigurableSpool) {
78+
$spool->setMessageLimit($input->getOption('message-limit'));
79+
$spool->setTimeLimit($input->getOption('time-limit'));
6780
}
68-
}
69-
$sent = $spool->flushQueue($this->getContainer()->get('swiftmailer.transport.real'));
81+
if ($spool instanceof \Swift_FileSpool) {
82+
if (null !== $input->getOption('recover-timeout')) {
83+
$spool->recover($input->getOption('recover-timeout'));
84+
} else {
85+
$spool->recover();
86+
}
87+
}
88+
$sent = $spool->flushQueue($this->getContainer()->get(sprintf('swiftmailer.mailer.%s.transport.real', $name)));
7089

71-
$output->writeln(sprintf('sent %s emails', $sent));
90+
$output->writeln(sprintf('<comment>%d</comment> emails sent', $sent));
91+
}
92+
} else {
93+
$output->writeln('No email to send as the spool is disabled.');
7294
}
7395
}
7496
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Bundle\SwiftmailerBundle\DataCollector;
13+
14+
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
15+
use Symfony\Component\HttpFoundation\Request;
16+
use Symfony\Component\HttpFoundation\Response;
17+
use Symfony\Component\DependencyInjection\ContainerInterface;
18+
19+
/**
20+
* MessageDataCollector.
21+
*
22+
* @author Fabien Potencier <[email protected]>
23+
* @author Clément JOBEILI <[email protected]>
24+
* @author Jérémy Romey <[email protected]>
25+
*/
26+
class MessageDataCollector extends DataCollector
27+
{
28+
private $container;
29+
30+
/**
31+
* Constructor.
32+
*
33+
* We don't inject the message logger and mailer here
34+
* to avoid the creation of these objects when no emails are sent.
35+
*
36+
* @param ContainerInterface $container A ContainerInterface instance
37+
*/
38+
public function __construct(ContainerInterface $container)
39+
{
40+
$this->container = $container;
41+
}
42+
43+
/**
44+
* {@inheritdoc}
45+
*/
46+
public function collect(Request $request, Response $response, \Exception $exception = null)
47+
{
48+
$this->data = array(
49+
'mailer' => array(),
50+
'messageCount' => 0,
51+
'defaultMailer' => '',
52+
);
53+
// only collect when Swiftmailer has already been initialized
54+
if (class_exists('Swift_Mailer', false)) {
55+
$mailers = $this->container->getParameter('swiftmailer.mailers');
56+
foreach ($mailers as $name => $mailer) {
57+
if ($this->container->getParameter('swiftmailer.default_mailer') == $name) {
58+
$this->data['defaultMailer'] = $name;
59+
}
60+
$loggerName = sprintf('swiftmailer.mailer.%s.plugin.messagelogger', $name);
61+
if ($this->container->has($loggerName)) {
62+
$logger = $this->container->get($loggerName);
63+
$this->data['mailer'][$name] = array(
64+
'messages' => $logger->getMessages(),
65+
'messageCount' => $logger->countMessages(),
66+
'isSpool' => $this->container->getParameter(sprintf('swiftmailer.mailer.%s.spool.enabled', $name)),
67+
);
68+
$this->data['messageCount'] += $logger->countMessages();
69+
}
70+
}
71+
}
72+
}
73+
74+
/**
75+
* Returns the mailer names.
76+
*
77+
* @return array The mailer names.
78+
*/
79+
public function getMailers()
80+
{
81+
return array_keys($this->data['mailer']);
82+
}
83+
84+
/**
85+
* Returns the data collected of a mailer.
86+
*
87+
* @return array The data of the mailer.
88+
*/
89+
90+
public function getMailerData($name)
91+
{
92+
if (!isset($this->data['mailer'][$name])) {
93+
throw new \LogicException(sprintf("Missing %s data in %s", $name, get_class()));
94+
}
95+
96+
return $this->data['mailer'][$name];
97+
}
98+
99+
/**
100+
* Returns the message count of a mailer or the total.
101+
*
102+
* @return int The number of messages.
103+
*/
104+
public function getMessageCount($name = null)
105+
{
106+
if (is_null($name)) {
107+
return $this->data['messageCount'];
108+
} elseif ($data = $this->getMailerData($name)) {
109+
return $data['messageCount'];
110+
}
111+
112+
return null;
113+
}
114+
115+
/**
116+
* Returns the message of a mailer.
117+
*
118+
* @return array The messages.
119+
*/
120+
public function getMessages($name)
121+
{
122+
if ($data = $this->getMailerData($name)) {
123+
return $data['messages'];
124+
}
125+
126+
return array();
127+
}
128+
129+
/**
130+
* Returns if the mailer has spool.
131+
*
132+
* @return boolean
133+
*/
134+
public function isSpool($name)
135+
{
136+
if ($data = $this->getMailerData($name)) {
137+
return $data['isSpool'];
138+
}
139+
140+
return null;
141+
}
142+
143+
/**
144+
* Returns if the mailer is the default mailer.
145+
*
146+
* @return boolean
147+
*/
148+
public function isDefaultMailer($name)
149+
{
150+
return $this->data['defaultMailer'] == $name;
151+
}
152+
153+
/**
154+
* {@inheritdoc}
155+
*/
156+
public function getName()
157+
{
158+
return 'swiftmailer';
159+
}
160+
}

0 commit comments

Comments
 (0)