Добавлена поддержка шабонов в письмах

This commit is contained in:
rsgrinko committed 2021-12-07 13:58:46 +03:00
1 parent 25fc2ab9bc
commit e8496feed8
4 files changed
+262 -3

No files matched your search

+148
View File
@@ -0,0 +1,148 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width"/>
<style type="text/css">
* {
margin: 0;
padding: 0;
font-size: 100%;
font-family: 'Avenir Next', "Helvetica Neue", "Helvetica", Helvetica, Arial, sans-serif;
line-height: 1.65; }
img {
max-width: 100%;
margin: 0 auto;
display: block; }
body,
.body-wrap {
width: 100% !important;
height: 100%;
background: #efefef;
-webkit-font-smoothing: antialiased;
-webkit-text-size-adjust: none; }
a {
color: #71bc37;
text-decoration: none; }
.text-center {
text-align: center; }
.text-right {
text-align: right; }
.text-left {
text-align: left; }
.button {
display: inline-block;
color: white;
background: #71bc37;
border: solid #71bc37;
border-width: 10px 20px 8px;
font-weight: bold;
border-radius: 4px; }
h1, h2, h3, h4, h5, h6 {
margin-bottom: 20px;
line-height: 1.25; }
h1 {
font-size: 32px; }
h2 {
font-size: 28px; }
h3 {
font-size: 24px; }
h4 {
font-size: 20px; }
h5 {
font-size: 16px; }
p, ul, ol {
font-size: 16px;
font-weight: normal;
margin-bottom: 20px; }
.container {
display: block !important;
clear: both !important;
margin: 0 auto !important;
max-width: 580px !important; }
.container table {
width: 100% !important;
border-collapse: collapse; }
.container .masthead {
padding: 80px 0;
background: #71bc37;
color: white; }
.container .masthead h1 {
margin: 0 auto !important;
max-width: 90%;
text-transform: uppercase; }
.container .content {
background: white;
padding: 30px 35px; }
.container .content.footer {
background: none; }
.container .content.footer p {
margin-bottom: 0;
color: #888;
text-align: center;
font-size: 14px; }
.container .content.footer a {
color: #888;
text-decoration: none;
font-weight: bold; }
</style>
</head>
<body>
<table class="body-wrap">
<tr>
<td class="container">
<table>
<tr>
<td align="center" class="masthead">
<h1>{HEADER}</h1>
</td>
</tr>
<tr>
<td class="content">
<h2>{TITLE}</h2>
<p>{MESSAGE}</p>
<table>
<tr>
<td align="center">
<p>
<a href="{LINK}}" class="button">{LINKNAME}</a>
</p>
</td>
</tr>
</table>
<p>{FOOTER}</p>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td class="container">
<table>
<tr>
<td class="content footer" align="center">
<p>Сообщение сгенерировано системой <a href="{HOME}">IoT Core Board</a>, {SERVERNAME}</p>
<p><a href="mailto:rsgrinko@gmail.com">E-Mail: rsgrinko@gmail.com</a> | <a href="https://telegram.me/rsgrinko">Telegram @rsgrinko</a></p>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
+29
View File
@@ -273,6 +273,35 @@ function getOS() {
return;
}
/**
* Отправка уведомления на почту администратору с использованием шаблонов
*/
function adminSendMailWithTemplate($template, $subject, $message, $file = false){
$mail = new CMail;
$mail->from('iot@'.$_SERVER['SERVER_NAME'], 'Система оповещений IoT Core');
$mail->to(ADMIN_EMAIL, 'Администратор панели');
$mail->subject = $subject;
$mail->assignTemplateVars(
array(
'HEADER' => 'IoT Core',
'MESSAGE' => 'test0',
'TITLE' => 'test title',
'LINKNAME' => 'Go to site',
'FOOTER' => 'Thanks!'
)
);
// прикрепляем лог, если он есть
if(isset($file) and !empty($file) and file_exists($file)){
$mail->addFile($file);
}
$mail->sendUsingTemplate($template);
return;
}
/**
* Отправка уведомления пользователю
*
+83 -1
View File
@@ -31,6 +31,11 @@ class CMail
* Текст
*/
public $body = '';
/**
* Массив с данными для подстановки в шаблон
*/
public $arrTemplate = [];
/**
* Массив заголовков файлов
@@ -112,6 +117,10 @@ class CMail
$this->toEmail = $email;
$this->toName = $name;
}
public function assignTemplateVars($arrVars) {
$this->arrTemplate = $arrVars;
}
/**
* Добавление файла к письму
@@ -239,7 +248,80 @@ class CMail
// Дамп письма в файл
if ($this->dump == true) {
if (empty($this->dumpPath)) {
$this->dumpPath = dirname(__FILE__) . '/sendmail';
$this->dumpPath = dirname(__FILE__) . '/uploads';
}
$dump = array_merge($headers, array('To: ' . $to, 'Subject: ' . $subject, ''), $message);
$file = $this->safeFile($this->dumpPath . '/' . date('Y-m-d_H-i-s') . '.eml');
file_put_contents($file, implode("\r\n", $dump));
}
$res[] = mb_send_mail($to, $subject, implode("\r\n", $message), implode("\r\n", $headers));
}
return $res;
}
public function sendUsingTemplate($template = 'default') {
if (empty($this->toEmail)) {
return false;
}
// От кого
$from = (empty($this->fromName)) ? $this->fromEmail : '=?UTF-8?B?' . base64_encode($this->fromName) . '?= <' . $this->fromEmail . '>';
// Кому
$array_to = array();
foreach (explode(',', $this->toEmail) as $row) {
$row = trim($row);
if (!empty($row)) {
$array_to[] = (empty($this->toName)) ? $row : '=?UTF-8?B?' . base64_encode($this->toName) . '?= <' . $row . '>';
}
}
// Тема письма
$subject = (empty($this->subject)) ? 'No subject' : $this->subject;
$body = file_get_contenst(__DIR__.'/../../assets/mail_template/'.$template.'.html');
foreach($this->arrTemplate as $templateKey => $templateValue){
$body = str_replace('{'.$templateKey.'}', $templateValue), $body);
}
$boundary = md5(uniqid(time()));
// Заголовок письма
$headers = array(
'Content-Type: multipart/mixed; boundary="' . $boundary . '"',
'Content-Transfer-Encoding: 7bit',
'MIME-Version: 1.0',
'From: ' . $from,
'Date: ' . date('r')
);
// Тело письма
$message = array(
'--' . $boundary,
'Content-Type: text/html; charset=UTF-8',
'Content-Transfer-Encoding: base64',
'',
chunk_split(base64_encode($body))
);
if (!empty($this->_files)) {
foreach ($this->_files as $row) {
$message = array_merge($message, array('', '--' . $boundary), $row);
}
}
$message[] = '';
$message[] = '--' . $boundary . '--';
$res = array();
foreach ($array_to as $to) {
// Дамп письма в файл
if ($this->dump == true) {
if (empty($this->dumpPath)) {
$this->dumpPath = dirname(__FILE__) . '/uploads';
}
$dump = array_merge($headers, array('To: ' . $to, 'Subject: ' . $subject, ''), $message);
+2 -2
View File
@@ -6,6 +6,6 @@ require_once __DIR__.'/inc/bootstrap.php';
if(!CUser::is_admin()) {
die('403 - Access denied');
}
userSendMail(1, 'Test userMail', 'Hello!!!',false);
//adminSendMail('Тестовое уведомление', 'Запушен тестовый скрипт для проверки функционала почтовых уведомлений.<br>Если Вы видите данное сообщение - значит система работает.');
//userSendMail(1, 'Test userMail', 'Hello!!!',false);
adminSendMailWithTemplate('default', 'Тестовое уведомление', 'Запушен тестовый скрипт для проверки функционала почтовых уведомлений.<br>Если Вы видите данное сообщение - значит система работает.');
echo 'ok';