cvs: php4 /pear/Net SMTP.php

From: Date: Fri, 07 Jun 2002 06:04:11 +0000
Subject: cvs: php4 /pear/Net SMTP.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-4460@lists.php.net to get a copy of this message
cox Fri Jun 7 02:04:11 2002 EDT Modified files: /php4/pear/Net SMTP.php Log: ws

Index: php4/pear/Net/SMTP.php diff -u php4/pear/Net/SMTP.php:1.7 php4/pear/Net/SMTP.php:1.8 --- php4/pear/Net/SMTP.php:1.7 Thu Feb 28 03:27:15 2002 +++ php4/pear/Net/SMTP.php Fri Jun 7 02:04:11 2002 @@ -23,49 +23,49 @@ * Net_Socket:: class. */ class Net_SMTP extends PEAR { - - /** + + /** * The server to connect to. * @var string */ - var $host = 'localhost'; - - /** + var $host = 'localhost'; + + /** * The port to connect to. * @var int */ - var $port = 25; - - /** + var $port = 25; + + /** * The value to give when sending EHLO or HELO. * @var string */ - var $localhost = 'localhost'; - - /** + var $localhost = 'localhost'; + + /** * The socket resource being used to connect to the SMTP server. * @var resource */ - var $socket; - - /** + var $socket; + + /** * The most recent reply code * @var int */ - var $code; - - /** + var $code; + + /** * Stores detected features of the SMTP server. * @var array */ - var $esmtp; - + var $esmtp; + /** * The last line read from the server. * @var string */ var $lastline; - + /** * Constructor * @@ -76,12 +76,12 @@ * @param int The port to connect to. * @param string The value to give when sending EHLO or HELO. */ - function Net_SMTP($host = null, $port = null, $localhost = null) { + function Net_SMTP($host = null, $port = null, $localhost = null) { if (isset($host)) $this->host = $host; if (isset($port)) $this->port = $port; if (isset($localhost)) $this->localhost = $localhost; - } - + } + /** * Attempt to connect to the SMTP server. * @@ -91,16 +91,16 @@ */ function connect() { include_once 'Net/Socket.php'; - - if (PEAR::isError($this->socket = new Net_Socket())) { return new PEAR_Error('unable to create a socket object'); } - if (PEAR::isError($this->socket->connect($this->host, $this->port))) { return new PEAR_Error('unable to open socket'); } - - if (PEAR::isError($this->validateResponse('220'))) { return new PEAR_Error('smtp server not 220 ready'); } - if (!$this->identifySender()) { return new PEAR_Error('unable to identify smtp server'); } - - return true; - } - + + if (PEAR::isError($this->socket = new Net_Socket())) { return new PEAR_Error('unable to create a socket object'); } + if (PEAR::isError($this->socket->connect($this->host, $this->port))) { return new PEAR_Error('unable to open socket'); } + + if (PEAR::isError($this->validateResponse('220'))) { return new PEAR_Error('smtp server not 220 ready'); } + if (!$this->identifySender()) { return new PEAR_Error('unable to identify smtp server'); } + + return true; + } + /** * Attempt to disconnect from the SMTP server. * @@ -108,14 +108,14 @@ * kind of failure, or true on success. * @access public */ - function disconnect() { - if (PEAR::isError($this->socket->write("QUIT\r\n"))) { return new PEAR_Error('write to socket failed'); } - if (!$this->validateResponse('221')) { return new PEAR_Error('221 Bye not received'); } - if (PEAR::isError($this->socket->disconnect())) { return new PEAR_Error('socket disconnect failed'); } - - return true; - } - + function disconnect() { + if (PEAR::isError($this->socket->write("QUIT\r\n"))) { return new PEAR_Error('write to socket failed'); } + if (!$this->validateResponse('221')) { return new PEAR_Error('221 Bye not received'); } + if (PEAR::isError($this->socket->disconnect())) { return new PEAR_Error('socket disconnect failed'); } + + return true; + } + /** * Attempt to do SMTP authentication. * @@ -126,242 +126,242 @@ * kind of failure, or true on success. * @access public */ - function auth($uid, $pwd) { - /* Note: not currently checking if AUTH LOGIN is allowed */ - /* Note: only allows one authentication mechanism */ - - if (!isset($this->esmtp['AUTH'])) { return new PEAR_Error('auth not supported'); } - - if (PEAR::isError($this->socket->write("AUTH LOGIN\r\n"))) { return new PEAR_Error('write to socket failed'); } - if (!$this->validateResponse('334')) { return new PEAR_Error('AUTH LOGIN not recognized'); } - - if (PEAR::isError($this->socket->write(base64_encode($uid) . "\n"))) { return new PEAR_Error('write to socket failed'); } - if (!$this->validateResponse('334')) { return new PEAR_Error('354 not received'); } - - if (PEAR::isError($this->socket->write(base64_encode($pwd) . "\n"))) { return new PEAR_Error('write to socket failed'); } - if (!$this->validateResponse('235')) { return new PEAR_Error('235 not received'); } - - return true; - } - + function auth($uid, $pwd) { + /* Note: not currently checking if AUTH LOGIN is allowed */ + /* Note: only allows one authentication mechanism */ + + if (!isset($this->esmtp['AUTH'])) { return new PEAR_Error('auth not supported'); } + + if (PEAR::isError($this->socket->write("AUTH LOGIN\r\n"))) { return new PEAR_Error('write to socket failed'); } + if (!$this->validateResponse('334')) { return new PEAR_Error('AUTH LOGIN not recognized'); } + + if (PEAR::isError($this->socket->write(base64_encode($uid) . "\n"))) { return new PEAR_Error('write to socket failed'); } + if (!$this->validateResponse('334')) { return new PEAR_Error('354 not received'); } + + if (PEAR::isError($this->socket->write(base64_encode($pwd) . "\n"))) { return new PEAR_Error('write to socket failed'); } + if (!$this->validateResponse('235')) { return new PEAR_Error('235 not received'); } + + return true; + } + /** * Send the HELO command. - * + * * @param string The domain name to say we are. * * @return mixed Returns a PEAR_Error with an error message on any * kind of failure, or true on success. * @access public */ - function helo($domain) { - if (PEAR::isError($this->socket->write("HELO $domain\r\n"))) { return new PEAR_Error('write to socket failed'); } - if (!($this->validateResponse('250'))) { return new PEAR_Error('250 OK not received'); } - - return true; - } - + function helo($domain) { + if (PEAR::isError($this->socket->write("HELO $domain\r\n"))) { return new PEAR_Error('write to socket failed'); } + if (!($this->validateResponse('250'))) { return new PEAR_Error('250 OK not received'); } + + return true; + } + /** * Send the MAIL FROM: command. - * + * * @param string The sender (reverse path) to set. * * @return mixed Returns a PEAR_Error with an error message on any * kind of failure, or true on success. * @access public */ - function mailFrom($reverse_path) { - if (PEAR::isError($this->socket->write("MAIL FROM:<$reverse_path>\r\n"))) { return new PEAR_Error('write to socket failed'); } - if (!($this->validateResponse('250'))) { return new PEAR_Error('250 OK not received'); } - - return true; - } - + function mailFrom($reverse_path) { + if (PEAR::isError($this->socket->write("MAIL FROM:<$reverse_path>\r\n"))) { return new PEAR_Error('write to socket failed'); } + if (!($this->validateResponse('250'))) { return new PEAR_Error('250 OK not received'); } + + return true; + } + /** * Send the RCPT TO: command. - * + * * @param string The recipient (forward path) to add. * * @return mixed Returns a PEAR_Error with an error message on any * kind of failure, or true on success. * @access public */ - function rcptTo($forward_path) { - /* Note: 251 is also a valid response code */ - - if (PEAR::isError($this->socket->write("RCPT TO: <$forward_path>\r\n"))) { return new PEAR_Error('write to socket failed'); } - if (!($this->validateResponse('250'))) { return new PEAR_Error($this->lastline); } - - return true; - } - + function rcptTo($forward_path) { + /* Note: 251 is also a valid response code */ + + if (PEAR::isError($this->socket->write("RCPT TO: <$forward_path>\r\n"))) { return new PEAR_Error('write to socket failed'); } + if (!($this->validateResponse('250'))) { return new PEAR_Error($this->lastline); } + + return true; + } + /** * Send the DATA command. - * + * * @param string The message body to send. * * @return mixed Returns a PEAR_Error with an error message on any * kind of failure, or true on success. * @access public */ - function data($data) { - $data = preg_replace("/([^\r]{1})\n/", "\\1\r\n", $data); - $data = preg_replace("/\n\n/", "\n\r\n", $data); - $data = preg_replace("/\n\./", "\n..", $data); - - if (PEAR::isError($this->socket->write("DATA\r\n"))) { return new PEAR_Error('write to socket failed'); } - if (!($this->validateResponse('354'))) { return new PEAR_Error('354 not received'); } - if (PEAR::isError($this->socket->write($data . "\r\n.\r\n"))) { return new PEAR_Error('write to socket failed'); } - if (!($this->validateResponse('250'))) { return new PEAR_Error('250 OK not received'); } - - return true; - } - + function data($data) { + $data = preg_replace("/([^\r]{1})\n/", "\\1\r\n", $data); + $data = preg_replace("/\n\n/", "\n\r\n", $data); + $data = preg_replace("/\n\./", "\n..", $data); + + if (PEAR::isError($this->socket->write("DATA\r\n"))) { return new PEAR_Error('write to socket failed'); } + if (!($this->validateResponse('354'))) { return new PEAR_Error('354 not received'); } + if (PEAR::isError($this->socket->write($data . "\r\n.\r\n"))) { return new PEAR_Error('write to socket failed'); } + if (!($this->validateResponse('250'))) { return new PEAR_Error('250 OK not received'); } + + return true; + } + /** * Send the SEND FROM: command. - * + * * @param string The reverse path to send. * * @return mixed Returns a PEAR_Error with an error message on any * kind of failure, or true on success. * @access public */ - function send_from($reverse_path) { - if (PEAR::isError($this->socket->write("SEND FROM:<$reverse_path>\r\n"))) { return new PEAR_Error('write to socket failed'); } - if (!($this->validateResponse('250'))) { return new PEAR_Error('250 OK not received'); } - - return true; - } - + function send_from($reverse_path) { + if (PEAR::isError($this->socket->write("SEND FROM:<$reverse_path>\r\n"))) { return new PEAR_Error('write to socket failed'); } + if (!($this->validateResponse('250'))) { return new PEAR_Error('250 OK not received'); } + + return true; + } + /** * Send the SOML FROM: command. - * + * * @param string The reverse path to send. * * @return mixed Returns a PEAR_Error with an error message on any * kind of failure, or true on success. * @access public */ - function soml_from($reverse_path) { - if (PEAR::isError($this->socket->write("SOML FROM:<$reverse_path>\r\n"))) { return new PEAR_Error('write to socket failed'); } - if (!($this->validateResponse('250'))) { return new PEAR_Error('250 OK not received'); } - - return true; - } - + function soml_from($reverse_path) { + if (PEAR::isError($this->socket->write("SOML FROM:<$reverse_path>\r\n"))) { return new PEAR_Error('write to socket failed'); } + if (!($this->validateResponse('250'))) { return new PEAR_Error('250 OK not received'); } + + return true; + } + /** * Send the SAML FROM: command. - * + * * @param string The reverse path to send. * * @return mixed Returns a PEAR_Error with an error message on any * kind of failure, or true on success. * @access public */ - function saml_from($reverse_path) { - if (PEAR::isError($this->socket->write("SAML FROM:<$reverse_path>\r\n"))) { return new PEAR_Error('write to socket failed'); } - if (!($this->validateResponse('250'))) { return new PEAR_Error('250 OK not received'); } - - return true; - } - + function saml_from($reverse_path) { + if (PEAR::isError($this->socket->write("SAML FROM:<$reverse_path>\r\n"))) { return new PEAR_Error('write to socket failed'); } + if (!($this->validateResponse('250'))) { return new PEAR_Error('250 OK not received'); } + + return true; + } + /** * Send the RSET command. - * + * * @return mixed Returns a PEAR_Error with an error message on any * kind of failure, or true on success. * @access public */ - function rset() { - if (PEAR::isError($this->socket->write("RSET\r\n"))) { return new PEAR_Error('write to socket failed'); } - if (!($this->validateResponse('250'))) { return new PEAR_Error('250 OK not received'); } - - return true; - } - + function rset() { + if (PEAR::isError($this->socket->write("RSET\r\n"))) { return new PEAR_Error('write to socket failed'); } + if (!($this->validateResponse('250'))) { return new PEAR_Error('250 OK not received'); } + + return true; + } + /** * Send the VRFY command. - * + * * @param string The string to verify * * @return mixed Returns a PEAR_Error with an error message on any * kind of failure, or true on success. * @access public */ - function vrfy($string) { - /* Note: 251 is also a valid response code */ - if (PEAR::isError($this->socket->write("VRFY $string\r\n"))) { return new PEAR_Error('write to socket failed'); } - if (!($this->validateResponse('250'))) { return new PEAR_Error('250 OK not received'); } - - return true; - } - + function vrfy($string) { + /* Note: 251 is also a valid response code */ + if (PEAR::isError($this->socket->write("VRFY $string\r\n"))) { return new PEAR_Error('write to socket failed'); } + if (!($this->validateResponse('250'))) { return new PEAR_Error('250 OK not received'); } + + return true; + } + /** * Send the NOOP command. - * + * * @return mixed Returns a PEAR_Error with an error message on any * kind of failure, or true on success. * @access public */ - function noop() { - if (PEAR::isError($this->socket->write("NOOP\r\n"))) { return new PEAR_Error('write to socket failed'); } - if (!($this->validateResponse('250'))) { return new PEAR_Error('250 OK not received'); } - - return true; - } - + function noop() { + if (PEAR::isError($this->socket->write("NOOP\r\n"))) { return new PEAR_Error('write to socket failed'); } + if (!($this->validateResponse('250'))) { return new PEAR_Error('250 OK not received'); } + + return true; + } + /** * Attempt to send the EHLO command and obtain a list of ESMTP * extensions available, and failing that just send HELO. - * + * * @return mixed Returns a PEAR_Error with an error message on any * kind of failure, or true on success. * @access private */ - function identifySender() { - if (PEAR::isError($this->socket->write("EHLO $this->localhost\r\n"))) { return new PEAR_Error('write to socket failed'); } - + function identifySender() { + if (PEAR::isError($this->socket->write("EHLO $this->localhost\r\n"))) { return new PEAR_Error('write to socket failed'); } + $extensions = array(); - if (!($this->validateAndParseResponse('250', $extensions))) { - if (PEAR::isError($this->socket->write("HELO $this->localhost\r\n"))) { return new PEAR_Error('write to socket failed'); } - if (!($this->validateResponse('250'))) { return new PEAR_Error('HELO not accepted', $this->code); } - return true; - } - - for ($i = 0; $i < count($extensions); $i++) { - $verb = strtok($extensions[$i], ' '); - $arguments = substr($extensions[$i], strlen($verb) + 1, strlen($extensions[$i]) - strlen($verb) - 2); - $this->esmtp[$verb] = $arguments; - } - return true; - } - + if (!($this->validateAndParseResponse('250', $extensions))) { + if (PEAR::isError($this->socket->write("HELO $this->localhost\r\n"))) { return new PEAR_Error('write to socket failed'); } + if (!($this->validateResponse('250'))) { return new PEAR_Error('HELO not accepted', $this->code); } + return true; + } + + for ($i = 0; $i < count($extensions); $i++) { + $verb = strtok($extensions[$i], ' '); + $arguments = substr($extensions[$i], strlen($verb) + 1, strlen($extensions[$i]) - strlen($verb) - 2); + $this->esmtp[$verb] = $arguments; + } + return true; + } + /** * Read a response from the server and see if the response code * matches what we are expecting. - * + * * @param int The response code we are expecting. * * @return boolean True if we get what we expect, false otherwise. * @access private */ - function validateResponse($code) { - while ($this->lastline = $this->socket->readLine()) { - $reply_code = strtok($this->lastline, ' '); - if (!(strcmp($code, $reply_code))) { - $this->code = $reply_code; - return true; - } else { - $reply_code = strtok($this->lastline, '-'); - if (strcmp($code, $reply_code)) { - $this->code = $reply_code; - return false; - } - } - } - + function validateResponse($code) { + while ($this->lastline = $this->socket->readLine()) { + $reply_code = strtok($this->lastline, ' '); + if (!(strcmp($code, $reply_code))) { + $this->code = $reply_code; + return true; + } else { + $reply_code = strtok($this->lastline, '-'); + if (strcmp($code, $reply_code)) { + $this->code = $reply_code; + return false; + } + } + } + return false; - } - + } + /** * Read a response from the server and see if the response code * matches what we are expecting. Also save the rest of the @@ -374,27 +374,27 @@ * @return boolean True if we get what we expect, false otherwise. * @access private */ - function validateAndParseResponse($code, &$arguments) { - $arguments = array(); - - while ($this->lastline = $this->socket->readLine()) { - $reply_code = strtok($this->lastline, ' '); - if (!(strcmp($code, $reply_code))) { - $arguments[] = substr($this->lastline, strlen($code) + 1, strlen($this->lastline) - strlen($code) - 1); - $this->code = $reply_code; - return true; - } else { - $reply_code = strtok($this->lastline, '-'); - if (strcmp($code, $reply_code)) { - $this->code = $reply_code; - return false; - } - } - $arguments[] = substr($this->lastline, strlen($code) + 1, strlen($this->lastline) - strlen($code) - 1); - } - + function validateAndParseResponse($code, &$arguments) { + $arguments = array(); + + while ($this->lastline = $this->socket->readLine()) { + $reply_code = strtok($this->lastline, ' '); + if (!(strcmp($code, $reply_code))) { + $arguments[] = substr($this->lastline, strlen($code) + 1, strlen($this->lastline) - strlen($code) - 1); + $this->code = $reply_code; + return true; + } else { + $reply_code = strtok($this->lastline, '-'); + if (strcmp($code, $reply_code)) { + $this->code = $reply_code; + return false; + } + } + $arguments[] = substr($this->lastline, strlen($code) + 1, strlen($this->lastline) - strlen($code) - 1); + } + return false; - } - + } + } -?> +?> \ No newline at end of file
« previous php.pear.cvs (#4460) next »