/** * Send a message * * @param peer.mail.Message message the Message object to send * @return bool success */ public function send($message) { // Sanity check: Is this a message? if (!$message instanceof Message) { throw new TransportException('Can only send messages (given: ' . xp::typeOf($message) . ')', new IllegalArgumentException('Parameter message is not a Message object')); } // Sanity check: Do we have at least one recipient? $to = ''; for ($i = 0, $s = sizeof($message->to); $i < $s; $i++) { if (!$message->to[$i] instanceof InternetAddress) { continue; } // Ignore! $to .= $message->to[$i]->toString($message->getCharset()) . ', '; } if (empty($to)) { throw new TransportException('No recipients defined (recipients[0]: ' . xp::typeOf($message->to[0]), new IllegalArgumentException('Recipient #0 is not an InternetAddress object')); } // Copy message and unset To / Subject. PHPs mail() function will add them // to the mail twice, otherwise $tmp = clone $message; unset($tmp->to); unset($tmp->subject); if (FALSE === mail(substr($to, 0, -2), QuotedPrintable::encode($message->getSubject(), $message->getCharset()), strtr($message->getBody(), array("\r\n" => "\n", "\r" => "\n")), rtrim($tmp->getHeaderString(), "\n"), $this->parameters)) { throw new TransportException('Could not send mail to ' . xp::stringOf($message->to[0]), new IOException('Call to mail() failed')); } return TRUE; }
/** * Create string representation * * Return values: * <pre> * - personal specified: =?iso-8859-1?Q?Timm_Friebe?= <*****@*****.**> * - Empty personal: <*****@*****.**> * </pre> * * @param string charset defaults to XP default encoding * @return string */ public function toString($charset = xp::ENCODING) { return (empty($this->personal) ? '' : QuotedPrintable::encode(iconv(xp::ENCODING, $charset, $this->personal), $charset) . ' ') . '<' . $this->localpart . '@' . $this->domain . '>'; }
/** * Check if a string needs to be encoded and encode it if necessary * * @param string str * @return string */ protected function _qstr($str) { static $q; if (!isset($q)) { $q = QuotedPrintable::getCharsToEncode(); } $n = FALSE; for ($i = 0, $s = strlen($str); $i < $s; $i++) { if (!in_array(ord($str[$i]), $q)) { continue; } $n = TRUE; break; } return $n ? QuotedPrintable::encode($str, $this->getCharset()) : $str; }