diff --git a/assets/mail_templates/default.html b/assets/mail_templates/default.html
new file mode 100644
index 0000000..24e993a
--- /dev/null
+++ b/assets/mail_templates/default.html
@@ -0,0 +1,148 @@
+
+
+
+
+
+
+
+
+
+ {HEADER}
+ |
+
+
+
+ {TITLE}
+ {MESSAGE}
+
+ {FOOTER}
+ |
+
+
+ |
+
+
+ |
+
+ |
+
+
+
+
\ No newline at end of file
diff --git a/inc/func.php b/inc/func.php
index f445e7a..fd93233 100644
--- a/inc/func.php
+++ b/inc/func.php
@@ -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;
+ }
/**
* Отправка уведомления пользователю
*
diff --git a/inc/lib/CMail.class.php b/inc/lib/CMail.class.php
index db4d249..66c9175 100644
--- a/inc/lib/CMail.class.php
+++ b/inc/lib/CMail.class.php
@@ -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);
diff --git a/test.php b/test.php
index 9c3661c..d12d66f 100644
--- a/test.php
+++ b/test.php
@@ -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('Тестовое уведомление', 'Запушен тестовый скрипт для проверки функционала почтовых уведомлений.