From 51d11c838f0b8bd9fccb2acef94b0f7c45c252df Mon Sep 17 00:00:00 2001 From: rsgrinko Date: Mon, 15 Aug 2022 15:35:00 +0300 Subject: [PATCH] =?UTF-8?q?[+]=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD?= =?UTF-8?q?=D0=BE=D1=81=D1=82=D1=8C=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=81=20=D0=BF?= =?UTF-8?q?=D0=BE=D1=87=D1=82=D0=BE=D0=B2=D1=8B=D0=BC=D0=B8=20=D1=88=D0=B0?= =?UTF-8?q?=D0=B1=D0=BB=D0=BE=D0=BD=D0=B0=D0=BC=D0=B8=20[*]=20=D1=81=D0=BA?= =?UTF-8?q?=D0=BE=D1=80=D1=80=D0=B5=D0=BA=D1=82=D0=B8=D1=80=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D0=BD=20=D1=82=D0=B5=D0=BA=D1=81=D1=82=20=D0=BF=D0=B8?= =?UTF-8?q?=D1=81=D1=8C=D0=BC=D0=B0=20=D0=B2=D0=B5=D1=80=D0=B8=D1=84=D0=B8?= =?UTF-8?q?=D0=BA=D0=B0=D1=86=D0=B8=D0=B8=20e-mail?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/config.php | 10 +++ core/lib/Core/Helpers/Mail.class.php | 62 +++++++++++++++++- core/lib/Core/Models/User.class.php | 11 ++-- core/mail_templates/default.html | 97 ++++++++++++++++++++++++++++ 4 files changed, 174 insertions(+), 6 deletions(-) create mode 100644 core/mail_templates/default.html diff --git a/core/config.php b/core/config.php index 2e42aea..0dd78ba 100644 --- a/core/config.php +++ b/core/config.php @@ -54,6 +54,16 @@ */ const UPLOADS_PATH = ROOT_PATH . '/uploads'; + /** + * Путь до папки почтовых шаблонов + */ + const MAIL_TEMPLATES_PATH = CORE_PATH . '/mail_templates'; + + /** + * Почтовый шаблон по умолчанию + */ + const MAIL_TEMPLATE_DEFAULT = 'default'; + /** * Путь до директории с логами */ diff --git a/core/lib/Core/Helpers/Mail.class.php b/core/lib/Core/Helpers/Mail.class.php index 9e2bdd5..8f69465 100644 --- a/core/lib/Core/Helpers/Mail.class.php +++ b/core/lib/Core/Helpers/Mail.class.php @@ -21,6 +21,7 @@ namespace Core\Helpers; + use Core\CoreException; use Core\Models\User; use Core\Helpers\Log; @@ -58,6 +59,16 @@ */ private $_files = []; + /** + * Почтовый шаблон + */ + private $templateName = null; + + /** + * Массив переменных шаблона + */ + private $templateVars = []; + /** * @var User|null $user Объект пользователя */ @@ -69,9 +80,55 @@ public function __construct(User $user = null) { if($user !== null) { $this->user = $user; - $this->setFrom(SERVER_EMAIL, SERVER_EMAIL_NAME)->setTo($user->getEmail(), $user->getName()); + $this->setFrom(SERVER_EMAIL, SERVER_EMAIL_NAME) + ->setTo($user->getEmail(), $user->getName()) + ->setTemplate(MAIL_TEMPLATE_DEFAULT); } } + + /** + * Использовать шаблон + * + * @param string $templateName Имя шаблона + * + * @return $this + */ + public function setTemplate(string $templateName): self + { + if(!file_exists(MAIL_TEMPLATES_PATH . '/' . $templateName . '.html')) { + throw new CoreException('Шаблон "' . $templateName . '" не найден'); + } + $this->templateName = $templateName; + return $this; + } + + /** + * Определение переменных шаблона + * + * @param array $templateVars Массив переменных шаблона + * + * @return $this + */ + public function setTemplateVars(array $templateVars): self + { + foreach($templateVars as $key => $value){ + $this->templateVars['{' . $key . '}'] = $value; + } + + return $this; + } + + private function messageConversionForTemplate(): self + { + $mailTemplate = file_get_contents(MAIL_TEMPLATES_PATH . '/' . $this->templateName . '.html'); + $this->body = str_replace("\r\n", PHP_EOL, $this->body); + $this->body = str_replace("\n\n", PHP_EOL, $this->body); + $this->body = str_replace(PHP_EOL, '
', $this->body); + $this->templateVars['{BODY}'] = $this->body; + $this->body = str_replace(array_keys($this->templateVars), array_values($this->templateVars), $mailTemplate); + return $this; + } + /** * От кого * @@ -161,6 +218,9 @@ } $subject = (empty($this->subject)) ? 'No subject' : $this->subject; + if($this->templateName !== null) { + $this->messageConversionForTemplate(); + } $body = $this->body; $boundary = md5(uniqid(time(), true)); $headers = [ diff --git a/core/lib/Core/Models/User.class.php b/core/lib/Core/Models/User.class.php index 055913f..20d5e45 100644 --- a/core/lib/Core/Models/User.class.php +++ b/core/lib/Core/Models/User.class.php @@ -746,19 +746,20 @@ /** * Отправка кода верификации пользователю на почту * - * @return void + * @return bool Результат отправки * @throws CoreException */ - public function sendVerificationCode(): void + public function sendVerificationCode(): bool { if (empty($this->getAllUserData()['verification_code'])) { Log::logToFile('Ошибка отправки кода верификации: код отсутствует', 'User.log', ['userId' => $this->id]); throw new CoreException('Ошибка отправки кода верификации: код отсутствует'); } Log::logToFile('Выслан код верификаци', 'User.log', ['userId' => $this->id, 'code' => $this->getAllUserData()['verification_code']]); - $this->getMailObject() + return (bool)$this->getMailObject() ->setSubject('Подтверждение E-Mail') - ->setBody('Для подтверждения E-Mail перейдите по ссылке: ' . SITE_URL_CORE . '/verification.php?code=' . $this->getVerificationCode()) - ->send(); + ->setBody('Код верификации: ' . $this->getVerificationCode() . '' . PHP_EOL . 'Для подтверждения E-Mail перейдите по ссылке') + ->setTemplateVars(['TITLE' => 'Подтверждение E-Mail']) + ->send()[0]; } } \ No newline at end of file diff --git a/core/mail_templates/default.html b/core/mail_templates/default.html new file mode 100644 index 0000000..697777b --- /dev/null +++ b/core/mail_templates/default.html @@ -0,0 +1,97 @@ + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + +
+

Delta Framework

+
+ + + + + + + + + + + + + + + + + + + + + +
+
{TITLE}
+ + + + + + +
{BODY}
+
+
+ + + + + + + + +
+ admin@it-stories.ru + |Telegram @rsgrinko
+

+ + + + + it-stories.ru + + + + +

+
+
+ +