From 48ef0c063e07fe8b11256d6ce382d9444e30519c Mon Sep 17 00:00:00 2001 From: osaajani <> Date: Thu, 16 Apr 2020 07:50:30 +0200 Subject: [PATCH] Working smtp for email --- composer.json | 3 ++- controllers/internals/Console.php | 8 ++++++++ controllers/internals/Mailer.php | 20 ++++++++++---------- controllers/internals/Tool.php | 18 ------------------ controllers/publics/Connect.php | 5 +++-- controllers/publics/User.php | 3 ++- daemons/Launcher.php | 21 +++++++++++++++++++++ daemons/Mailer.php | 6 +++--- 8 files changed, 49 insertions(+), 35 deletions(-) diff --git a/composer.json b/composer.json index de6ff36..c174fd5 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,8 @@ "robmorgan/phinx": "^0.11.1", "monolog/monolog": "^2.0", "ovh/ovh": "^2.0", - "twilio/sdk": "^6.1" + "twilio/sdk": "^6.1", + "phpmailer/phpmailer": "^6.1" }, "require-dev": { } diff --git a/controllers/internals/Console.php b/controllers/internals/Console.php index 19e0bb3..7e1b05b 100644 --- a/controllers/internals/Console.php +++ b/controllers/internals/Console.php @@ -39,6 +39,14 @@ namespace controllers\internals; { new \daemons\Webhook(); } + + /** + * Start mailer daemon. + */ + public function mailer() + { + new \daemons\Mailer(); + } /** * Start a phone daemon. diff --git a/controllers/internals/Mailer.php b/controllers/internals/Mailer.php index 5c177d9..8b1c701 100755 --- a/controllers/internals/Mailer.php +++ b/controllers/internals/Mailer.php @@ -22,18 +22,18 @@ class Mailer extends \descartes\Controller $this->log->pushHandler(new StreamHandler(PWD_LOGS . '/mail.log', Logger::DEBUG)); $this->mail = new PHPMailer(true); - $mail->SMTPDebug = SMTP::DEBUG_OFF; - $mail->isSMTP(); - $mail->Host = MAIL['SMTP']['HOST']; - $mail->SMTPAuth = true; - $mail->Username = MAIL['SMTP']['USER']; - $mail->Password = MAIL['SMTP']['PASS']; - $mail->Port = MAIL['SMTP']['PORT']; - $mail->setFrom(MAIL['FROM']); + $this->mail->SMTPDebug = SMTP::DEBUG_OFF; + $this->mail->isSMTP(); + $this->mail->Host = MAIL['SMTP']['HOST']; + $this->mail->SMTPAuth = true; + $this->mail->Username = MAIL['SMTP']['USER']; + $this->mail->Password = MAIL['SMTP']['PASS']; + $this->mail->Port = MAIL['SMTP']['PORT']; + $this->mail->setFrom(MAIL['FROM']); if (MAIL['SMTP']['TLS']) { - $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; + $this->mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; } } @@ -127,7 +127,7 @@ class Mailer extends \descartes\Controller * @param array $datas : Datas to inject into email template * @return bool : true on success, false on error */ - public function enqueue (string $destination, string $settings, string $datas) : bool + public function enqueue (string $destination, array $settings, array $datas) : bool { $response = $this->generate_body($settings, $datas); diff --git a/controllers/internals/Tool.php b/controllers/internals/Tool.php index bcfa61d..bcc2b13 100644 --- a/controllers/internals/Tool.php +++ b/controllers/internals/Tool.php @@ -189,24 +189,6 @@ namespace controllers\internals; return (bool) ($_SESSION['user']['admin'] ?? false); } - /** - * Cette fonction s'occupe d'envoyer les emails. - * - * @param string $to : L'adresse mail à laquelle envoyer le mail - * @param array $settings : Les settings du mail, type, sujet, template - * @param array $datas : Les données à fournir au template du mail - */ - public static function send_email($to, $settings, $datas = []) - { - $controller = new \descartes\Controller(); - - ob_start(); - $controller->render($settings['template'], $datas); - $content = ob_get_clean(); - - return @mail($to, $settings['subject'], $content); - } - /** * Allow to read an uploaded file. * diff --git a/controllers/publics/Connect.php b/controllers/publics/Connect.php index bc860df..cedc6de 100644 --- a/controllers/publics/Connect.php +++ b/controllers/publics/Connect.php @@ -119,8 +119,9 @@ namespace controllers\publics; $token = $Tokenista->generate(3600, ['id_user' => $user['id']]); $reset_link = \descartes\Router::url('Connect', 'reset_password', ['id_user' => $user['id'], 'token' => $token]); - - \controllers\internals\Tool::send_email($email, EMAIL_RESET_PASSWORD, ['reset_link' => $reset_link]); + + $mailer = new \controllers\internals\Mailer(); + $email_send = $mailer->enqueue($email, EMAIL_RESET_PASSWORD, ['reset_link' => $reset_link]); return $this->render('connect/send-reset-password'); } diff --git a/controllers/publics/User.php b/controllers/publics/User.php index c2d4faf..5a2592e 100644 --- a/controllers/publics/User.php +++ b/controllers/publics/User.php @@ -171,7 +171,8 @@ class User extends \descartes\Controller return $this->redirect(\descartes\Router::url('User', 'add')); } - $email_send = \controllers\internals\Tool::send_email($email, EMAIL_CREATE_USER, ['email' => $email, 'password' => $password]); + $mailer = new \controllers\internals\Mailer(); + $email_send = $mailer->enqueue($email, EMAIL_CREATE_USER, ['email' => $email, 'password' => $password]); if (!$email_send) { \FlashMessage\FlashMessage::push('danger', 'Impossible d\'envoyer l\'e-mail à l\'utilisateur.'); diff --git a/daemons/Launcher.php b/daemons/Launcher.php index 066e68c..fda056f 100644 --- a/daemons/Launcher.php +++ b/daemons/Launcher.php @@ -50,6 +50,8 @@ class Launcher extends AbstractDaemon $this->start_sender_daemon(); $this->start_webhook_daemon(); + + $this->start_mailer_daemon(); $phones = $this->internal_phone->get_all(); $this->start_phones_daemons($phones); @@ -74,6 +76,25 @@ class Launcher extends AbstractDaemon $pid = null; exec('php ' . PWD . '/console.php controllers/internals/Console.php sender > /dev/null 2>&1 &'); } + + + /** + * Function to start mailer daemon. + */ + public function start_mailer_daemon() + { + $name = 'RaspiSMS Daemon Mailer'; + $pid_file = PWD_PID . '/' . $name . '.pid'; + + if (file_exists($pid_file)) + { + return false; + } + + //Create a new daemon for sender + $pid = null; + exec('php ' . PWD . '/console.php controllers/internals/Console.php mailer > /dev/null 2>&1 &'); + } /** * Function to start webhook daemon. diff --git a/daemons/Mailer.php b/daemons/Mailer.php index be77282..330db9b 100644 --- a/daemons/Mailer.php +++ b/daemons/Mailer.php @@ -55,7 +55,7 @@ class Mailer extends AbstractDaemon $message = null; $error_code = null; - $success = msg_receive($this->mailer_queue, QUEUE_TYPE_MAIL, $msgtype, $maxsize, $message, true, MSG_IPC_NOWAIT, $error_code); //MSG_IPC_NOWAIT == dont wait if no message found + $success = msg_receive($this->mailer_queue, QUEUE_TYPE_EMAIL, $msgtype, $maxsize, $message, true, MSG_IPC_NOWAIT, $error_code); //MSG_IPC_NOWAIT == dont wait if no message found if (!$success && MSG_ENOMSG !== $error_code) { $this->logger->critical('Error for mailer queue reading, error code : ' . $error_code); @@ -90,7 +90,7 @@ class Mailer extends AbstractDaemon public function on_start() { //Set last message at to construct time - $this->mailer_queue = msg_get_queue(QUEUE_ID_MAIL); + $this->mailer_queue = msg_get_queue(QUEUE_ID_EMAIL); $this->logger->info('Starting Mailer daemon with pid ' . getmypid()); } @@ -98,7 +98,7 @@ class Mailer extends AbstractDaemon public function on_stop() { //Delete queue on daemon close - $this->logger->info('Closing queue : ' . QUEUE_ID_MAIL); + $this->logger->info('Closing queue : ' . QUEUE_ID_EMAIL); msg_remove_queue($this->mailer_queue); $this->logger->info('Stopping Mailer daemon with pid ' . getmypid());