This commit is contained in:
snipp-ru committed 2017-01-18 18:54:01 +03:00
1 parent 6955f11efa
commit bc78b6fb6a
1 file changed
+35 -32
+35 -32
View File
@@ -4,7 +4,7 @@
*
* @author Snipp.ru
* @website http://snipp.ru/view/80
* @version 2.1
* @version 2.2
*/
class Mail
{
@@ -13,7 +13,7 @@ class Mail
*/
public $fromEmail = '';
public $fromName = '';
/**
* Кому.
*/
@@ -29,17 +29,17 @@ class Mail
* Текст.
*/
public $body = '';
/**
* Массив заголовков файлов.
*/
private $_files = array();
/**
* Управление дампированием.
*/
public $dump = false;
/**
* Директория куда сохранять письма.
*/
@@ -63,7 +63,7 @@ class Mail
'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;'
);
/**
* Проверка существования файла.
* Если директория не существует - пытается её создать.
@@ -75,23 +75,24 @@ class Mail
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;
}
/**
* От кого.
*/
@@ -100,7 +101,7 @@ class Mail
$this->fromEmail = $email;
$this->fromName = $name;
}
/**
* Кому.
*/
@@ -109,7 +110,7 @@ class Mail
$this->toEmail = $email;
$this->toName = $name;
}
/**
* Добавление файла к письму.
*/
@@ -120,7 +121,6 @@ class Mail
$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',
@@ -137,26 +137,31 @@ class Mail
public function addHtmlStyle($html)
{
foreach ($this->_styles as $tag => $style) {
preg_match_all('/<' . $tag . '(.*?)>/i', $html, $matchs, PREG_SET_ORDER);
preg_match_all('/<' . $tag . '([\s].*?)?>/i', $html, $matchs, PREG_SET_ORDER);
foreach ($matchs as $match) {
preg_match_all('/[ ]?(.*?)=[\"|\'](.*?)[\"|\'][ ]?/', $match[1], $chanks);
$attrs = array_combine($chanks[1], $chanks[2]);
$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;
}
@@ -168,10 +173,10 @@ class Mail
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) {
@@ -180,10 +185,10 @@ class Mail
$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">
@@ -194,12 +199,12 @@ class Mail
' . $this->body . '
</body>
</html>';
// Добавление стилей к тегам.
$body = $this->addHtmlStyle($body);
$boundary = md5(uniqid(time()));
// Заголовок письма.
$headers = array(
'Content-Type: multipart/mixed; boundary="' . $boundary . '"',
@@ -217,33 +222,31 @@ class Mail
'',
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__) . '/sendmail';
}
$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;
}
}