setEncoding("UTF-8"); $this->uniqid = strtoupper(uniqid(time())); // Идентефикатор разделителя // $this->mime } /** * Установка отправителя */ 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 setType($type) { $this->type = $type; } /** * Кодировка текста в письме */ function setEncoding($encoding) { $this->encoding = $encoding; } /** * Добавление вложения из файла */ function addAttachment($filename, $name = false) { assert(is_string($filename)); if(file_exists($filename)) { // assert ?? $file = fopen($filename, "rb"); $data = fread($file, filesize($filename)); $this->attachment [] = ($name) ? array($data, $name) : array($data, basename($filename)); } } /** * Добавление вложения из строки с указанием имени файла */ function addAttachmentRaw($data, $name) { assert(is_string($name)); $this->attachment [] = array($data, $name); } function quote($var, $val) { return ";" . PHP_EOL . "\t" . $var . "=\"" . $val . "\""; } /** * Общий формат тегов MIME * 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; } /** * * 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' => $name)); $message .= $this->mimeTag("Content-Transfer-Encoding", "base64"); $message .= $this->mimeTag("Content-Disposition", "attachment", array ('filename' => $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", "CIS Tool"); $head .= $this->mimeTag("Reply-To", $this->from); if ($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) { throw new Exception('Невозможно отправить почту'); // require_once "core/path.php"; // file_put_contents(Path::resolveFile("data/email/send.eml"), $this->eml()); } return $result; } function clear() { foreach ($this->attachment as $key => &$value) { unset($this->attachment[$key]); } $this->attachment = array(); } }