/
ProcessEmailQueueCommand.php
182 lines (156 loc) · 7.39 KB
/
ProcessEmailQueueCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
/*
* @copyright 2014 Mautic Contributors. All rights reserved
* @author Mautic
*
* @link http://mautic.org
*
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/
namespace Mautic\EmailBundle\Command;
use Mautic\CoreBundle\Command\ModeratedCommand;
use Mautic\EmailBundle\EmailEvents;
use Mautic\EmailBundle\Event\QueueEmailEvent;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;
/**
* CLI command to process the e-mail queue.
*/
class ProcessEmailQueueCommand extends ModeratedCommand
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('mautic:emails:send')
->setDescription('Processes SwiftMail\'s mail queue')
->addOption('--message-limit', null, InputOption::VALUE_OPTIONAL, 'Limit number of messages sent at a time. Defaults to value set in config.')
->addOption('--time-limit', null, InputOption::VALUE_OPTIONAL, 'Limit the number of seconds per batch. Defaults to value set in config.')
->addOption('--do-not-clear', null, InputOption::VALUE_NONE, 'By default, failed messages older than the --recover-timeout setting will be attempted one more time then deleted if it fails again. If this is set, sending of failed messages will continue to be attempted.')
->addOption('--recover-timeout', null, InputOption::VALUE_OPTIONAL, 'Sets the amount of time in seconds before attempting to resend failed messages. Defaults to value set in config.')
->addOption('--clear-timeout', null, InputOption::VALUE_OPTIONAL, 'Sets the amount of time in seconds before deleting failed messages. Defaults to value set in config.')
->setHelp(<<<'EOT'
The <info>%command.name%</info> command is used to process the application's e-mail queue
<info>php %command.full_name%</info>
EOT
);
parent::configure();
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$options = $input->getOptions();
$env = (!empty($options['env'])) ? $options['env'] : 'dev';
$container = $this->getContainer();
$dispatcher = $container->get('event_dispatcher');
$skipClear = $input->getOption('do-not-clear');
$quiet = $input->hasOption('quiet') ? $input->getOption('quiet') : false;
$timeout = $input->getOption('clear-timeout');
$queueMode = $container->get('mautic.helper.core_parameters')->getParameter('mailer_spool_type');
if ($queueMode != 'file') {
$output->writeln('Mautic is not set to queue email.');
return 0;
}
if (!$this->checkRunStatus($input, $output)) {
return 0;
}
if (empty($timeout)) {
$timeout = $container->getParameter('mautic.mailer_spool_clear_timeout');
}
if (!$skipClear) {
//Swift mailer's send command does not handle failed messages well rather it will retry sending forever
//so let's first handle emails stuck in the queue and remove them if necessary
$transport = $this->getContainer()->get('swiftmailer.transport.real');
if (!$transport->isStarted()) {
$transport->start();
}
$spoolPath = $container->getParameter('mautic.mailer_spool_path');
if (file_exists($spoolPath)) {
$finder = Finder::create()->in($spoolPath)->name('*.{finalretry,sending,tryagain}');
foreach ($finder as $failedFile) {
$file = $failedFile->getRealPath();
$lockedtime = filectime($file);
if (!(time() - $lockedtime) > $timeout) {
//the file is not old enough to be resent yet
continue;
}
//rename the file so no other process tries to find it
$tmpFilename = str_replace(['.finalretry', '.sending', '.tryagain'], '', $failedFile);
$tmpFilename .= '.finalretry';
rename($failedFile, $tmpFilename);
$message = unserialize(file_get_contents($tmpFilename));
if ($message !== false && is_object($message) && get_class($message) === 'Swift_Message') {
$tryAgain = false;
if ($dispatcher->hasListeners(EmailEvents::EMAIL_RESEND)) {
$event = new QueueEmailEvent($message);
$dispatcher->dispatch(EmailEvents::EMAIL_RESEND, $event);
$tryAgain = $event->shouldTryAgain();
}
try {
$transport->send($message);
} catch (\Swift_TransportException $e) {
if ($dispatcher->hasListeners(EmailEvents::EMAIL_FAILED)) {
$event = new QueueEmailEvent($message);
$dispatcher->dispatch(EmailEvents::EMAIL_FAILED, $event);
}
}
} else {
// $message isn't a valid message file
$tryAgain = false;
}
if ($tryAgain) {
$retryFilename = str_replace('.finalretry', '.tryagain', $tmpFilename);
rename($tmpFilename, $retryFilename);
} else {
//delete the file, either because it sent or because it failed
unlink($tmpFilename);
}
}
}
}
//now process new emails
if (!$quiet) {
$output->setVerbosity(OutputInterface::VERBOSITY_QUIET);
}
$command = $this->getApplication()->find('swiftmailer:spool:send');
$commandArgs = [
'command' => 'swiftmailer:spool:send',
'--env' => $env,
];
if ($quiet) {
$commandArgs['--quiet'] = true;
}
//set spool message limit
if ($msgLimit = $input->getOption('message-limit')) {
$commandArgs['--message-limit'] = $msgLimit;
} elseif ($msgLimit = $container->getParameter('mautic.mailer_spool_msg_limit')) {
$commandArgs['--message-limit'] = $msgLimit;
}
//set time limit
if ($timeLimit = $input->getOption('time-limit')) {
$commandArgs['--time-limit'] = $timeLimit;
} elseif ($timeLimit = $container->getParameter('mautic.mailer_spool_time_limit')) {
$commandArgs['--time-limit'] = $timeLimit;
}
//set the recover timeout
if ($timeout = $input->getOption('recover-timeout')) {
$commandArgs['--recover-timeout'] = $timeout;
} elseif ($timeout = $container->getParameter('mautic.mailer_spool_recover_timeout')) {
$commandArgs['--recover-timeout'] = $timeout;
}
$input = new ArrayInput($commandArgs);
$returnCode = $command->run($input, $output);
$this->completeRun();
if ($returnCode !== 0) {
return $returnCode;
}
return 0;
}
}