2020-04-02 18:38:41 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of RaspiSMS.
|
|
|
|
*
|
|
|
|
* (c) Pierre-Lin Bonnemaison <plebwebsas@gmail.com>
|
|
|
|
*
|
|
|
|
* This source file is subject to the GPL-3.0 license that is bundled
|
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace daemons;
|
|
|
|
|
2024-10-31 21:22:56 +01:00
|
|
|
use controllers\internals\Queue;
|
2020-04-02 18:38:41 +02:00
|
|
|
use Monolog\Handler\StreamHandler;
|
|
|
|
use Monolog\Logger;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Phone daemon class.
|
|
|
|
*/
|
|
|
|
class Mailer extends AbstractDaemon
|
|
|
|
{
|
2024-10-31 21:22:56 +01:00
|
|
|
private ?Queue $mailer_queue;
|
2020-04-02 18:38:41 +02:00
|
|
|
private $last_message_at;
|
|
|
|
private $bdd;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param array $phone : A phone table entry
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$name = 'RaspiSMS Daemon Mailer';
|
|
|
|
$logger = new Logger($name);
|
|
|
|
$logger->pushHandler(new StreamHandler(PWD_LOGS . '/daemons.log', Logger::DEBUG));
|
|
|
|
$pid_dir = PWD_PID;
|
|
|
|
$no_parent = false; //Rattach to parent so parent can stop it
|
|
|
|
$additional_signals = [];
|
|
|
|
$uniq = true; //Sender should be uniq
|
|
|
|
|
|
|
|
//Construct the daemon
|
|
|
|
parent::__construct($name, $logger, $pid_dir, $no_parent, $additional_signals, $uniq);
|
|
|
|
|
|
|
|
parent::start();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$find_message = true;
|
|
|
|
while ($find_message)
|
|
|
|
{
|
2024-10-31 21:22:56 +01:00
|
|
|
$message = $this->mailer_queue->read(QUEUE_TYPE_EMAIL);
|
2020-04-02 18:38:41 +02:00
|
|
|
|
2024-10-31 21:22:56 +01:00
|
|
|
if ($message === null)
|
2020-04-02 18:38:41 +02:00
|
|
|
{
|
|
|
|
$find_message = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-10-31 21:22:56 +01:00
|
|
|
$message = json_decode($message, true);
|
|
|
|
|
2020-04-02 18:38:41 +02:00
|
|
|
$this->logger->info('Try sending email : ' . json_encode($message));
|
|
|
|
|
|
|
|
$mailer = new \controllers\internals\Mailer();
|
2021-04-03 18:19:08 +02:00
|
|
|
$success = $mailer->send($message['destinations'], $message['subject'], $message['body'], $message['alt_body'], $message['attachments']);
|
2020-04-02 18:38:41 +02:00
|
|
|
if (!$success)
|
|
|
|
{
|
|
|
|
$this->logger->error('Failed sending email');
|
2020-06-23 21:06:13 +02:00
|
|
|
|
2020-04-02 18:38:41 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->logger->info('Success sending email');
|
|
|
|
}
|
|
|
|
|
|
|
|
//Send mail every 5 seconds
|
|
|
|
usleep(5 * 1000000);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function on_start()
|
|
|
|
{
|
|
|
|
//Set last message at to construct time
|
2024-10-31 21:22:56 +01:00
|
|
|
$this->mailer_queue = new Queue(QUEUE_ID_EMAIL);
|
2020-04-02 18:38:41 +02:00
|
|
|
|
|
|
|
$this->logger->info('Starting Mailer daemon with pid ' . getmypid());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function on_stop()
|
|
|
|
{
|
|
|
|
//Delete queue on daemon close
|
2020-04-16 07:50:30 +02:00
|
|
|
$this->logger->info('Closing queue : ' . QUEUE_ID_EMAIL);
|
2020-04-02 18:38:41 +02:00
|
|
|
$this->logger->info('Stopping Mailer daemon with pid ' . getmypid());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function handle_other_signals($signal)
|
|
|
|
{
|
|
|
|
$this->logger->info('Signal not handled by ' . $this->name . ' Daemon : ' . $signal);
|
|
|
|
}
|
|
|
|
}
|