/**
  * Send a message
  *
  * @param   peer.mail.Message message the Message object to send
  * @return  bool success
  */
 public function send(Message $message)
 {
     // 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 \peer\mail\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 \lang\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(), ["\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 \io\IOException('Call to mail() failed'));
     }
     return true;
 }
Example #2
0
 /**
  * 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;
 }
 /**
  * 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 . '>';
 }