/** * Build a standardized string describing the current SMTP error. * * @param string $text Custom string describing the error context. * @param PEAR_Error $error PEAR_Error object. * @param integer $e_code Error code. * * @throws Horde_Mail_Exception */ protected function _error($text, $error, $e_code) { /* Split the SMTP response into a code and a response string. */ list($code, $response) = $this->_smtp->getResponse(); /* Abort current SMTP transaction. */ $this->_smtp->rset(); /* Build our standardized error string. */ throw new Horde_Mail_Exception($text . ' [SMTP: ' . $error->getMessage() . " (code: {$code}, response: {$response})]", $e_code); }
/** */ public function send($recipients, array $headers, $body) { $headers = $this->_sanitizeHeaders($headers); // Prepare headers list($from, $textHeaders) = $this->prepareHeaders($headers); try { $from = $this->_getFrom($from, $headers); } catch (Horde_Mail_Exception $e) { $this->_error('no_from'); } // Prepare recipients foreach ($this->parseRecipients($recipients) as $rcpt) { list(, $host) = explode('@', $rcpt); $mx = $this->_getMx($host); if (!$mx) { $this->_error('no_mx', array('rcpt' => $rcpt)); } $connected = false; foreach (array_keys($mx) as $mserver) { $this->_smtp = new Net_SMTP($mserver, $this->_params['port'], $this->_params['mailname']); // configure the SMTP connection. if ($this->_params['debug']) { $this->_smtp->setDebug(true); } // attempt to connect to the configured SMTP server. $res = $this->_smtp->connect($this->_params['timeout']); if ($res instanceof PEAR_Error) { $this->_smtp = null; continue; } // connection established if ($res) { $connected = true; break; } } if (!$connected) { $this->_error('not_connected', array('host' => implode(', ', array_keys($mx)), 'port' => $this->_params['port'], 'rcpt' => $rcpt)); } // Verify recipient if ($this->_params['vrfy']) { $res = $this->_smtp->vrfy($rcpt); if ($res instanceof PEAR_Error) { $this->_error('failed_vrfy_rcpt', array('rcpt' => $rcpt)); } } // mail from: $args['verp'] = $this->_params['verp']; $res = $this->_smtp->mailFrom($from, $args); if ($res instanceof PEAR_Error) { $this->_error('failed_set_from', array('from' => $from)); } // rcpt to: $res = $this->_smtp->rcptTo($rcpt); if ($res instanceof PEAR_Error) { $this->_error('failed_set_rcpt', array('rcpt' => $rcpt)); } // Don't send anything in test mode if ($this->_params['test']) { $res = $this->_smtp->rset(); if ($res instanceof PEAR_Error) { $this->_error('failed_rset'); } $this->_smtp->disconnect(); $this->_smtp = null; return; } // Send data. Net_SMTP does necessary EOL conversions. $res = $this->_smtp->data($body, $textHeaders); if ($res instanceof PEAR_Error) { $this->_error('failed_send_data', array('rcpt' => $rcpt)); } $this->_smtp->disconnect(); $this->_smtp = null; } }