2023-03-04 23:45:46 +00:00
|
|
|
<?php
|
2023-05-05 01:17:19 +00:00
|
|
|
if (!DEFINED('EGP'))
|
|
|
|
exit(header('Refresh: 0; URL=http://' . $_SERVER['SERVER_NAME'] . '/404'));
|
|
|
|
|
|
|
|
class smtp
|
|
|
|
{
|
|
|
|
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')
|
2023-03-04 23:45:46 +00:00
|
|
|
{
|
2023-05-05 01:17:19 +00:00
|
|
|
$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;
|
|
|
|
}
|
2023-03-04 23:45:46 +00:00
|
|
|
|
2023-05-05 01:17:19 +00:00
|
|
|
function send($mailTo, $subject, $message, $headers)
|
|
|
|
{
|
|
|
|
global $cfg;
|
2023-03-04 23:45:46 +00:00
|
|
|
|
2023-05-05 01:17:19 +00:00
|
|
|
$contentMail = 'Date: ' . date('D, d M Y H:i:s') . " UT\r\n";
|
|
|
|
$contentMail .= 'Subject: =?' . $this->smtp_charset . '?B?' . base64_encode($subject) . "=?=\r\n";
|
|
|
|
$contentMail .= $headers . "\r\n";
|
|
|
|
$contentMail .= $message . "\r\n";
|
2023-03-04 23:45:46 +00:00
|
|
|
|
2023-05-05 01:17:19 +00:00
|
|
|
try {
|
|
|
|
if (!$socket = @fsockopen($this->smtp_host, $this->smtp_port, $errorNumber, $errorDescription, 30))
|
|
|
|
throw new Exception($errorNumber . '.' . $errorDescription);
|
2023-03-04 23:45:46 +00:00
|
|
|
|
2023-05-05 01:17:19 +00:00
|
|
|
if (!$this->_parseServer($socket, '220'))
|
|
|
|
throw new Exception('Connection error');
|
2023-03-04 23:45:46 +00:00
|
|
|
|
2023-05-05 01:17:19 +00:00
|
|
|
$server_name = $cfg['url'];
|
2023-03-04 23:45:46 +00:00
|
|
|
|
2023-05-05 01:17:19 +00:00
|
|
|
fputs($socket, 'HELO ' . $server_name . "\r\n");
|
2023-03-04 23:45:46 +00:00
|
|
|
|
2023-05-05 01:17:19 +00:00
|
|
|
if (!$this->_parseServer($socket, '250')) {
|
|
|
|
fclose($socket);
|
|
|
|
throw new Exception('Error of command sending: HELO');
|
|
|
|
}
|
2023-03-04 23:45:46 +00:00
|
|
|
|
2023-05-05 01:17:19 +00:00
|
|
|
fputs($socket, 'AUTH LOGIN' . "\r\n");
|
2023-03-04 23:45:46 +00:00
|
|
|
|
2023-05-05 01:17:19 +00:00
|
|
|
if (!$this->_parseServer($socket, '334')) {
|
|
|
|
fclose($socket);
|
|
|
|
throw new Exception('Autorization error');
|
|
|
|
}
|
2023-03-04 23:45:46 +00:00
|
|
|
|
2023-05-05 01:17:19 +00:00
|
|
|
fputs($socket, base64_encode($this->smtp_username) . "\r\n");
|
2023-03-04 23:45:46 +00:00
|
|
|
|
2023-05-05 01:17:19 +00:00
|
|
|
if (!$this->_parseServer($socket, '334')) {
|
|
|
|
fclose($socket);
|
|
|
|
throw new Exception('Autorization error');
|
|
|
|
}
|
2023-03-04 23:45:46 +00:00
|
|
|
|
2023-05-05 01:17:19 +00:00
|
|
|
fputs($socket, base64_encode($this->smtp_password) . "\r\n");
|
2023-03-04 23:45:46 +00:00
|
|
|
|
2023-05-05 01:17:19 +00:00
|
|
|
if (!$this->_parseServer($socket, '235')) {
|
|
|
|
fclose($socket);
|
|
|
|
throw new Exception('Autorization error');
|
|
|
|
}
|
2023-03-04 23:45:46 +00:00
|
|
|
|
2023-05-05 01:17:19 +00:00
|
|
|
fputs($socket, 'MAIL FROM: <' . $this->smtp_username . ">\r\n");
|
2023-03-04 23:45:46 +00:00
|
|
|
|
2023-05-05 01:17:19 +00:00
|
|
|
if (!$this->_parseServer($socket, '250')) {
|
|
|
|
fclose($socket);
|
|
|
|
throw new Exception('Error of command sending: MAIL FROM');
|
|
|
|
}
|
2023-03-04 23:45:46 +00:00
|
|
|
|
2023-05-05 01:17:19 +00:00
|
|
|
$mailTo = ltrim($mailTo, '<');
|
|
|
|
$mailTo = rtrim($mailTo, '>');
|
2023-03-04 23:45:46 +00:00
|
|
|
|
2023-05-05 01:17:19 +00:00
|
|
|
fputs($socket, 'RCPT TO: <' . $mailTo . ">\r\n");
|
2023-03-04 23:45:46 +00:00
|
|
|
|
2023-05-05 01:17:19 +00:00
|
|
|
if (!$this->_parseServer($socket, '250')) {
|
|
|
|
fclose($socket);
|
|
|
|
throw new Exception('Error of command sending: RCPT TO');
|
|
|
|
}
|
2023-03-04 23:45:46 +00:00
|
|
|
|
2023-05-05 01:17:19 +00:00
|
|
|
fputs($socket, 'DATA' . "\r\n");
|
2023-03-04 23:45:46 +00:00
|
|
|
|
2023-05-05 01:17:19 +00:00
|
|
|
if (!$this->_parseServer($socket, "354")) {
|
2023-03-04 23:45:46 +00:00
|
|
|
fclose($socket);
|
2023-05-05 01:17:19 +00:00
|
|
|
throw new Exception('Error of command sending: DATA');
|
2023-03-04 23:45:46 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 01:17:19 +00:00
|
|
|
fputs($socket, $contentMail . "\r\n.\r\n");
|
|
|
|
|
|
|
|
if (!$this->_parseServer($socket, '250')) {
|
|
|
|
fclose($socket);
|
|
|
|
throw new Exception('E-mail didn\'t sent');
|
2023-03-04 23:45:46 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 01:17:19 +00:00
|
|
|
fputs($socket, 'QUIT' . "\r\n");
|
|
|
|
fclose($socket);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
return $e->getMessage();
|
2023-03-04 23:45:46 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 01:17:19 +00:00
|
|
|
return true;
|
|
|
|
}
|
2023-03-04 23:45:46 +00:00
|
|
|
|
2023-05-05 01:17:19 +00:00
|
|
|
private function _parseServer($socket, $response)
|
|
|
|
{
|
|
|
|
while (@substr($responseServer, 3, 1) != ' ')
|
|
|
|
if (!($responseServer = fgets($socket, 256)))
|
2023-03-04 23:45:46 +00:00
|
|
|
return false;
|
|
|
|
|
2023-05-05 01:17:19 +00:00
|
|
|
if (!(substr($responseServer, 0, 3) == $response))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2023-03-04 23:45:46 +00:00
|
|
|
}
|
2023-05-05 01:17:19 +00:00
|
|
|
}
|
|
|
|
|
2023-03-04 23:45:46 +00:00
|
|
|
?>
|