| version 1.1, 2005/06/05 12:14:30 |
version 1.2, 2005/11/07 20:37:49 |
|
|
| // +----------------------------------------------------------------------+ |
// +----------------------------------------------------------------------+ |
| |
|
| /** |
/** |
| * SMTP implementation of the PEAR Mail:: interface. Requires the PEAR |
* SMTP implementation of the PEAR Mail interface. Requires the Net_SMTP class. |
| * Net_SMTP:: class. |
|
| * @access public |
* @access public |
| * @package Mail |
* @package Mail |
| * @version $Revision$ |
* @version $Revision$ |
|
|
| class Mail_smtp extends Mail { |
class Mail_smtp extends Mail { |
| |
|
| /** |
/** |
| |
* SMTP connection object. |
| |
* |
| |
* @var object |
| |
* @access private |
| |
*/ |
| |
var $_smtp = null; |
| |
|
| |
/** |
| * The SMTP host to connect to. |
* The SMTP host to connect to. |
| * @var string |
* @var string |
| */ |
*/ |
| Line 72 class Mail_smtp extends Mail { |
|
| Line 79 class Mail_smtp extends Mail { |
|
| var $localhost = 'localhost'; |
var $localhost = 'localhost'; |
| |
|
| /** |
/** |
| |
* SMTP connection timeout value. NULL indicates no timeout. |
| |
* |
| |
* @var integer |
| |
*/ |
| |
var $timeout = null; |
| |
|
| |
/** |
| * Whether to use VERP or not. If not a boolean, the string value |
* Whether to use VERP or not. If not a boolean, the string value |
| * will be used as the VERP separators. |
* will be used as the VERP separators. |
| * |
* |
| Line 87 class Mail_smtp extends Mail { |
|
| Line 101 class Mail_smtp extends Mail { |
|
| var $debug = false; |
var $debug = false; |
| |
|
| /** |
/** |
| |
* Indicates whether or not the SMTP connection should persist over |
| |
* multiple calls to the send() method. |
| |
* |
| |
* @var boolean |
| |
*/ |
| |
var $persist = false; |
| |
|
| |
/** |
| * Constructor. |
* Constructor. |
| * |
* |
| * Instantiates a new Mail_smtp:: object based on the parameters |
* Instantiates a new Mail_smtp:: object based on the parameters |
| Line 97 class Mail_smtp extends Mail { |
|
| Line 119 class Mail_smtp extends Mail { |
|
| * username The username to use for SMTP auth. No default. |
* username The username to use for SMTP auth. No default. |
| * password The password to use for SMTP auth. No default. |
* password The password to use for SMTP auth. No default. |
| * localhost The local hostname / domain. Defaults to localhost. |
* localhost The local hostname / domain. Defaults to localhost. |
| |
* timeout The SMTP connection timeout. Defaults to none. |
| * verp Whether to use VERP or not. Defaults to false. |
* verp Whether to use VERP or not. Defaults to false. |
| * debug Activate SMTP debug mode? Defaults to false. |
* debug Activate SMTP debug mode? Defaults to false. |
| |
* persist Should the SMTP connection persist? |
| * |
* |
| * If a parameter is present in the $params array, it replaces the |
* If a parameter is present in the $params array, it replaces the |
| * default. |
* default. |
| Line 115 class Mail_smtp extends Mail { |
|
| Line 139 class Mail_smtp extends Mail { |
|
| if (isset($params['username'])) $this->username = $params['username']; |
if (isset($params['username'])) $this->username = $params['username']; |
| if (isset($params['password'])) $this->password = $params['password']; |
if (isset($params['password'])) $this->password = $params['password']; |
| if (isset($params['localhost'])) $this->localhost = $params['localhost']; |
if (isset($params['localhost'])) $this->localhost = $params['localhost']; |
| |
if (isset($params['timeout'])) $this->timeout = $params['timeout']; |
| if (isset($params['verp'])) $this->verp = $params['verp']; |
if (isset($params['verp'])) $this->verp = $params['verp']; |
| if (isset($params['debug'])) $this->debug = (boolean)$params['debug']; |
if (isset($params['debug'])) $this->debug = (boolean)$params['debug']; |
| |
if (isset($params['persist'])) $this->persist = (boolean)$params['persist']; |
| |
|
| |
register_shutdown_function(array(&$this, '_Mail_smtp')); |
| |
} |
| |
|
| |
/** |
| |
* Destructor implementation to ensure that we disconnect from any |
| |
* potentially-alive persistent SMTP connections. |
| |
*/ |
| |
function _Mail_smtp() |
| |
{ |
| |
$this->disconnect(); |
| } |
} |
| |
|
| /** |
/** |
| Line 147 class Mail_smtp extends Mail { |
|
| Line 184 class Mail_smtp extends Mail { |
|
| { |
{ |
| include_once 'Net/SMTP.php'; |
include_once 'Net/SMTP.php'; |
| |
|
| if (!($smtp = &new Net_SMTP($this->host, $this->port, $this->localhost))) { |
/* If we don't already have an SMTP object, create one. */ |
| return PEAR::raiseError('unable to instantiate Net_SMTP object'); |
if (is_object($this->_smtp) === false) { |
| } |
$this->_smtp =& new Net_SMTP($this->host, $this->port, |
| |
$this->localhost); |
| if ($this->debug) { |
|
| $smtp->setDebug(true); |
/* If we still don't have an SMTP object at this point, fail. */ |
| } |
if (is_object($this->_smtp) === false) { |
| |
return PEAR::raiseError('Failed to create a Net_SMTP object'); |
| |
} |
| |
|
| if (PEAR::isError($smtp->connect())) { |
/* Configure the SMTP connection. */ |
| return PEAR::raiseError('unable to connect to smtp server ' . |
if ($this->debug) { |
| $this->host . ':' . $this->port); |
$this->_smtp->setDebug(true); |
| } |
} |
| |
|
| if ($this->auth) { |
/* Attempt to connect to the configured SMTP server. */ |
| $method = is_string($this->auth) ? $this->auth : ''; |
if (PEAR::isError($res = $this->_smtp->connect($this->timeout))) { |
| |
$error = $this->_error('Failed to connect to ' . |
| |
$this->host . ':' . $this->port, |
| |
$res); |
| |
return PEAR::raiseError($error); |
| |
} |
| |
|
| if (PEAR::isError($smtp->auth($this->username, $this->password, |
/* Attempt to authenticate if authentication has been enabled. */ |
| $method))) { |
if ($this->auth) { |
| return PEAR::raiseError('unable to authenticate to smtp server'); |
$method = is_string($this->auth) ? $this->auth : ''; |
| |
|
| |
if (PEAR::isError($res = $this->_smtp->auth($this->username, |
| |
$this->password, |
| |
$method))) { |
| |
$error = $this->_error("$method authentication failure", |
| |
$res); |
| |
$this->_smtp->rset(); |
| |
return PEAR::raiseError($error); |
| |
} |
| } |
} |
| } |
} |
| |
|
| $headerElements = $this->prepareHeaders($headers); |
$headerElements = $this->prepareHeaders($headers); |
| if (PEAR::isError($headerElements)) { |
if (PEAR::isError($headerElements)) { |
| |
$this->_smtp->rset(); |
| return $headerElements; |
return $headerElements; |
| } |
} |
| list($from, $text_headers) = $headerElements; |
list($from, $textHeaders) = $headerElements; |
| |
|
| /* Since few MTAs are going to allow this header to be forged |
/* Since few MTAs are going to allow this header to be forged |
| * unless it's in the MAIL FROM: exchange, we'll use |
* unless it's in the MAIL FROM: exchange, we'll use |
| Line 183 class Mail_smtp extends Mail { |
|
| Line 237 class Mail_smtp extends Mail { |
|
| } |
} |
| |
|
| if (!isset($from)) { |
if (!isset($from)) { |
| return PEAR::raiseError('No from address given'); |
$this->_smtp->rset(); |
| |
return PEAR::raiseError('No From: address has been provided'); |
| } |
} |
| |
|
| $args['verp'] = $this->verp; |
$args['verp'] = $this->verp; |
| if (PEAR::isError($smtp->mailFrom($from, $args))) { |
if (PEAR::isError($res = $this->_smtp->mailFrom($from, $args))) { |
| return PEAR::raiseError('unable to set sender to [' . $from . ']'); |
$error = $this->_error("Failed to set sender: $from", $res); |
| |
$this->_smtp->rset(); |
| |
return PEAR::raiseError($error); |
| } |
} |
| |
|
| $recipients = $this->parseRecipients($recipients); |
$recipients = $this->parseRecipients($recipients); |
| if (PEAR::isError($recipients)) { |
if (PEAR::isError($recipients)) { |
| |
$this->_smtp->rset(); |
| return $recipients; |
return $recipients; |
| } |
} |
| |
|
| foreach ($recipients as $recipient) { |
foreach ($recipients as $recipient) { |
| if (PEAR::isError($res = $smtp->rcptTo($recipient))) { |
if (PEAR::isError($res = $this->_smtp->rcptTo($recipient))) { |
| return PEAR::raiseError('unable to add recipient [' . |
$error = $this->_error("Failed to add recipient: $recipient", |
| $recipient . ']: ' . $res->getMessage()); |
$res); |
| |
$this->_smtp->rset(); |
| |
return PEAR::raiseError($error); |
| } |
} |
| } |
} |
| |
|
| if (PEAR::isError($smtp->data($text_headers . "\r\n" . $body))) { |
/* Send the message's headers and the body as SMTP data. */ |
| return PEAR::raiseError('unable to send data'); |
if (PEAR::isError($res = $this->_smtp->data("$textHeaders\r\n$body"))) { |
| |
$error = $this->_error('Failed to send data', $res); |
| |
$this->_smtp->rset(); |
| |
return PEAR::raiseError($error); |
| |
} |
| |
|
| |
/* If persistent connections are disabled, destroy our SMTP object. */ |
| |
if ($this->persist === false) { |
| |
$this->disconnect(); |
| } |
} |
| |
|
| $smtp->disconnect(); |
|
| return true; |
return true; |
| } |
} |
| |
|
| |
/** |
| |
* Disconnect and destroy the current SMTP connection. |
| |
* |
| |
* @return boolean True if the SMTP connection no longer exists. |
| |
* |
| |
* @since 1.1.9 |
| |
* @access public |
| |
*/ |
| |
function disconnect() |
| |
{ |
| |
/* If we have an SMTP object, disconnect and destroy it. */ |
| |
if (is_object($this->_smtp) && $this->_smtp->disconnect()) { |
| |
$this->_smtp = null; |
| |
} |
| |
|
| |
/* We are disconnected if we no longer have an SMTP object. */ |
| |
return ($this->_smtp === null); |
| |
} |
| |
|
| |
/** |
| |
* Build a standardized string describing the current SMTP error. |
| |
* |
| |
* @param string $text Custom string describing the error context. |
| |
* @param object $error Reference to the current PEAR_Error object. |
| |
* |
| |
* @return string A string describing the current SMTP error. |
| |
* |
| |
* @since 1.1.7 |
| |
* @access private |
| |
*/ |
| |
function _error($text, &$error) |
| |
{ |
| |
/* Split the SMTP response into a code and a response string. */ |
| |
list($code, $response) = $this->_smtp->getResponse(); |
| |
|
| |
/* Build our standardized error string. */ |
| |
$msg = $text; |
| |
$msg .= ' [SMTP: ' . $error->getMessage(); |
| |
$msg .= " (code: $code, response: $response)]"; |
| |
|
| |
return $msg; |
| |
} |
| } |
} |