/** * 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; }
/** * Send a message * * @param peer.mail.Message message the Message object to send * @return bool TRUE in case of success * @throws peer.mail.transport.TransportException to indicate an error occured */ public function send(Message $message) { try { $this->_sockcmd('MAIL FROM: %s', $message->from->getAddress(), 250); foreach ([TO, CC, BCC] as $type) { foreach ($message->getRecipients($type) as $r) { $this->_sockcmd('RCPT TO: %s', $r->getAddress(), [250, 251]); } } // Content: Headers and body. Make sure lines containing a dot by itself are // properly escaped. $this->_sockcmd('DATA', 354); $this->_sockcmd('%s', $message->getHeaderString(), false); $this->_sockcmd('%s', preg_replace('/(^|[\\r\\n])([\\.]+)([\\r\\n]|$)/', '$1.$2$3', $message->getBody()), false); } catch (\lang\XPException $e) { throw new TransportException('Sending message failed', $e); } return (bool) $this->_sockcmd('.', 250); }
/** * Parse a string containing message headers * * @param string $str * @return peer.mail.Message */ protected function parse($str) { $m = new Message(); $m->setHeaderString($str . "\n\n"); return $m; }