From 24d0281ffa847c2b57345dd75a03971cc81cda36 Mon Sep 17 00:00:00 2001 From: rsgrinko Date: Wed, 3 May 2023 22:22:10 +0300 Subject: [PATCH] =?UTF-8?q?[+]=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8?= =?UTF-8?q?=D0=BE=D0=BD=D0=B0=D0=BB=20=D0=BE=D1=82=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BA=D0=B8=20=D0=BF=D0=B8=D1=81=D0=B5=D0=BC=20=D1=87=D0=B5?= =?UTF-8?q?=D1=80=D0=B5=D0=B7=20=D0=BE=D1=87=D0=B5=D1=80=D0=B5=D0=B4=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/lib/Core/Api/ApiController.class.php | 37 +++++++++++------- .../Core/Helpers/SystemFunctions.class.php | 3 +- core/lib/Core/MQTasks.class.php | 38 +++++++++++++++++++ 3 files changed, 63 insertions(+), 15 deletions(-) diff --git a/core/lib/Core/Api/ApiController.class.php b/core/lib/Core/Api/ApiController.class.php index 4b2e697..03917b0 100644 --- a/core/lib/Core/Api/ApiController.class.php +++ b/core/lib/Core/Api/ApiController.class.php @@ -226,20 +226,29 @@ */ public function sendMail(): void { - $mailObject = new Mail(); - $mailTo = $this->request->getProperty('to'); - $mailSubject = $this->request->getProperty('subject'); - $mailBody = $this->request->getProperty('body'); - $mailTemplate = $this->request->getProperty('template'); - $mailFrom = 'noreply@it-stories.ru'; - $result = $mailObject->setTo($mailTo, 'user') - ->setFrom($mailFrom, 'Info System') - ->setTemplate($mailTemplate ?: MAIL_TEMPLATE_DEFAULT) - ->setTemplateVars(['TITLE' => $mailSubject]) - ->setSubject($mailSubject) - ->setBody($mailBody) - ->send(); - ApiView::output(['result' => $result]); + $responseObject = (new MQ())->setPriority(7) + ->setAttempts(2) + ->createTask('Core\MQTasks', + 'sendMail', + [ + SERVER_EMAIL, + SERVER_EMAIL_NAME, + $this->request->getProperty('to'), + null, + $this->request->getProperty('subject'), + $this->request->getProperty('body'), + MAIL_TEMPLATE_DEFAULT, + ['TITLE' => $this->request->getProperty('subject')], + ] + ); + + + ApiView::output( + [ + 'status' => $responseObject->getStatus(), + 'response' => $responseObject->getResponse(), + ] + ); } diff --git a/core/lib/Core/Helpers/SystemFunctions.class.php b/core/lib/Core/Helpers/SystemFunctions.class.php index 88c5442..bc95e8d 100644 --- a/core/lib/Core/Helpers/SystemFunctions.class.php +++ b/core/lib/Core/Helpers/SystemFunctions.class.php @@ -43,7 +43,8 @@ ]; foreach ($keys as $key) { if (!empty($_SERVER[$key])) { - $ip = trim(end(explode(',', $_SERVER[$key]))); + $arData = explode(',', $_SERVER[$key]); + $ip = trim(end($arData)); if (filter_var($ip, FILTER_VALIDATE_IP)) { return $ip; } diff --git a/core/lib/Core/MQTasks.class.php b/core/lib/Core/MQTasks.class.php index ffe8d5a..fadba8f 100644 --- a/core/lib/Core/MQTasks.class.php +++ b/core/lib/Core/MQTasks.class.php @@ -22,6 +22,7 @@ namespace Core; use Core\Helpers\Cache; + use Core\Helpers\Mail; use Core\Helpers\SystemFunctions; use Core\Helpers\Thumbs; use Core\ExternalServices\TelegramSender; @@ -458,4 +459,41 @@ DELETE from `jokes` WHERE `jokes`.id not in (SELECT id FROM t_temp);' return 'Добавлено постов: ' . count($result); } + /** + * Отправка E-Mail + * + * @param string $fromMail E-Mail отправителя + * @param string|null $fromName Имя отправителя + * @param string $toMail E-Mail получателя + * @param string|null $toName Имя получателя + * @param string $subject Тема письма + * @param string $body Тело письма + * @param string|null $template Шаблон + * @param array|null $templateVars Переменные шаблона + * + * @return array + * @throws CoreException + */ + public static function sendMail( + string $fromMail, + ?string $fromName, + string $toMail, + ?string $toName, + string $subject, + string $body, + ?string $template = null, + ?array $templateVars = null + ): array { + $mailObject = new Mail(); + $mailObject->setTo($toMail, $toName) + ->setFrom($fromMail, $fromName) + ->setSubject($subject)->setBody($body); + if ($template !== null && $templateVars !== null) { + $mailObject->setTemplate($template) + ->setTemplateVars($templateVars); + } + + return $mailObject->send(); + } + } \ No newline at end of file