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

[*] скорректирован текст письма верификации 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

+10
View File
@@ -54,6 +54,16 @@
*/
const UPLOADS_PATH = ROOT_PATH . '/uploads';
/**
* Путь до папки почтовых шаблонов
*/
const MAIL_TEMPLATES_PATH = CORE_PATH . '/mail_templates';
/**
* Почтовый шаблон по умолчанию
*/
const MAIL_TEMPLATE_DEFAULT = 'default';
/**
* Путь до директории с логами
*/
+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];
}
}
+97
View File
@@ -0,0 +1,97 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head content="text/html" http-equiv="Content-Type" charset="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
</head>
<body>
<table style="padding:0;margin:0; font-family:'PT Sans', sans-serif; color: #404040;" width="100%" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td align="center">
<table width="680" border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bgcolor="#F3F3F3">
<tbody>
<tr>
<td align="center" height="50"></td>
</tr>
<tr>
<td align="center">
<h1>Delta Framework</h1>
</td>
</tr>
<tr>
<td align="center">
<table width="563" border="0" cellspacing="0" cellpadding="0" style="border-collapse: collapse" bgcolor="#ffffff">
<tbody>
<tr>
<td align="center" height="25">
</td>
</tr>
<tr>
<td align="center" height="39"></td>
</tr>
<tr>
<td align="center" style="font-size: 22px; line-height: 28px; font-weight: bold; color: #333333;">{TITLE}</td>
</tr>
<tr>
<td align="center" height="24"></td>
</tr>
<tr>
<td align="center">
<table border="0" cellspacing="0" cellpadding="0" style="border-collapse: collapse">
<tbody>
<tr>
<td align="left" width="440" style="font-size: 16px; line-height: 24px; color: #404040;">{BODY}</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td align="center" height="60"></td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td align="center" height="38"></td>
</tr>
<tr>
<td align="center">
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse">
<tbody>
<tr>
<td align="center" style="font-weight: bold; font-size: 14px; line-height: 26px; color: #48a501;">
<a href="admin@it-stories.ru" target="_blank"
style="font-weight: bold; font-size: 14px; line-height: 26px; color:#48a501; text-decoration: none;">admin@it-stories.ru</a>
</td>
<td align="center" width="30" style="font-weight: bold; font-size: 14px; line-height: 26px; color: #7CB648;">|</td>
<td align="center" style="font-weight: bold; font-size: 14px; line-height: 26px; color: #48a501;">Telegram @rsgrinko</td>
</tr>
</tbody>
</table>
<p class="MsoNormal" style="margin-bottom:0cm;margin-bottom:.0001pt;line-height: 19.5pt">
<b>
<u>
<span style="font-size:10.5pt;font-family:'Arial',sans-serif; mso-fareast-font-family:'Times New Roman';color:#7CB648;mso-fareast-language:RU">
<a href="https://it-stories.ru">
<span style="color:#48a501">it-stories.ru</span>
</a>
</span>
</u>
</b>
</p>
</td>
</tr>
<tr>
<td align="center" height="43"></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</body>
</html>