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