215 lines
5.6 KiB
PHP
215 lines
5.6 KiB
PHP
<?php
|
||
|
||
/**
|
||
* Класс для работы с почтой
|
||
* http://en.wikipedia.org/wiki/MIME
|
||
*/
|
||
class Mail
|
||
{
|
||
public $_from;
|
||
public $_to;
|
||
public $_subject;
|
||
public $content;
|
||
public $copy;
|
||
|
||
private $encoding;
|
||
private $_notify = null;
|
||
|
||
protected $attachment = array ();
|
||
protected $uniqid;
|
||
protected $type = "text/plain";
|
||
|
||
function __construct() {
|
||
$this->setEncoding("UTF-8");
|
||
$this->uniqid = strtoupper(uniqid(time())); // Идентефикатор разделителя
|
||
}
|
||
|
||
/**
|
||
* Установка отправителя
|
||
*/
|
||
function from($name)
|
||
{
|
||
$this->_from = $name;
|
||
}
|
||
|
||
/**
|
||
* Установка получателя
|
||
*/
|
||
function to($name) // recipient
|
||
{
|
||
$this->_to = $name;
|
||
}
|
||
|
||
/**
|
||
* Установка получателей копии
|
||
*/
|
||
function copy($name) // recipient cc
|
||
{
|
||
$this->copy = $name;
|
||
}
|
||
|
||
function notify($notify)
|
||
{
|
||
$this->_notify = $notify;
|
||
}
|
||
|
||
/**
|
||
* Тема письма
|
||
*/
|
||
function subject($subject)
|
||
{
|
||
$this->_subject = $subject;
|
||
}
|
||
|
||
/**
|
||
* Текст письма
|
||
*/
|
||
function setContent($text)
|
||
{
|
||
$this->content = $text;
|
||
}
|
||
|
||
/**
|
||
* Кодировка текста в письме
|
||
*/
|
||
function setEncoding($encoding)
|
||
{
|
||
$this->encoding = $encoding;
|
||
}
|
||
|
||
/**
|
||
* Добавление вложения из файла
|
||
*/
|
||
function addAttachment($filename, $name = false)
|
||
{
|
||
assert(is_string($filename));
|
||
|
||
if(file_exists($filename)) { // assert ??
|
||
$file = fopen($filename, "rb");
|
||
if (is_resource($file)) {
|
||
$data = fread($file, filesize($filename));
|
||
$this->attachment [] = ($name) ? array($data, $name) : array($data, basename($filename));
|
||
}
|
||
}
|
||
}
|
||
|
||
function setType($type)
|
||
{
|
||
$this->type = $type;
|
||
}
|
||
|
||
/**
|
||
* Добавление вложения из строки с указанием имени файла
|
||
*/
|
||
function addAttachmentRaw($data, $name)
|
||
{
|
||
assert(is_string($name));
|
||
|
||
$this->attachment [] = array($data, $name);
|
||
}
|
||
|
||
function quote($var, $val)
|
||
{
|
||
return ";" . PHP_EOL . "\t" . $var . "=\"" . $val . "\"";
|
||
}
|
||
|
||
/**
|
||
* Общий формат тегов MIME
|
||
* @see http://tools.ietf.org/html/rfc2045
|
||
*/
|
||
function mimeTag($name, $value, array $args = array())
|
||
{
|
||
assert (is_string($name));
|
||
assert (is_string($value));
|
||
|
||
return $name . ": " . $value . implode("", array_map(array($this, 'quote'), array_keys($args), array_values($args))) . PHP_EOL;
|
||
}
|
||
|
||
/**
|
||
*
|
||
* @see http://tools.ietf.org/html/rfc2047
|
||
*/
|
||
function encodedWord($text, $encoding = 'B')
|
||
{
|
||
return "=?{$this->encoding}?$encoding?".base64_encode($text)."?=";
|
||
}
|
||
|
||
/**
|
||
* Тело сообщения
|
||
*/
|
||
function getMessage()
|
||
{
|
||
$message = "--".$this->uniqid . PHP_EOL;
|
||
$message .= $this->mimeTag("Content-Type", $this->type, array ('charset' => $this->encoding));
|
||
$message .= $this->mimeTag("Content-Transfer-Encoding", "8bit");
|
||
$message .= PHP_EOL . $this->content . PHP_EOL . PHP_EOL;
|
||
|
||
/*
|
||
* Вложения
|
||
* http://tools.ietf.org/html/rfc2046#section-5.1.3
|
||
*/
|
||
foreach ($this->attachment as $value) {
|
||
list($data, $name) = $value;
|
||
$message .= "--" . $this->uniqid . PHP_EOL;
|
||
$message .= $this->mimeTag("Content-Type", "application/octet-stream", array ('name' => basename($name)));
|
||
$message .= $this->mimeTag("Content-Transfer-Encoding", "base64");
|
||
$message .= $this->mimeTag("Content-Disposition", "attachment", array ('filename' => basename($name)));
|
||
$message .= PHP_EOL . chunk_split(base64_encode($data)) . PHP_EOL;
|
||
}
|
||
|
||
return $message;
|
||
}
|
||
|
||
/**
|
||
* Заголовок сообщения
|
||
*/
|
||
function getHeader()
|
||
{
|
||
$head = $this->mimeTag("MIME-Version", "1.0");
|
||
$head .= $this->mimeTag("From", $this->_from);
|
||
$head .= $this->mimeTag("X-Mailer", "CMS Tool");
|
||
$head .= $this->mimeTag("Reply-To", $this->_from);
|
||
if (is_string($this->_notify)) {
|
||
$head .= $this->mimeTag("Disposition-Notification-To", "\"" . $this->_notify . "\" <" . $this->_from . ">");
|
||
}
|
||
$head .= $this->mimeTag("Content-Type", "multipart/mixed", array ("boundary" => $this->uniqid));
|
||
if ($this->copy) {
|
||
$head .= $this->mimeTag("BCC", $this->copy);
|
||
}
|
||
$head .= PHP_EOL;
|
||
return $head;
|
||
}
|
||
|
||
function getSubject()
|
||
{
|
||
return $this->encodedWord($this->_subject);
|
||
}
|
||
|
||
/**
|
||
* Вывод строки для сохранения в формате .eml
|
||
*/
|
||
function eml()
|
||
{
|
||
return "To: " . $this->_to . PHP_EOL . "Subject: {$this->getSubject()}" . PHP_EOL . $this->getHeader() . $this->getMessage();
|
||
}
|
||
|
||
/**
|
||
* Отправка почты
|
||
*/
|
||
function send()
|
||
{
|
||
$result = mail($this->_to, $this->getSubject(), $this->getMessage(), $this->getHeader());
|
||
if(! $result) {
|
||
file_put_contents(Path::resolveFile("send.eml"), $this->eml());
|
||
throw new Exception('Невозможно отправить почту');
|
||
}
|
||
return $result;
|
||
}
|
||
|
||
function clear() {
|
||
foreach ($this->attachment as $key => &$value) {
|
||
unset($this->attachment[$key]);
|
||
}
|
||
$this->attachment = array();
|
||
}
|
||
}
|