<?php
class Mail
{
private $charset = "UTF-8";
private $boundary = "";
private $SubBody = array();
private $body = array();
private $ctencoding = "base64";
private $count_body = 1;
private $checkAddress = true;
private $headers = array();
private $ready_headers = array();
private $names_email = array();
private $receipt = 0;
private $smtpsendto = array();
private $sendto = array();
private $acc = array();
private $abcc = array();
private $smtp = array();
private $smtp_log = '';
private $log_on = false;
private $body_header = array();
public $status_mail = array('status' => true, "message" => 'ок');
public function __construct($charset = "", $ctencoding = '')
{
$this->boundary = md5(uniqid("myboundary"));
$this->smtp['on'] = false;
if (strlen($ctencoding) and $ctencoding == '8bit')
{
$this->ctencoding = '8bit';
}
if (strlen($charset))
{
$this->charset = strtolower($charset);
if ($this->charset == "us-ascii")
{
$this->ctencoding = "7bit";
}
}
}
public function Body($text, $text_html = "", $alternative_text = '', $resource = 'webi')
{
if (!strlen($resource))
$resource = 'webi';
if ($text_html == "html")
$text_html = "text/html";
else
$text_html = "text/plain";
if ($this->ctencoding == 'base64')
{
if (strlen($alternative_text))
$alternative_text = chunk_split(base64_encode($alternative_text));
if (strlen($text))
$text = chunk_split(base64_encode($text));
}
if (!strlen($alternative_text))
{
$body = "Content-Type: ".$text_html."; charset=".$this->charset."\r\n";
$body.="Content-Transfer-Encoding: ".$this->ctencoding."\r\n\r\n";
$body.=$text;
}
elseif (strlen($alternative_text) and $text_html == 'text/html')
{
$body = "Content-Type: multipart/alternative; boundary=ALT-".$this->boundary."\r\n\r\n";
$body.="--ALT-".$this->boundary."\r\n";
$body.="Content-Type: text/plain; charset=".$this->charset."\r\n";
$body.="Content-Transfer-Encoding: ".$this->ctencoding."\r\n\r\n";
$body.=$alternative_text."\r\n";
$body.="--ALT-".$this->boundary."\r\n";
$body.="Content-Type: text/html; charset=".$this->charset."\r\n";
$body.="Content-Transfer-Encoding: ".$this->ctencoding."\r\n\r\n";
$body.=$text."\r\n";
$body.="--ALT-".$this->boundary."--";
}
$this->SubBody[$resource]['body'][0] = $body;
}
protected function mime_content_type($file)
{
$ext = strtolower(substr(strrchr(basename($file), '.'), 1));
switch ($ext)
{
case 'jpg': return 'image/jpeg';
case 'jpeg': return 'image/jpeg';
case 'gif': return 'image/gif';
case 'png': return 'image/png';
case 'ico': return 'image/x-icon';
case 'txt': return 'text/plain';
default: return 'application/octet-stream';
}
}
public function Attach($filename, $new_name_filename = "", $filetype = "", $disposition = "", $resource = 'webi')
{
if (!strlen($resource))
$resource = 'webi';
if (!file_exists($filename))
{
return FALSE;
}
if (strlen($new_name_filename))
$basename = basename($new_name_filename);
else
$basename = basename($filename);
$charset_name = "=?".$this->charset."?B?".base64_encode($basename)."?=";
if (!strlen($filetype))
$filetype = $this->mime_content_type($basename);
$body = "Content-Type: ".$filetype."; name=\"$charset_name\"\r\n";
$body.="Content-Transfer-Encoding: base64\r\n";
if ($disposition == 'attachment')
{
$body.="Content-Disposition: attachment; filename=\"$charset_name\"\r\n";
}
$body.="Content-ID: <".$basename.">\r\n";
$body.="\r\n";
$body.=chunk_split(base64_encode(file_get_contents($filename)));
if ($disposition == 'attachment')
$this->SubBody[$resource]['mixed'][] = $body;
else
{
$this->SubBody[$resource]['body'][$this->count_body] = $body;
$this->count_body++;
}
}
public function BuildMail($resource = 'webi')
{
if (!strlen($resource))
$resource = 'webi';
$this->ready_headers[$resource] = '';
if (isset($this->SubBody[$resource]['body']))
$resource_body = $resource;
else
$resource_body = 'webi';
if (!is_array($this->sendto[$resource]) OR !count($this->sendto[$resource]))
{
$this->status_mail['status'] = false;
$this->status_mail['message'] = "Error : no recipient selected for ".$resource;
// return false;
}
if (!isset($this->body[$resource_body]))
{
if (count($this->SubBody[$resource_body]['body']) > 1)
{
$body = implode("\r\n--REL-".$this->boundary."\r\n", $this->SubBody[$resource_body]['body']);
$body = "Content-Type: multipart/related; boundary=REL-".$this->boundary."\r\n\r\n"
.'--REL-'.$this->boundary."\r\n".$body.'--REL-'.$this->boundary."--";
}
else
{
$body = $this->SubBody[$resource_body]['body'][0];
}
if (isset($this->SubBody[$resource_body]['mixed']) AND count($this->SubBody[$resource_body]['mixed']))
{
$bodymix = implode('--MIX-'.$this->boundary."\r\n", $this->SubBody[$resource_body]['mixed']);
$body = $body."\r\n--MIX-".$this->boundary."\r\n".$bodymix;
$body = "Content-Type: multipart/mixed; boundary=MIX-".$this->boundary."\r\n\r\n"
.'--MIX-'.$this->boundary."\r\n".$body.'--MIX-'.$this->boundary."--";
}
unset($this->SubBody[$resource_body]);
$temp_mass = explode("\r\n\r\n", $body);
$this->body_header[$resource_body] = $temp_mass[0];
unset($temp_mass[0]);
$this->body[$resource_body] = implode("\r\n\r\n", $temp_mass);
unset($temp_mass);
unset($body);
}
$temp_mass = array();
foreach ($this->sendto[$resource] as $key => $value)
{
if (strlen($this->names_email[$resource]['To'][$value]))
$temp_mass[] = "=?".$this->charset."?Q?".str_replace("+", "_", str_replace("%", "=", urlencode(strtr($this->names_email[$resource]['To'][$value], "\r\n", " "))))."?= <".$value.">";
else
$temp_mass[] = $value;
}
$this->headers[$resource]['To'] = implode(", ", $temp_mass);
if (isset($this->acc[$resource]) and count($this->acc[$resource]) > 0)
$this->headers[$resource]['CC'] = implode(", ", $this->acc[$resource]);
if (isset($this->abcc[$resource]) and count($this->abcc[$resource]) > 0)
$this->headers[$resource]['BCC'] = implode(", ", $this->abcc[$resource]);
if ($this->receipt)
{
if (isset($this->headers["Reply-To"]))
$this->headers["Disposition-Notification-To"] = $this->headers["Reply-To"];
else
$this->headers["Disposition-Notification-To"] = $this->headers['From'];
}
if ($this->charset != "")
{
$this->headers["Mime-Version"] = "1.0";
}
$this->headers["X-Mailer"] = "Php_libMail_v_2.0(webi.ru)";
if (!isset($this->headers[$resource]['Subject']) and isset($this->headers['webi']['Su