[+] добавлена возможность отправки писем с почтовыми шаблонами

[*] скорректирован текст письма верификации e-mail
This commit is contained in:
rsgrinko committed 2022-08-15 15:35:00 +03:00
1 parent 015b977190
commit 51d11c838f
4 files changed
+174 -6

No files matched your search

+61 -1
View File
@@ -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, '<br>', $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 = [
+6 -5
View File
@@ -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('Код верификации: <b>' . $this->getVerificationCode() . '</b>' . PHP_EOL . 'Для подтверждения E-Mail перейдите по <a href="' . SITE_URL_CORE . '/verification.php?code=' . $this->getVerificationCode() . '">ссылке</a>')
->setTemplateVars(['TITLE' => 'Подтверждение E-Mail'])
->send()[0];
}
}