[+] добавлен функционал почтовых рассылок
This commit is contained in:
1 parent
a2eae57a11
commit
689753ddf1
7 files changed
+232
-382
No files matched your search
+1
-31
@@ -274,36 +274,6 @@ function getOS() {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Отправка уведомления на почту администратору с использованием шаблонов
|
||||
*/
|
||||
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',
|
||||
'LINK' => 'https://it-stories.ru/',
|
||||
'LINKNAME' => 'Go to site',
|
||||
'FOOTER' => 'Thanks!',
|
||||
'SERVERNAME' => 'it-stories.ru'
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
|
||||
// прикрепляем лог, если он есть
|
||||
if(isset($file) and !empty($file) and file_exists($file)){
|
||||
$mail->addFile($file);
|
||||
}
|
||||
$mail->sendUsingTemplate($template);
|
||||
|
||||
return;
|
||||
}
|
||||
/**
|
||||
* Отправка уведомления пользователю
|
||||
*
|
||||
@@ -332,7 +302,7 @@ function adminSendMailWithTemplate($template, $subject, $message, $file = false)
|
||||
if(isset($file) and !empty($file) and file_exists($file)){
|
||||
$mail->addFile($file);
|
||||
}
|
||||
$mail->send();
|
||||
$mail->sendDefault();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
+1
-1
@@ -162,7 +162,7 @@ if(!CUser::is_user() and $_SERVER['REQUEST_URI'] !== '/login.php') {
|
||||
</li>
|
||||
|
||||
<li class="nav-item <?php echo (basename($_SERVER['PHP_SELF']) == 'mailings.php') ? 'active' : '';?>">
|
||||
<a href="<?php echo HOME; ?>/mailings.php"><i class="fa fa-tasks"></i> Почтовые рассылки</a>
|
||||
<a href="<?php echo HOME; ?>/mailings.php"><i class="fa fa-envelope-o"></i> Почтовые рассылки</a>
|
||||
</li>
|
||||
|
||||
<li class="nav-item <?php echo (basename($_SERVER['PHP_SELF']) == 'cron.php' or basename($_SERVER['PHP_SELF']) == 'add_cron.php' or basename($_SERVER['PHP_SELF']) == 'edit_cron.php') ? 'active' : '';?>">
|
||||
|
||||
+87
-340
@@ -1,346 +1,8 @@
|
||||
<?php
|
||||
/**
|
||||
* Класс для отправки E-mail
|
||||
* Данный файл входит в состав системы IoT Core System
|
||||
* Разработчик: Роман Сергеевич Гринько
|
||||
* E-mail: rsgrinko@gmail.com
|
||||
* Cайт: https://it-stories.ru
|
||||
* @author rsgrinko@gmail.com
|
||||
*/
|
||||
|
||||
class CMail
|
||||
{
|
||||
/**
|
||||
* От кого
|
||||
*/
|
||||
public $fromEmail = '';
|
||||
public $fromName = '';
|
||||
|
||||
/**
|
||||
* Кому
|
||||
*/
|
||||
public $toEmail = '';
|
||||
public $toName = '';
|
||||
|
||||
/**
|
||||
* Тема
|
||||
*/
|
||||
public $subject = '';
|
||||
|
||||
/**
|
||||
* Текст
|
||||
*/
|
||||
public $body = '';
|
||||
|
||||
/**
|
||||
* Массив с данными для подстановки в шаблон
|
||||
*/
|
||||
public $arrTemplate = [];
|
||||
|
||||
/**
|
||||
* Массив заголовков файлов
|
||||
*/
|
||||
private $_files = array();
|
||||
|
||||
/**
|
||||
* Управление дампированием
|
||||
*/
|
||||
public $dump = false;
|
||||
|
||||
/**
|
||||
* Директория куда сохранять письма
|
||||
*/
|
||||
public $dumpPath = '';
|
||||
|
||||
/**
|
||||
* CSS стили для тегов письма
|
||||
*/
|
||||
private $_styles = array(
|
||||
'body' => 'margin: 0 0 0 0; padding: 10px 10px 10px 10px; background: #ffffff; color: #000000; font-size: 14px; font-family: Arial, Helvetica, sans-serif; line-height: 18px;',
|
||||
'a' => 'color: #003399; text-decoration: underline; font-size: 14px; font-family: Arial, Helvetica, sans-serif; line-height: 18px;',
|
||||
'p' => 'margin: 0 0 20px 0; padding: 0 0 0 0; color: #000000; font-size: 14px; font-family: Arial, Helvetica, sans-serif; line-height: 18px;',
|
||||
'ul' => 'margin: 0 0 20px 20px; padding: 0 0 0 0; color: #000000; font-size: 14px; font-family: Arial, Helvetica, sans-serif; line-height: 18px;',
|
||||
'ol' => 'margin: 0 0 20px 20px; padding: 0 0 0 0; color: #000000; font-size: 14px; font-family: Arial, Helvetica, sans-serif; line-height: 18px;',
|
||||
'table' => 'margin: 0 0 20px 0; border: 1px solid #dddddd; border-collapse: collapse;',
|
||||
'th' => 'padding: 10px; border: 1px solid #dddddd; vertical-align: middle; background-color: #eeeeee; color: #000000; font-size: 14px; font-family: Arial, Helvetica, sans-serif; line-height: 18px;',
|
||||
'td' => 'padding: 10px; border: 1px solid #dddddd; vertical-align: middle; background-color: #ffffff; color: #000000; font-size: 14px; font-family: Arial, Helvetica, sans-serif; line-height: 18px;',
|
||||
'h1' => 'margin: 0 0 20px 0; padding: 0 0 0 0; color: #000000; font-size: 22px; font-family: Arial, Helvetica, sans-serif; line-height: 26px; font-weight: bold;',
|
||||
'h2' => 'margin: 0 0 20px 0; padding: 0 0 0 0; color: #000000; font-size: 20px; font-family: Arial, Helvetica, sans-serif; line-height: 24px; font-weight: bold;',
|
||||
'h3' => 'margin: 0 0 20px 0; padding: 0 0 0 0; color: #000000; font-size: 18px; font-family: Arial, Helvetica, sans-serif; line-height: 22px; font-weight: bold;',
|
||||
'h4' => 'margin: 0 0 20px 0; padding: 0 0 0 0; color: #000000; font-size: 16px; font-family: Arial, Helvetica, sans-serif; line-height: 20px; font-weight: bold;',
|
||||
'hr' => 'height: 1px; border: none; color: #dddddd; background: #dddddd; margin: 0 0 20px 0;'
|
||||
);
|
||||
|
||||
/**
|
||||
* Проверка существования файла
|
||||
* Если директория не существует - пытается её создать.
|
||||
* Если файл существует - к концу файла приписывает префикс.
|
||||
*/
|
||||
private function safeFile($filename)
|
||||
{
|
||||
$dir = dirname($filename);
|
||||
if (!is_dir($dir)) {
|
||||
mkdir($dir, 0777, true);
|
||||
}
|
||||
|
||||
$info = pathinfo($filename);
|
||||
$name = $dir . '/' . $info['filename'];
|
||||
$ext = (empty($info['extension'])) ? '' : '.' . $info['extension'];
|
||||
$prefix = '';
|
||||
|
||||
if (is_file($name . $ext)) {
|
||||
$i = 1;
|
||||
$prefix = '_' . $i;
|
||||
|
||||
while (is_file($name . $prefix . $ext)) {
|
||||
$prefix = '_' . ++$i;
|
||||
}
|
||||
}
|
||||
|
||||
return $name . $prefix . $ext;
|
||||
}
|
||||
|
||||
/**
|
||||
* От кого
|
||||
*/
|
||||
public function from($email, $name = null)
|
||||
{
|
||||
$this->fromEmail = $email;
|
||||
$this->fromName = $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Кому
|
||||
*/
|
||||
public function to($email, $name = null)
|
||||
{
|
||||
$this->toEmail = $email;
|
||||
$this->toName = $name;
|
||||
}
|
||||
|
||||
public function assignTemplateVars($arrVars) {
|
||||
$this->arrTemplate = $arrVars;
|
||||
}
|
||||
|
||||
/**
|
||||
* Добавление файла к письму
|
||||
*/
|
||||
public function addFile($filename)
|
||||
{
|
||||
if (is_file($filename)) {
|
||||
$name = basename($filename);
|
||||
$fp = fopen($filename, 'rb');
|
||||
$file = fread($fp, filesize($filename));
|
||||
fclose($fp);
|
||||
$this->_files[] = array(
|
||||
'Content-Type: application/octet-stream; name="' . $name . '"',
|
||||
'Content-Transfer-Encoding: base64',
|
||||
'Content-Disposition: attachment; filename="' . $name . '"',
|
||||
'',
|
||||
chunk_split(base64_encode($file)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Добавление к тегам стили
|
||||
*/
|
||||
public function addHtmlStyle($html)
|
||||
{
|
||||
foreach ($this->_styles as $tag => $style) {
|
||||
preg_match_all('/<' . $tag . '([\s].*?)?>/i', $html, $matchs, PREG_SET_ORDER);
|
||||
foreach ($matchs as $match) {
|
||||
$attrs = array();
|
||||
if (!empty($match[1])) {
|
||||
preg_match_all('/[ ]?(.*?)=[\"|\'](.*?)[\"|\'][ ]?/', $match[1], $chanks);
|
||||
if (!empty($chanks[1]) && !empty($chanks[2])) {
|
||||
$attrs = array_combine($chanks[1], $chanks[2]);
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($attrs['style'])) {
|
||||
$attrs['style'] = $style;
|
||||
} else {
|
||||
$attrs['style'] = rtrim($attrs['style'], '; ') . '; ' . $style;
|
||||
}
|
||||
|
||||
$compile = array();
|
||||
foreach ($attrs as $name => $value) {
|
||||
$compile[] = $name . '="' . $value . '"';
|
||||
}
|
||||
|
||||
$html = str_replace($match[0], '<' . $tag . ' ' . implode(' ', $compile) . '>', $html);
|
||||
}
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Отправка
|
||||
*/
|
||||
public function send()
|
||||
{
|
||||
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 = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ru" lang="ru">
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
</head>
|
||||
<body>
|
||||
' . $this->body . '
|
||||
</body>
|
||||
</html>';
|
||||
|
||||
// Добавление стилей к тегам
|
||||
$body = $this->addHtmlStyle($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);
|
||||
$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_contents(__DIR__.'/../../assets/mail_templates/'.$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);
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Класс для отправки E-Mail с использованием почтовых шаблонов
|
||||
*/
|
||||
class CMail2 {
|
||||
class CMail {
|
||||
/**
|
||||
* Шаблон письма
|
||||
*/
|
||||
@@ -480,7 +142,7 @@ class CMail2 {
|
||||
}
|
||||
|
||||
/**
|
||||
* Отправка
|
||||
* Отправка с использованием шаблонов
|
||||
*/
|
||||
public function send() {
|
||||
if (empty($this->toEmail)) {
|
||||
@@ -556,4 +218,89 @@ class CMail2 {
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Отправка с использованием встроенных стилей
|
||||
*/
|
||||
public function sendDefault()
|
||||
{
|
||||
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 = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ru" lang="ru">
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
</head>
|
||||
<body>
|
||||
' . $this->body . '
|
||||
</body>
|
||||
</html>';
|
||||
|
||||
// Добавление стилей к тегам
|
||||
$body = $this->addHtmlStyle($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);
|
||||
$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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user