123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442 |
- <?php
- define('PEAR_MAIL_SMTP_ERROR_CREATE', 10000);
- define('PEAR_MAIL_SMTP_ERROR_CONNECT', 10001);
- define('PEAR_MAIL_SMTP_ERROR_AUTH', 10002);
- define('PEAR_MAIL_SMTP_ERROR_FROM', 10003);
- define('PEAR_MAIL_SMTP_ERROR_SENDER', 10004);
- define('PEAR_MAIL_SMTP_ERROR_RECIPIENT', 10005);
- define('PEAR_MAIL_SMTP_ERROR_DATA', 10006);
- class Mail_smtp extends Mail {
-
- var $_smtp = null;
-
- var $_extparams = array();
-
- var $host = 'localhost';
-
- var $port = 25;
-
- var $auth = false;
-
- var $username = '';
-
- var $password = '';
-
- var $localhost = 'localhost';
-
- var $timeout = null;
-
- var $debug = false;
-
- var $persist = false;
-
- var $pipelining;
-
- var $socket_options = array();
-
- public function __construct($params)
- {
- if (isset($params['host'])) $this->host = $params['host'];
- if (isset($params['port'])) $this->port = $params['port'];
- if (isset($params['auth'])) $this->auth = $params['auth'];
- if (isset($params['username'])) $this->username = $params['username'];
- if (isset($params['password'])) $this->password = $params['password'];
- if (isset($params['localhost'])) $this->localhost = $params['localhost'];
- if (isset($params['timeout'])) $this->timeout = $params['timeout'];
- if (isset($params['debug'])) $this->debug = (bool)$params['debug'];
- if (isset($params['persist'])) $this->persist = (bool)$params['persist'];
- if (isset($params['pipelining'])) $this->pipelining = (bool)$params['pipelining'];
- if (isset($params['socket_options'])) $this->socket_options = $params['socket_options'];
-
- if (isset($params['verp'])) {
- $this->addServiceExtensionParameter('XVERP', is_bool($params['verp']) ? null : $params['verp']);
- }
- }
-
- public function __destruct()
- {
- $this->disconnect();
- }
-
- public function send($recipients, $headers, $body)
- {
-
- $result = $this->getSMTPObject();
- if (PEAR::isError($result)) {
- return $result;
- }
- if (!is_array($headers)) {
- return PEAR::raiseError('$headers must be an array');
- }
- $this->_sanitizeHeaders($headers);
- $headerElements = $this->prepareHeaders($headers);
- if (is_a($headerElements, 'PEAR_Error')) {
- $this->_smtp->rset();
- return $headerElements;
- }
- list($from, $textHeaders) = $headerElements;
-
- if (!empty($headers['Return-Path'])) {
- $from = $headers['Return-Path'];
- }
- if (!isset($from)) {
- $this->_smtp->rset();
- return PEAR::raiseError('No From: address has been provided',
- PEAR_MAIL_SMTP_ERROR_FROM);
- }
- $params = null;
- if (!empty($this->_extparams)) {
- foreach ($this->_extparams as $key => $val) {
- $params .= ' ' . $key . (is_null($val) ? '' : '=' . $val);
- }
- }
- if (PEAR::isError($res = $this->_smtp->mailFrom($from, ltrim($params)))) {
- $error = $this->_error("Failed to set sender: $from", $res);
- $this->_smtp->rset();
- return PEAR::raiseError($error, PEAR_MAIL_SMTP_ERROR_SENDER);
- }
- $recipients = $this->parseRecipients($recipients);
- if (is_a($recipients, 'PEAR_Error')) {
- $this->_smtp->rset();
- return $recipients;
- }
- foreach ($recipients as $recipient) {
- $res = $this->_smtp->rcptTo($recipient);
- if (is_a($res, 'PEAR_Error')) {
- $error = $this->_error("Failed to add recipient: $recipient", $res);
- $this->_smtp->rset();
- return PEAR::raiseError($error, PEAR_MAIL_SMTP_ERROR_RECIPIENT);
- }
- }
-
- $res = $this->_smtp->data($body, $textHeaders);
- list(,$args) = $this->_smtp->getResponse();
- if (preg_match("/Ok: queued as (.*)/", $args, $queued)) {
- $this->queued_as = $queued[1];
- }
-
- $this->greeting = $this->_smtp->getGreeting();
- if (is_a($res, 'PEAR_Error')) {
- $error = $this->_error('Failed to send data', $res);
- $this->_smtp->rset();
- return PEAR::raiseError($error, PEAR_MAIL_SMTP_ERROR_DATA);
- }
-
- if ($this->persist === false) {
- $this->disconnect();
- }
- return true;
- }
-
- public function getSMTPObject()
- {
- if (is_object($this->_smtp) !== false) {
- return $this->_smtp;
- }
- include_once 'Net/SMTP.php';
- $this->_smtp = new Net_SMTP($this->host,
- $this->port,
- $this->localhost,
- $this->pipelining,
- 0,
- $this->socket_options);
-
- if (is_object($this->_smtp) === false) {
- return PEAR::raiseError('Failed to create a Net_SMTP object',
- PEAR_MAIL_SMTP_ERROR_CREATE);
- }
-
- if ($this->debug) {
- $this->_smtp->setDebug(true);
- }
-
- 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, PEAR_MAIL_SMTP_ERROR_CONNECT);
- }
-
- if ($this->auth) {
- $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, PEAR_MAIL_SMTP_ERROR_AUTH);
- }
- }
- return $this->_smtp;
- }
-
- public function addServiceExtensionParameter($keyword, $value = null)
- {
- $this->_extparams[$keyword] = $value;
- }
-
- public function disconnect()
- {
-
- if (is_object($this->_smtp) && $this->_smtp->disconnect()) {
- $this->_smtp = null;
- }
-
- return ($this->_smtp === null);
- }
-
- protected function _error($text, $error)
- {
-
- list($code, $response) = $this->_smtp->getResponse();
-
- return $text
- . ' [SMTP: ' . $error->getMessage()
- . " (code: $code, response: $response)]";
- }
- }
|