1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087 |
- <?php
- class SMTP {
-
- public $SMTP_PORT = 25;
-
- public $CRLF = "\r\n";
-
- public $do_debug = 0;
-
- public $Debugoutput = 'echo';
-
- public $do_verp = false;
-
- public $Timeout = 15;
-
- public $Timelimit = 30;
-
- public $Version = '5.2.4';
-
-
-
-
- private $smtp_conn;
-
- private $error;
-
- private $helo_rply;
-
- private function edebug($str) {
- if ($this->Debugoutput == 'error_log') {
- error_log($str);
- } else {
- echo $str;
- }
- }
-
- public function __construct() {
- $this->smtp_conn = 0;
- $this->error = null;
- $this->helo_rply = null;
- $this->do_debug = 0;
- }
-
-
-
-
- public function Connect($host, $port = 0, $tval = 30) {
-
- $this->error = null;
-
- if($this->connected()) {
-
- $this->error = array('error' => 'Already connected to a server');
- return false;
- }
- if(empty($port)) {
- $port = $this->SMTP_PORT;
- }
-
- $this->smtp_conn = @fsockopen($host,
- $port,
- $errno,
- $errstr,
- $tval);
-
- if(empty($this->smtp_conn)) {
- $this->error = array('error' => 'Failed to connect to server',
- 'errno' => $errno,
- 'errstr' => $errstr);
- if($this->do_debug >= 1) {
- $this->edebug('SMTP -> ERROR: ' . $this->error['error'] . ": $errstr ($errno)" . $this->CRLF . '<br />');
- }
- return false;
- }
-
-
- if(substr(PHP_OS, 0, 3) != 'WIN') {
- $max = ini_get('max_execution_time');
- if ($max != 0 && $tval > $max) {
- @set_time_limit($tval);
- }
- stream_set_timeout($this->smtp_conn, $tval, 0);
- }
-
- $announce = $this->get_lines();
- if($this->do_debug >= 2) {
- $this->edebug('SMTP -> FROM SERVER:' . $announce . $this->CRLF . '<br />');
- }
- return true;
- }
-
- public function StartTLS() {
- $this->error = null;
- if(!$this->connected()) {
- $this->error = array('error' => 'Called StartTLS() without being connected');
- return false;
- }
- $this->client_send('STARTTLS' . $this->CRLF);
- $rply = $this->get_lines();
- $code = substr($rply, 0, 3);
- if($this->do_debug >= 2) {
- $this->edebug('SMTP -> FROM SERVER:' . $rply . $this->CRLF . '<br />');
- }
- if($code != 220) {
- $this->error =
- array('error' => 'STARTTLS not accepted from server',
- 'smtp_code' => $code,
- 'smtp_msg' => substr($rply, 4));
- if($this->do_debug >= 1) {
- $this->edebug('SMTP -> ERROR: ' . $this->error['error'] . ': ' . $rply . $this->CRLF . '<br />');
- }
- return false;
- }
-
- if(!stream_socket_enable_crypto($this->smtp_conn, true, STREAM_CRYPTO_METHOD_TLS_CLIENT)) {
- return false;
- }
- return true;
- }
-
- public function Authenticate($username, $password, $authtype='LOGIN', $realm='', $workstation='') {
- if (empty($authtype)) {
- $authtype = 'LOGIN';
- }
- switch ($authtype) {
- case 'PLAIN':
-
- $this->client_send('AUTH PLAIN' . $this->CRLF);
- $rply = $this->get_lines();
- $code = substr($rply, 0, 3);
- if($code != 334) {
- $this->error =
- array('error' => 'AUTH not accepted from server',
- 'smtp_code' => $code,
- 'smtp_msg' => substr($rply, 4));
- if($this->do_debug >= 1) {
- $this->edebug('SMTP -> ERROR: ' . $this->error['error'] . ': ' . $rply . $this->CRLF . '<br />');
- }
- return false;
- }
-
- $this->client_send(base64_encode("\0".$username."\0".$password) . $this->CRLF);
- $rply = $this->get_lines();
- $code = substr($rply, 0, 3);
- if($code != 235) {
- $this->error =
- array('error' => 'Authentication not accepted from server',
- 'smtp_code' => $code,
- 'smtp_msg' => substr($rply, 4));
- if($this->do_debug >= 1) {
- $this->edebug('SMTP -> ERROR: ' . $this->error['error'] . ': ' . $rply . $this->CRLF . '<br />');
- }
- return false;
- }
- break;
- case 'LOGIN':
-
- $this->client_send('AUTH LOGIN' . $this->CRLF);
- $rply = $this->get_lines();
- $code = substr($rply, 0, 3);
- if($code != 334) {
- $this->error =
- array('error' => 'AUTH not accepted from server',
- 'smtp_code' => $code,
- 'smtp_msg' => substr($rply, 4));
- if($this->do_debug >= 1) {
- $this->edebug('SMTP -> ERROR: ' . $this->error['error'] . ': ' . $rply . $this->CRLF . '<br />');
- }
- return false;
- }
-
- $this->client_send(base64_encode($username) . $this->CRLF);
- $rply = $this->get_lines();
- $code = substr($rply, 0, 3);
- if($code != 334) {
- $this->error =
- array('error' => 'Username not accepted from server',
- 'smtp_code' => $code,
- 'smtp_msg' => substr($rply, 4));
- if($this->do_debug >= 1) {
- $this->edebug('SMTP -> ERROR: ' . $this->error['error'] . ': ' . $rply . $this->CRLF . '<br />');
- }
- return false;
- }
-
- $this->client_send(base64_encode($password) . $this->CRLF);
- $rply = $this->get_lines();
- $code = substr($rply, 0, 3);
- if($code != 235) {
- $this->error =
- array('error' => 'Password not accepted from server',
- 'smtp_code' => $code,
- 'smtp_msg' => substr($rply, 4));
- if($this->do_debug >= 1) {
- $this->edebug('SMTP -> ERROR: ' . $this->error['error'] . ': ' . $rply . $this->CRLF . '<br />');
- }
- return false;
- }
- break;
- case 'NTLM':
-
- require_once 'extras/ntlm_sasl_client.php';
- $temp = new stdClass();
- $ntlm_client = new ntlm_sasl_client_class;
- if(! $ntlm_client->Initialize($temp)){
- $this->error = array('error' => $temp->error);
- if($this->do_debug >= 1) {
- $this->edebug('You need to enable some modules in your php.ini file: ' . $this->error['error'] . $this->CRLF);
- }
- return false;
- }
- $msg1 = $ntlm_client->TypeMsg1($realm, $workstation);
- $this->client_send('AUTH NTLM ' . base64_encode($msg1) . $this->CRLF);
- $rply = $this->get_lines();
- $code = substr($rply, 0, 3);
- if($code != 334) {
- $this->error =
- array('error' => 'AUTH not accepted from server',
- 'smtp_code' => $code,
- 'smtp_msg' => substr($rply, 4));
- if($this->do_debug >= 1) {
- $this->edebug('SMTP -> ERROR: ' . $this->error['error'] . ': ' . $rply . $this->CRLF);
- }
- return false;
- }
- $challenge = substr($rply, 3);
- $challenge = base64_decode($challenge);
- $ntlm_res = $ntlm_client->NTLMResponse(substr($challenge, 24, 8), $password);
- $msg3 = $ntlm_client->TypeMsg3($ntlm_res, $username, $realm, $workstation);
-
- $this->client_send(base64_encode($msg3) . $this->CRLF);
- $rply = $this->get_lines();
- $code = substr($rply, 0, 3);
- if($code != 235) {
- $this->error =
- array('error' => 'Could not authenticate',
- 'smtp_code' => $code,
- 'smtp_msg' => substr($rply, 4));
- if($this->do_debug >= 1) {
- $this->edebug('SMTP -> ERROR: ' . $this->error['error'] . ': ' . $rply . $this->CRLF);
- }
- return false;
- }
- break;
- case 'CRAM-MD5':
-
- $this->client_send('AUTH CRAM-MD5' . $this->CRLF);
- $rply = $this->get_lines();
- $code = substr($rply, 0, 3);
- if($code != 334) {
- $this->error =
- array('error' => 'AUTH not accepted from server',
- 'smtp_code' => $code,
- 'smtp_msg' => substr($rply, 4));
- if($this->do_debug >= 1) {
- $this->edebug('SMTP -> ERROR: ' . $this->error['error'] . ': ' . $rply . $this->CRLF . '<br />');
- }
- return false;
- }
-
- $challenge = base64_decode(substr($rply, 4));
-
- $response = $username . ' ' . $this->hmac($challenge, $password);
-
- $this->client_send(base64_encode($response) . $this->CRLF);
- $rply = $this->get_lines();
- $code = substr($rply, 0, 3);
- if($code != 334) {
- $this->error =
- array('error' => 'Credentials not accepted from server',
- 'smtp_code' => $code,
- 'smtp_msg' => substr($rply, 4));
- if($this->do_debug >= 1) {
- $this->edebug('SMTP -> ERROR: ' . $this->error['error'] . ': ' . $rply . $this->CRLF . '<br />');
- }
- return false;
- }
- break;
- }
- return true;
- }
-
- private function hmac($data, $key) {
- if (function_exists('hash_hmac')) {
- return hash_hmac('md5', $data, $key);
- }
-
-
-
-
-
- $b = 64;
- if (strlen($key) > $b) {
- $key = pack('H*', md5($key));
- }
- $key = str_pad($key, $b, chr(0x00));
- $ipad = str_pad('', $b, chr(0x36));
- $opad = str_pad('', $b, chr(0x5c));
- $k_ipad = $key ^ $ipad ;
- $k_opad = $key ^ $opad;
- return md5($k_opad . pack('H*', md5($k_ipad . $data)));
- }
-
- public function Connected() {
- if(!empty($this->smtp_conn)) {
- $sock_status = stream_get_meta_data($this->smtp_conn);
- if($sock_status['eof']) {
-
- if($this->do_debug >= 1) {
- $this->edebug('SMTP -> NOTICE:' . $this->CRLF . 'EOF caught while checking if connected');
- }
- $this->Close();
- return false;
- }
- return true;
- }
- return false;
- }
-
- public function Close() {
- $this->error = null;
- $this->helo_rply = null;
- if(!empty($this->smtp_conn)) {
-
- fclose($this->smtp_conn);
- $this->smtp_conn = 0;
- }
- }
-
-
-
-
- public function Data($msg_data) {
- $this->error = null;
- if(!$this->connected()) {
- $this->error = array(
- 'error' => 'Called Data() without being connected');
- return false;
- }
- $this->client_send('DATA' . $this->CRLF);
- $rply = $this->get_lines();
- $code = substr($rply, 0, 3);
- if($this->do_debug >= 2) {
- $this->edebug('SMTP -> FROM SERVER:' . $rply . $this->CRLF . '<br />');
- }
- if($code != 354) {
- $this->error =
- array('error' => 'DATA command not accepted from server',
- 'smtp_code' => $code,
- 'smtp_msg' => substr($rply, 4));
- if($this->do_debug >= 1) {
- $this->edebug('SMTP -> ERROR: ' . $this->error['error'] . ': ' . $rply . $this->CRLF . '<br />');
- }
- return false;
- }
-
-
- $msg_data = str_replace("\r\n", "\n", $msg_data);
- $msg_data = str_replace("\r", "\n", $msg_data);
- $lines = explode("\n", $msg_data);
-
- $field = substr($lines[0], 0, strpos($lines[0], ':'));
- $in_headers = false;
- if(!empty($field) && !strstr($field, ' ')) {
- $in_headers = true;
- }
- $max_line_length = 998;
- while(list(, $line) = @each($lines)) {
- $lines_out = null;
- if($line == '' && $in_headers) {
- $in_headers = false;
- }
-
- while(strlen($line) > $max_line_length) {
- $pos = strrpos(substr($line, 0, $max_line_length), ' ');
-
- if(!$pos) {
- $pos = $max_line_length - 1;
- $lines_out[] = substr($line, 0, $pos);
- $line = substr($line, $pos);
- } else {
- $lines_out[] = substr($line, 0, $pos);
- $line = substr($line, $pos + 1);
- }
-
- if($in_headers) {
- $line = "\t" . $line;
- }
- }
- $lines_out[] = $line;
-
- while(list(, $line_out) = @each($lines_out)) {
- if(strlen($line_out) > 0)
- {
- if(substr($line_out, 0, 1) == '.') {
- $line_out = '.' . $line_out;
- }
- }
- $this->client_send($line_out . $this->CRLF);
- }
- }
-
- $this->client_send($this->CRLF . '.' . $this->CRLF);
- $rply = $this->get_lines();
- $code = substr($rply, 0, 3);
- if($this->do_debug >= 2) {
- $this->edebug('SMTP -> FROM SERVER:' . $rply . $this->CRLF . '<br />');
- }
- if($code != 250) {
- $this->error =
- array('error' => 'DATA not accepted from server',
- 'smtp_code' => $code,
- 'smtp_msg' => substr($rply, 4));
- if($this->do_debug >= 1) {
- $this->edebug('SMTP -> ERROR: ' . $this->error['error'] . ': ' . $rply . $this->CRLF . '<br />');
- }
- return false;
- }
- return true;
- }
-
- public function Hello($host = '') {
- $this->error = null;
- if(!$this->connected()) {
- $this->error = array(
- 'error' => 'Called Hello() without being connected');
- return false;
- }
-
- if(empty($host)) {
-
- $host = 'localhost';
- }
-
- if(!$this->SendHello('EHLO', $host)) {
- if(!$this->SendHello('HELO', $host)) {
- return false;
- }
- }
- return true;
- }
-
- private function SendHello($hello, $host) {
- $this->client_send($hello . ' ' . $host . $this->CRLF);
- $rply = $this->get_lines();
- $code = substr($rply, 0, 3);
- if($this->do_debug >= 2) {
- $this->edebug('SMTP -> FROM SERVER: ' . $rply . $this->CRLF . '<br />');
- }
- if($code != 250) {
- $this->error =
- array('error' => $hello . ' not accepted from server',
- 'smtp_code' => $code,
- 'smtp_msg' => substr($rply, 4));
- if($this->do_debug >= 1) {
- $this->edebug('SMTP -> ERROR: ' . $this->error['error'] . ': ' . $rply . $this->CRLF . '<br />');
- }
- return false;
- }
- $this->helo_rply = $rply;
- return true;
- }
-
- public function Mail($from) {
- $this->error = null;
- if(!$this->connected()) {
- $this->error = array(
- 'error' => 'Called Mail() without being connected');
- return false;
- }
- $useVerp = ($this->do_verp ? ' XVERP' : '');
- $this->client_send('MAIL FROM:<' . $from . '>' . $useVerp . $this->CRLF);
- $rply = $this->get_lines();
- $code = substr($rply, 0, 3);
- if($this->do_debug >= 2) {
- $this->edebug('SMTP -> FROM SERVER:' . $rply . $this->CRLF . '<br />');
- }
- if($code != 250) {
- $this->error =
- array('error' => 'MAIL not accepted from server',
- 'smtp_code' => $code,
- 'smtp_msg' => substr($rply, 4));
- if($this->do_debug >= 1) {
- $this->edebug('SMTP -> ERROR: ' . $this->error['error'] . ': ' . $rply . $this->CRLF . '<br />');
- }
- return false;
- }
- return true;
- }
-
- public function Quit($close_on_error = true) {
- $this->error = null;
- if(!$this->connected()) {
- $this->error = array(
- 'error' => 'Called Quit() without being connected');
- return false;
- }
-
- $this->client_send('quit' . $this->CRLF);
-
- $byemsg = $this->get_lines();
- if($this->do_debug >= 2) {
- $this->edebug('SMTP -> FROM SERVER:' . $byemsg . $this->CRLF . '<br />');
- }
- $rval = true;
- $e = null;
- $code = substr($byemsg, 0, 3);
- if($code != 221) {
-
- $e = array('error' => 'SMTP server rejected quit command',
- 'smtp_code' => $code,
- 'smtp_rply' => substr($byemsg, 4));
- $rval = false;
- if($this->do_debug >= 1) {
- $this->edebug('SMTP -> ERROR: ' . $e['error'] . ': ' . $byemsg . $this->CRLF . '<br />');
- }
- }
- if(empty($e) || $close_on_error) {
- $this->Close();
- }
- return $rval;
- }
-
- public function Recipient($to) {
- $this->error = null;
- if(!$this->connected()) {
- $this->error = array(
- 'error' => 'Called Recipient() without being connected');
- return false;
- }
- $this->client_send('RCPT TO:<' . $to . '>' . $this->CRLF);
- $rply = $this->get_lines();
- $code = substr($rply, 0, 3);
- if($this->do_debug >= 2) {
- $this->edebug('SMTP -> FROM SERVER:' . $rply . $this->CRLF . '<br />');
- }
- if($code != 250 && $code != 251) {
- $this->error =
- array('error' => 'RCPT not accepted from server',
- 'smtp_code' => $code,
- 'smtp_msg' => substr($rply, 4));
- if($this->do_debug >= 1) {
- $this->edebug('SMTP -> ERROR: ' . $this->error['error'] . ': ' . $rply . $this->CRLF . '<br />');
- }
- return false;
- }
- return true;
- }
-
- public function Reset() {
- $this->error = null;
- if(!$this->connected()) {
- $this->error = array('error' => 'Called Reset() without being connected');
- return false;
- }
- $this->client_send('RSET' . $this->CRLF);
- $rply = $this->get_lines();
- $code = substr($rply, 0, 3);
- if($this->do_debug >= 2) {
- $this->edebug('SMTP -> FROM SERVER:' . $rply . $this->CRLF . '<br />');
- }
- if($code != 250) {
- $this->error =
- array('error' => 'RSET failed',
- 'smtp_code' => $code,
- 'smtp_msg' => substr($rply, 4));
- if($this->do_debug >= 1) {
- $this->edebug('SMTP -> ERROR: ' . $this->error['error'] . ': ' . $rply . $this->CRLF . '<br />');
- }
- return false;
- }
- return true;
- }
-
- public function SendAndMail($from) {
- $this->error = null;
- if(!$this->connected()) {
- $this->error = array(
- 'error' => 'Called SendAndMail() without being connected');
- return false;
- }
- $this->client_send('SAML FROM:' . $from . $this->CRLF);
- $rply = $this->get_lines();
- $code = substr($rply, 0, 3);
- if($this->do_debug >= 2) {
- $this->edebug('SMTP -> FROM SERVER:' . $rply . $this->CRLF . '<br />');
- }
- if($code != 250) {
- $this->error =
- array('error' => 'SAML not accepted from server',
- 'smtp_code' => $code,
- 'smtp_msg' => substr($rply, 4));
- if($this->do_debug >= 1) {
- $this->edebug('SMTP -> ERROR: ' . $this->error['error'] . ': ' . $rply . $this->CRLF . '<br />');
- }
- return false;
- }
- return true;
- }
-
- public function Turn() {
- $this->error = array('error' => 'This method, TURN, of the SMTP '.
- 'is not implemented');
- if($this->do_debug >= 1) {
- $this->edebug('SMTP -> NOTICE: ' . $this->error['error'] . $this->CRLF . '<br />');
- }
- return false;
- }
-
- public function client_send($data) {
- if ($this->do_debug >= 1) {
- $this->edebug("CLIENT -> SMTP: $data" . $this->CRLF . '<br />');
- }
- return fwrite($this->smtp_conn, $data);
- }
-
- public function getError() {
- return $this->error;
- }
-
-
-
-
- private function get_lines() {
- $data = '';
- $endtime = 0;
-
- if (!is_resource($this->smtp_conn)) {
- return $data;
- }
- stream_set_timeout($this->smtp_conn, $this->Timeout);
- if ($this->Timelimit > 0) {
- $endtime = time() + $this->Timelimit;
- }
- while(is_resource($this->smtp_conn) && !feof($this->smtp_conn)) {
- $str = @fgets($this->smtp_conn, 515);
- if($this->do_debug >= 4) {
- $this->edebug("SMTP -> get_lines(): \$data was \"$data\"" . $this->CRLF . '<br />');
- $this->edebug("SMTP -> get_lines(): \$str is \"$str\"" . $this->CRLF . '<br />');
- }
- $data .= $str;
- if($this->do_debug >= 4) {
- $this->edebug("SMTP -> get_lines(): \$data is \"$data\"" . $this->CRLF . '<br />');
- }
-
- if(substr($str, 3, 1) == ' ') { break; }
-
- $info = stream_get_meta_data($this->smtp_conn);
- if ($info['timed_out']) {
- if($this->do_debug >= 4) {
- $this->edebug('SMTP -> get_lines(): timed-out (' . $this->Timeout . ' seconds) <br />');
- }
- break;
- }
-
- if ($endtime) {
- if (time() > $endtime) {
- if($this->do_debug >= 4) {
- $this->edebug('SMTP -> get_lines(): timelimit reached (' . $this->Timelimit . ' seconds) <br />');
- }
- break;
- }
- }
- }
- return $data;
- }
- }
|