Example #1
0
 /**
  */
 public function send($recipients, array $headers, $body)
 {
     /* If we don't already have an SMTP object, create one. */
     $this->getSMTPObject();
     $headers = $this->_sanitizeHeaders($headers);
     /* Make sure the message has a trailing newline. */
     if (is_resource($body)) {
         fseek($body, -1, SEEK_END);
         switch (fgetc($body)) {
             case "\r":
                 if (fgetc($body) != "\n") {
                     fputs($body, "\n");
                 }
                 break;
             default:
                 fputs($body, "\r\n");
                 break;
         }
         rewind($body);
     } elseif (substr($body, -2, 0) != "\r\n") {
         $body .= "\r\n";
     }
     try {
         list($from, $textHeaders) = $this->prepareHeaders($headers);
     } catch (Horde_Mail_Exception $e) {
         $this->_smtp->rset();
         throw $e;
     }
     try {
         $from = $this->_getFrom($from, $headers);
     } catch (Horde_Mail_Exception $e) {
         $this->_smtp->rset();
         throw new Horde_Mail_Exception('No From: address has been provided', self::ERROR_FROM);
     }
     $params = '';
     foreach ($this->_extparams as $key => $val) {
         $params .= ' ' . $key . (is_null($val) ? '' : '=' . $val);
     }
     $res = $this->_smtp->mailFrom($from, ltrim($params));
     if ($res instanceof PEAR_Error) {
         $this->_error(sprintf("Failed to set sender: %s", $from), $res, self::ERROR_SENDER);
     }
     try {
         $recipients = $this->parseRecipients($recipients);
     } catch (Horde_Mail_Exception $e) {
         $this->_smtp->rset();
         throw $e;
     }
     foreach ($recipients as $recipient) {
         $res = $this->_smtp->rcptTo($recipient);
         if ($res instanceof PEAR_Error) {
             $this->_error("Failed to add recipient: {$recipient}", $res, self::ERROR_RECIPIENT);
         }
     }
     /* Send the message's headers and the body as SMTP data. Net_SMTP does
      * the necessary EOL conversions. */
     $res = $this->_smtp->data($body, $textHeaders);
     list(, $args) = $this->_smtp->getResponse();
     if (preg_match("/Ok: queued as (.*)/", $args, $queued)) {
         $this->queuedAs = $queued[1];
     }
     /* We need the greeting; from it we can extract the authorative name
      * of the mail server we've really connected to. Ideal if we're
      * connecting to a round-robin of relay servers and need to track
      * which exact one took the email */
     $this->greeting = $this->_smtp->getGreeting();
     if ($res instanceof PEAR_Error) {
         $this->_error('Failed to send data', $res, self::ERROR_DATA);
     }
     /* If persistent connections are disabled, destroy our SMTP object. */
     if (!$this->_params['persist']) {
         $this->disconnect();
     }
 }
Example #2
0
 /**
  * Send a message.
  *
  * @param mixed $recipients  Either a comma-seperated list of recipients
  *                           (RFC822 compliant), or an array of
  *                           recipients, each RFC822 valid. This may
  *                           contain recipients not specified in the
  *                           headers, for Bcc:, resending messages, etc.
  * @param array $headers     The headers to send with the mail, in an
  *                           associative array, where the array key is the
  *                           header name (ie, 'Subject'), and the array
  *                           value is the header value (ie, 'test'). The
  *                           header produced from those values would be
  *                           'Subject: test'.
  *                           If the '_raw' key exists, the value of this
  *                           key will be used as the exact text for
  *                           sending the message.
  * @param mixed $body        The full text of the message body, including
  *                           any Mime parts, etc. Either a string or a
  *                           stream resource.
  *
  * @throws Horde_Mail_Exception
  */
 public function send($recipients, array $headers, $body)
 {
     /* If we don't already have an SMTP object, create one. */
     $this->getSMTPObject();
     $headers = $this->_sanitizeHeaders($headers);
     try {
         list($from, $textHeaders) = $this->prepareHeaders($headers);
     } catch (Horde_Mail_Exception $e) {
         $this->_smtp->rset();
         throw $e;
     }
     /* Since few MTAs are going to allow this header to be forged unless
      * it's in the MAIL FROM: exchange, we'll use Return-Path instead of
      * From: if it's set. */
     foreach (array_keys($headers) as $hdr) {
         if (strcasecmp($hdr, 'Return-Path') === 0) {
             $from = $headers[$hdr];
             break;
         }
     }
     if (!strlen($from)) {
         $this->_smtp->rset();
         throw new Horde_Mail_Exception('No From: address has been provided', self::ERROR_FROM);
     }
     $params = '';
     foreach ($this->_extparams as $key => $val) {
         $params .= ' ' . $key . (is_null($val) ? '' : '=' . $val);
     }
     $res = $this->_smtp->mailFrom($from, ltrim($params));
     if ($res instanceof PEAR_Error) {
         $this->_error("Failed to set sender: {$from}", $res, self::ERROR_SENDER);
     }
     try {
         $recipients = $this->parseRecipients($recipients);
     } catch (Horde_Mail_Exception $e) {
         $this->_smtp->rset();
         throw $e;
     }
     foreach ($recipients as $recipient) {
         $res = $this->_smtp->rcptTo($recipient);
         if ($res instanceof PEAR_Error) {
             $this->_error("Failed to add recipient: {$recipient}", $res, self::ERROR_RECIPIENT);
         }
     }
     /* Send the message's headers and the body as SMTP data. Net_SMTP does
      * the necessary EOL conversions. */
     $res = $this->_smtp->data($body, $textHeaders);
     list(, $args) = $this->_smtp->getResponse();
     if (preg_match("/Ok: queued as (.*)/", $args, $queued)) {
         $this->queuedAs = $queued[1];
     }
     /* We need the greeting; from it we can extract the authorative name
      * of the mail server we've really connected to. Ideal if we're
      * connecting to a round-robin of relay servers and need to track
      * which exact one took the email */
     $this->greeting = $this->_smtp->getGreeting();
     if ($res instanceof PEAR_Error) {
         $this->_error('Failed to send data', $res, self::ERROR_DATA);
     }
     /* If persistent connections are disabled, destroy our SMTP object. */
     if ($this->_params['persist']) {
         $this->disconnect();
     }
 }