210 lines
5.1 KiB
PHP
210 lines
5.1 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= false;
|
|
|
|
protected $attachment = array ();
|
|
protected $uniqid;
|
|
|
|
function __construct() {
|
|
$this->setEncoding("windows-1251");
|
|
$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 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", "text/plain", 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());
|
|
// $result = false;
|
|
if(! $result) {
|
|
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();
|
|
}
|
|
}
|