2019-07-14 12:13:58 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
|
|
use App\EmailLog;
|
|
|
|
use App\Facade\UserConfig;
|
|
|
|
use App\Http\Middleware\GlobalConfiguration;
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
use Illuminate\Mail\Message;
|
|
|
|
|
|
|
|
class SendEmailsCommand extends Command
|
|
|
|
{
|
2019-07-14 16:12:56 +01:00
|
|
|
private $maxPerBatch;
|
|
|
|
private $numberOfAttempts;
|
|
|
|
private $secondsBetweenPolls;
|
2019-07-14 12:13:58 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The name and signature of the console command.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $signature = 'bt-queue:send-emails {--poll}';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $description = 'Sends e-mails queued in the database.';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new command instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
2019-07-14 16:12:56 +01:00
|
|
|
|
|
|
|
$this->maxPerBatch = config('mail.queue.emails_per_batch');
|
|
|
|
$this->numberOfAttempts = config('mail.queue.max_attempts');
|
|
|
|
$this->secondsBetweenPolls = config('mail.queue.seconds_between_polls');
|
2019-07-14 12:13:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
if (!UserConfig::get('queue_emails'))
|
|
|
|
{
|
|
|
|
$this->output->error('E-mail queueing is not enabled. E-mails are being sent immediately.');
|
|
|
|
}
|
|
|
|
|
2019-07-14 14:18:15 +01:00
|
|
|
$this->output->writeln('Setting mail configuration');
|
|
|
|
|
|
|
|
GlobalConfiguration::updateMailConfig();
|
|
|
|
|
2019-07-14 12:13:58 +01:00
|
|
|
$this->output->writeln('E-mail queue runner started');
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
$emailsToSend = EmailLog::where([
|
|
|
|
['sent_at', null],
|
2019-07-14 16:12:56 +01:00
|
|
|
['number_attempts', '<', $this->numberOfAttempts]
|
|
|
|
])->limit($this->maxPerBatch)->get();
|
2019-07-14 12:13:58 +01:00
|
|
|
|
|
|
|
$this->output->writeln(sprintf(
|
|
|
|
'%d e-mail%s to send',
|
|
|
|
$emailsToSend->count(),
|
|
|
|
$emailsToSend->count() == 1 ? '' : 's'
|
|
|
|
));
|
|
|
|
|
|
|
|
/** @var EmailLog $emailToSend */
|
|
|
|
foreach ($emailsToSend as $emailToSend)
|
|
|
|
{
|
|
|
|
$this->sendEmail($emailToSend);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->option('poll'))
|
|
|
|
{
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
2019-07-14 16:12:56 +01:00
|
|
|
sleep($this->secondsBetweenPolls);
|
2019-07-14 12:13:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function sendEmail(EmailLog $emailLog)
|
|
|
|
{
|
|
|
|
$this->output->writeln(sprintf('Sending message with subject \'%s\'', $emailLog->subject));
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
app('mailer')->send(
|
|
|
|
[],
|
|
|
|
[],
|
|
|
|
function (Message $message) use ($emailLog)
|
|
|
|
{
|
|
|
|
$message->setFrom($emailLog->sender_address, $emailLog->sender_name);
|
|
|
|
$message->setSubject($emailLog->subject);
|
|
|
|
|
|
|
|
$this->addAddresses($emailLog->to_addresses, $message, 'To');
|
|
|
|
$this->addAddresses($emailLog->cc_addresses, $message, 'Cc');
|
|
|
|
$this->addAddresses($emailLog->bcc_addresses, $message, 'Bcc');
|
|
|
|
|
|
|
|
$message->addPart($emailLog->body_plain, 'text/plain');
|
|
|
|
$message->setBody($emailLog->body_html, 'text/html');
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
$emailLog->sent_at = new \DateTime();
|
|
|
|
|
|
|
|
$this->output->writeln('Send completed');
|
|
|
|
}
|
|
|
|
catch (\Exception $ex)
|
|
|
|
{
|
|
|
|
$this->output->error(sprintf('Send failed: %s', $ex->getMessage()));
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
$emailLog->number_attempts++;
|
|
|
|
$emailLog->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function addAddresses($dbFieldData, Message $message, $property)
|
|
|
|
{
|
|
|
|
$decoded = json_decode($dbFieldData);
|
|
|
|
|
|
|
|
if (is_array($decoded))
|
|
|
|
{
|
|
|
|
foreach ($decoded as $addressInfo)
|
|
|
|
{
|
|
|
|
$this->output->writeln(sprintf('Adding %s address: \'"%s" <%s>\'', $property, $addressInfo->name, $addressInfo->address));
|
|
|
|
|
|
|
|
$message->{"set{$property}"}($addressInfo->address, $addressInfo->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|