Файл: incfiles/SendMailSmtpClass.php
Строк: 117
<?php
/**
* SendMailSmtpClass
*
* Класс для отправки писем через SMTP с авторизацией
* Может работать через SSL протокол
* Тестировалось на почтовых серверах yandex.ru, mail.ru и gmail.com
*
* @author Ipatov Evgeniy <admin@ipatov-soft.ru>
* @version 1.0
*/
class SendMailSmtpClass {
/**
*
* @var string $smtp_username - логин
* @var string $smtp_password - пароль
* @var string $smtp_host - хост
* @var string $smtp_from - от кого
* @var integer $smtp_port - порт
* @var string $smtp_charset - кодировка
*
*/
public $smtp_username;
public $smtp_password;
public $smtp_host;
public $smtp_from;
public $smtp_port;
public $smtp_charset;
public function __construct($smtp_username, $smtp_password, $smtp_host, $smtp_from, $smtp_port = 25, $smtp_charset = "utf-8") {
$this->smtp_username = $smtp_username;
$this->smtp_password = $smtp_password;
$this->smtp_host = $smtp_host;
$this->smtp_from = $smtp_from;
$this->smtp_port = $smtp_port;
$this->smtp_charset = $smtp_charset;
}
/**
* Отправка письма
*
* @param string $mailTo - получатель письма
* @param string $subject - тема письма
* @param string $message - тело письма
* @param string $headers - заголовки письма
*
* @return bool|string В случаи отправки вернет true, иначе текст ошибки *
*/
function send($mailTo, $subject, $message, $headers) {
$contentMail = "Date: " . date("D, d M Y H:i:s") . " UTrn";
$contentMail .= 'Subject: =?' . $this->smtp_charset . '?B?' . base64_encode($subject) . "=?=rn";
$contentMail .= $headers . "rn";
$contentMail .= $message . "rn";
try {
if(!$socket = @fsockopen($this->smtp_host, $this->smtp_port, $errorNumber, $errorDescription, 30)){
throw new Exception($errorNumber.".".$errorDescription);
}
if (!$this->_parseServer($socket, "220")){
throw new Exception('Connection error');
}
$server_name = $_SERVER["SERVER_NAME"];
fputs($socket, "HELO $server_namern");
if (!$this->_parseServer($socket, "250")) {
fclose($socket);
throw new Exception('Error of command sending: HELO');
}
fputs($socket, "AUTH LOGINrn");
if (!$this->_parseServer($socket, "334")) {
fclose($socket);
throw new Exception('Autorization error');
}
fputs($socket, base64_encode($this->smtp_username) . "rn");
if (!$this->_parseServer($socket, "334")) {
fclose($socket);
throw new Exception('Autorization error');
}
fputs($socket, base64_encode($this->smtp_password) . "rn");
if (!$this->_parseServer($socket, "235")) {
fclose($socket);
throw new Exception('Autorization error');
}
fputs($socket, "MAIL FROM: <".$this->smtp_username.">rn");
if (!$this->_parseServer($socket, "250")) {
fclose($socket);
throw new Exception('Error of command sending: MAIL FROM');
}
$mailTo = ltrim($mailTo, '<');
$mailTo = rtrim($mailTo, '>');
fputs($socket, "RCPT TO: <" . $mailTo . ">rn");
if (!$this->_parseServer($socket, "250")) {
fclose($socket);
throw new Exception('Error of command sending: RCPT TO');
}
fputs($socket, "DATArn");
if (!$this->_parseServer($socket, "354")) {
fclose($socket);
throw new Exception('Error of command sending: DATA');
}
fputs($socket, $contentMail."rn.rn");
if (!$this->_parseServer($socket, "250")) {
fclose($socket);
throw new Exception("E-mail didn't sent");
}
fputs($socket, "QUITrn");
fclose($socket);
} catch (Exception $e) {
return $e->getMessage();
}
return true;
}
private function _parseServer($socket, $response) {
while (@substr($responseServer, 3, 1) != ' ') {
if (!($responseServer = fgets($socket, 256))) {
return false;
}
}
if (!(substr($responseServer, 0, 3) == $response)) {
return false;
}
return true;
}
}