123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503 |
- <?php
- require_once 'Net/SMTP.php';
- class Mail_smtpmx extends Mail {
-
- var $_smtp = null;
-
- var $port = 25;
-
- var $mailname = 'localhost';
-
- var $timeout = 10;
-
- var $withNetDns = true;
-
- var $resolver;
-
- var $verp = false;
-
- var $vrfy = false;
-
- var $test = false;
-
- var $debug = false;
-
- var $errorCode = array(
- 'not_connected' => array(
- 'code' => 1,
- 'msg' => 'Could not connect to any mail server ({HOST}) at port {PORT} to send mail to {RCPT}.'
- ),
- 'failed_vrfy_rcpt' => array(
- 'code' => 2,
- 'msg' => 'Recipient "{RCPT}" could not be veryfied.'
- ),
- 'failed_set_from' => array(
- 'code' => 3,
- 'msg' => 'Failed to set sender: {FROM}.'
- ),
- 'failed_set_rcpt' => array(
- 'code' => 4,
- 'msg' => 'Failed to set recipient: {RCPT}.'
- ),
- 'failed_send_data' => array(
- 'code' => 5,
- 'msg' => 'Failed to send mail to: {RCPT}.'
- ),
- 'no_from' => array(
- 'code' => 5,
- 'msg' => 'No from address has be provided.'
- ),
- 'send_data' => array(
- 'code' => 7,
- 'msg' => 'Failed to create Net_SMTP object.'
- ),
- 'no_mx' => array(
- 'code' => 8,
- 'msg' => 'No MX-record for {RCPT} found.'
- ),
- 'no_resolver' => array(
- 'code' => 9,
- 'msg' => 'Could not start resolver! Install PEAR:Net_DNS or switch off "netdns"'
- ),
- 'failed_rset' => array(
- 'code' => 10,
- 'msg' => 'RSET command failed, SMTP-connection corrupt.'
- ),
- );
-
- function __construct($params)
- {
- if (isset($params['mailname'])) {
- $this->mailname = $params['mailname'];
- } else {
-
- if (function_exists('posix_uname')) {
- $uname = posix_uname();
- $this->mailname = $uname['nodename'];
- }
- }
-
- if (isset($params['port'])) {
- $this->_port = $params['port'];
- } else {
- $this->_port = getservbyname('smtp', 'tcp');
- }
- if (isset($params['timeout'])) $this->timeout = $params['timeout'];
- if (isset($params['verp'])) $this->verp = $params['verp'];
- if (isset($params['test'])) $this->test = $params['test'];
- if (isset($params['peardebug'])) $this->test = $params['peardebug'];
- if (isset($params['netdns'])) $this->withNetDns = $params['netdns'];
- }
-
- function Mail_smtpmx($params)
- {
- $this->__construct($params);
- register_shutdown_function(array(&$this, '__destruct'));
- }
-
- function __destruct()
- {
- if (is_object($this->_smtp)) {
- $this->_smtp->disconnect();
- $this->_smtp = null;
- }
- }
-
- function send($recipients, $headers, $body)
- {
- if (!is_array($headers)) {
- return PEAR::raiseError('$headers must be an array');
- }
- $result = $this->_sanitizeHeaders($headers);
- if (is_a($result, 'PEAR_Error')) {
- return $result;
- }
-
- $headerElements = $this->prepareHeaders($headers);
- if (is_a($headerElements, 'PEAR_Error')) {
- return $headerElements;
- }
- list($from, $textHeaders) = $headerElements;
-
- if (!empty($headers['Return-Path'])) {
- $from = $headers['Return-Path'];
- }
- if (!isset($from)) {
- return $this->_raiseError('no_from');
- }
-
- $recipients = $this->parseRecipients($recipients);
- if (is_a($recipients, 'PEAR_Error')) {
- return $recipients;
- }
- foreach ($recipients as $rcpt) {
- list($user, $host) = explode('@', $rcpt);
- $mx = $this->_getMx($host);
- if (is_a($mx, 'PEAR_Error')) {
- return $mx;
- }
- if (empty($mx)) {
- $info = array('rcpt' => $rcpt);
- return $this->_raiseError('no_mx', $info);
- }
- $connected = false;
- foreach ($mx as $mserver => $mpriority) {
- $this->_smtp = new Net_SMTP($mserver, $this->port, $this->mailname);
-
- if ($this->debug) {
- $this->_smtp->setDebug(true);
- }
-
- $res = $this->_smtp->connect($this->timeout);
- if (is_a($res, 'PEAR_Error')) {
- $this->_smtp = null;
- continue;
- }
-
- if ($res) {
- $connected = true;
- break;
- }
- }
- if (!$connected) {
- $info = array(
- 'host' => implode(', ', array_keys($mx)),
- 'port' => $this->port,
- 'rcpt' => $rcpt,
- );
- return $this->_raiseError('not_connected', $info);
- }
-
- if ($this->vrfy) {
- $res = $this->_smtp->vrfy($rcpt);
- if (is_a($res, 'PEAR_Error')) {
- $info = array('rcpt' => $rcpt);
- return $this->_raiseError('failed_vrfy_rcpt', $info);
- }
- }
-
- $args['verp'] = $this->verp;
- $res = $this->_smtp->mailFrom($from, $args);
- if (is_a($res, 'PEAR_Error')) {
- $info = array('from' => $from);
- return $this->_raiseError('failed_set_from', $info);
- }
-
- $res = $this->_smtp->rcptTo($rcpt);
- if (is_a($res, 'PEAR_Error')) {
- $info = array('rcpt' => $rcpt);
- return $this->_raiseError('failed_set_rcpt', $info);
- }
-
- if ($this->test) {
- $result = $this->_smtp->rset();
- $res = $this->_smtp->rset();
- if (is_a($res, 'PEAR_Error')) {
- return $this->_raiseError('failed_rset');
- }
- $this->_smtp->disconnect();
- $this->_smtp = null;
- return true;
- }
-
- $res = $this->_smtp->data($body, $textHeaders);
- if (is_a($res, 'PEAR_Error')) {
- $info = array('rcpt' => $rcpt);
- return $this->_raiseError('failed_send_data', $info);
- }
- $this->_smtp->disconnect();
- $this->_smtp = null;
- }
- return true;
- }
-
- function _getMx($host)
- {
- $mx = array();
- if ($this->withNetDns) {
- $res = $this->_loadNetDns();
- if (is_a($res, 'PEAR_Error')) {
- return $res;
- }
- $response = $this->resolver->query($host, 'MX');
- if (!$response) {
- return false;
- }
- foreach ($response->answer as $rr) {
- if ($rr->type == 'MX') {
- $mx[$rr->exchange] = $rr->preference;
- }
- }
- } else {
- $mxHost = array();
- $mxWeight = array();
- if (!getmxrr($host, $mxHost, $mxWeight)) {
- return false;
- }
- for ($i = 0; $i < count($mxHost); ++$i) {
- $mx[$mxHost[$i]] = $mxWeight[$i];
- }
- }
- asort($mx);
- return $mx;
- }
-
- function _loadNetDns()
- {
- if (is_object($this->resolver)) {
- return true;
- }
- if (!include_once 'Net/DNS.php') {
- return $this->_raiseError('no_resolver');
- }
- $this->resolver = new Net_DNS_Resolver();
- if ($this->debug) {
- $this->resolver->test = 1;
- }
- return true;
- }
-
- function _raiseError($id, $info = array())
- {
- $code = $this->errorCode[$id]['code'];
- $msg = $this->errorCode[$id]['msg'];
-
- if (!empty($info)) {
- $search = array();
- $replace = array();
- foreach ($info as $key => $value) {
- array_push($search, '{' . strtoupper($key) . '}');
- array_push($replace, $value);
- }
- $msg = str_replace($search, $replace, $msg);
- }
- return PEAR::raiseError($msg, $code);
- }
- }
|