📄 smtp.php
字号:
<?php/* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */// +----------------------------------------------------------------------+// | PHP Version 4 |// +----------------------------------------------------------------------+// | Copyright (c) 1997-2003 The PHP Group |// +----------------------------------------------------------------------+// | This source file is subject to version 2.02 of the PHP license, |// | that is bundled with this package in the file LICENSE, and is |// | available at through the world-wide-web at |// | http://www.php.net/license/2_02.txt. |// | If you did not receive a copy of the PHP license and are unable to |// | obtain it through the world-wide-web, please send a note to |// | license@php.net so we can mail you a copy immediately. |// +----------------------------------------------------------------------+// | Authors: Chuck Hagenbuch <chuck@horde.org> |// | Jon Parise <jon@php.net> |// | Damian Alejandro Fernandez Sosa <damlists@cnba.uba.ar> |// +----------------------------------------------------------------------+//// $Id: SMTP.php,v 1.58 2007/03/28 04:53:34 chagenbu Exp $require_once 'PEAR.php';require_once 'Net/Socket.php';/** * Provides an implementation of the SMTP protocol using PEAR's * Net_Socket:: class. * * @package Net_SMTP * @author Chuck Hagenbuch <chuck@horde.org> * @author Jon Parise <jon@php.net> * @author Damian Alejandro Fernandez Sosa <damlists@cnba.uba.ar> * * @example basic.php A basic implementation of the Net_SMTP package. */class Net_SMTP{ /** * The server to connect to. * @var string * @access public */ var $host = 'localhost'; /** * The port to connect to. * @var int * @access public */ var $port = 25; /** * The value to give when sending EHLO or HELO. * @var string * @access public */ var $localhost = 'localhost'; /** * List of supported authentication methods, in preferential order. * @var array * @access public */ var $auth_methods = array('DIGEST-MD5', 'CRAM-MD5', 'LOGIN', 'PLAIN'); /** * Should debugging output be enabled? * @var boolean * @access private */ var $_debug = false; /** * The socket resource being used to connect to the SMTP server. * @var resource * @access private */ var $_socket = null; /** * The most recent server response code. * @var int * @access private */ var $_code = -1; /** * The most recent server response arguments. * @var array * @access private */ var $_arguments = array(); /** * Stores detected features of the SMTP server. * @var array * @access private */ var $_esmtp = array(); /** * Instantiates a new Net_SMTP object, overriding any defaults * with parameters that are passed in. * * If you have SSL support in PHP, you can connect to a server * over SSL using an 'ssl://' prefix: * * // 465 is a common smtps port. * $smtp = new Net_SMTP('ssl://mail.host.com', 465); * $smtp->connect(); * * @param string $host The server to connect to. * @param integer $port The port to connect to. * @param string $localhost The value to give when sending EHLO or HELO. * * @access public * @since 1.0 */ 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; $this->_socket = new Net_Socket(); /* * Include the Auth_SASL package. If the package is not available, * we disable the authentication methods that depend upon it. */ if ((@include_once 'Auth/SASL.php') === false) { $pos = array_search('DIGEST-MD5', $this->auth_methods); unset($this->auth_methods[$pos]); $pos = array_search('CRAM-MD5', $this->auth_methods); unset($this->auth_methods[$pos]); } } /** * Set the value of the debugging flag. * * @param boolean $debug New value for the debugging flag. * * @access public * @since 1.1.0 */ function setDebug($debug) { $this->_debug = $debug; } /** * Send the given string of data to the server. * * @param string $data The string of data to send. * * @return mixed True on success or a PEAR_Error object on failure. * * @access private * @since 1.1.0 */ function _send($data) { if ($this->_debug) { echo "DEBUG: Send: $data\n"; } if (PEAR::isError($error = $this->_socket->write($data))) { return PEAR::raiseError('Failed to write to socket: ' . $error->getMessage()); } return true; } /** * Send a command to the server with an optional string of * arguments. A carriage return / linefeed (CRLF) sequence will * be appended to each command string before it is sent to the * SMTP server - an error will be thrown if the command string * already contains any newline characters. Use _send() for * commands that must contain newlines. * * @param string $command The SMTP command to send to the server. * @param string $args A string of optional arguments to append * to the command. * * @return mixed The result of the _send() call. * * @access private * @since 1.1.0 */ function _put($command, $args = '') { if (!empty($args)) { $command .= ' ' . $args; } if (strcspn($command, "\r\n") !== strlen($command)) { return PEAR::raiseError('Commands cannot contain newlines'); } return $this->_send($command . "\r\n"); } /** * Read a reply from the SMTP server. The reply consists of a response * code and a response message. * * @param mixed $valid The set of valid response codes. These * may be specified as an array of integer * values or as a single integer value. * * @return mixed True if the server returned a valid response code or * a PEAR_Error object is an error condition is reached. * * @access private * @since 1.1.0 * * @see getResponse */ function _parseResponse($valid) { $this->_code = -1; $this->_arguments = array(); while ($line = $this->_socket->readLine()) { if ($this->_debug) { echo "DEBUG: Recv: $line\n"; } /* If we receive an empty line, the connection has been closed. */ if (empty($line)) { $this->disconnect(); return PEAR::raiseError('Connection was unexpectedly closed'); } /* Read the code and store the rest in the arguments array. */ $code = substr($line, 0, 3); $this->_arguments[] = trim(substr($line, 4)); /* Check the syntax of the response code. */ if (is_numeric($code)) { $this->_code = (int)$code; } else { $this->_code = -1; break; } /* If this is not a multiline response, we're done. */ if (substr($line, 3, 1) != '-') { break; } } /* Compare the server's response code with the valid code. */ if (is_int($valid) && ($this->_code === $valid)) { return true; } /* If we were given an array of valid response codes, check each one. */ if (is_array($valid)) { foreach ($valid as $valid_code) { if ($this->_code === $valid_code) { return true; } } } return PEAR::raiseError('Invalid response code received from server', $this->_code); } /** * Return a 2-tuple containing the last response from the SMTP server. * * @return array A two-element array: the first element contains the * response code as an integer and the second element * contains the response's arguments as a string. * * @access public * @since 1.1.0 */ function getResponse() { return array($this->_code, join("\n", $this->_arguments)); } /** * Attempt to connect to the SMTP server. * * @param int $timeout The timeout value (in seconds) for the * socket connection. * @param bool $persistent Should a persistent socket connection * be used? * * @return mixed Returns a PEAR_Error with an error message on any * kind of failure, or true on success. * @access public * @since 1.0 */ function connect($timeout = null, $persistent = false) { $result = $this->_socket->connect($this->host, $this->port, $persistent, $timeout); if (PEAR::isError($result)) { return PEAR::raiseError('Failed to connect socket: ' . $result->getMessage()); } if (PEAR::isError($error = $this->_parseResponse(220))) { return $error; } if (PEAR::isError($error = $this->_negotiate())) { return $error; } return true; } /** * Attempt to disconnect from the SMTP server. * * @return mixed Returns a PEAR_Error with an error message on any * kind of failure, or true on success. * @access public * @since 1.0 */ function disconnect() { if (PEAR::isError($error = $this->_put('QUIT'))) { return $error; } if (PEAR::isError($error = $this->_parseResponse(221))) { return $error; } if (PEAR::isError($error = $this->_socket->disconnect())) { return PEAR::raiseError('Failed to disconnect socket: ' . $error->getMessage()); } return true; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -