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 + + + + +

+
+
+ +