* @license PHP License * @package Wombat * @subpackage MailMime */ WBClass::load('WBMail_Mime'); /** * Wombat Mail Mime Storage String * * Read and save mail from and to string. Usefull process incomming mail or * to transform mail object to sendable e-mail * * @version 0.3.5 * @package Wombat * @subpackage MailMime */ class WBMail_Mime_Storage_String extends WBMail_Mime_Storage { /** * second constructor */ protected function init() { } /** * Convert e-mail object to mime message * * Use current mail object and convert it to mime-mail using * PEAR package Mail_mime * * @param array $header * @return string $body */ public function saveToString(&$header = array()) { if (!is_array( $header)) { $header = array(); } $header = array_merge($header, $this->mail->getHeader()); $header['To'] = $this->mail->getRcpt()->get(); $header['From'] = $this->mail->getFrom()->get(); $tmp = $this->mail->getReturnPath()->get(); if (!empty($tmp)) { $header['Return-Path'] = $tmp; } $tmp = $this->mail->getReplyTo()->get(); if (!empty($tmp)) { $header['Reply-To'] = $tmp; } $tmp = $this->mail->getXSender()->get(); if (!empty($tmp)) { $header['X-Sender'] = $tmp; } $header['Subject'] = WBMail_Mime::utf82base64( $this->mail->GetSubject() ); $header['Date'] = gmdate('r', strtotime($this->mail->getDate())); WBClass::load('Mail', 'Mail_mime'); $mime = new Mail_mime("\n"); // fetch html body to replace content-ids $html = $this->mail->getHtmlBody(); // add files $atts = $this->mail->getAttachments(); foreach ($atts as $att) { /** @var $att WBMail_Mime_Part */ $cid = $att->getCid(); if (empty($cid)) { $mime->addAttachment( $att->getData() , $att->getMime() , $att->getName() , false ); continue; } $html = str_replace('cid:' . $cid, $att->getName(), $html); $mime->addHTMLImage( $att->getData() , $att->getMime() , $att->getName() , false ); } // add textual body $mime->setTXTBody($this->mail->getPlainBody()); if (!empty($html)) { $mime->setHTMLBody($html); } // build mime body and headers $params = array( 'head_charset' => 'utf-8' , 'text_charset' => 'utf-8' , 'text_encoding' => 'base64' , 'html_charset' => 'utf-8' , 'html_encoding' => 'base64' ); $body = $mime->get($params); $header = $mime->headers($header); return $body; } /** * Parse mime string * * Use PEAR::Mail_mimeDecode to parse mail string * * @param string $str */ public function loadFromString($str) { $this->clear(); /** @var WBMail_Mime_Parser */ $parser = WBClass::create('WBMail_Mime_Parser_PECL'); $parser->setRAW($str); $this->mail->setSubject($parser->getSubject()); $this->mail->setDate($parser->getDate()); $this->mail->setFrom($this->createPartAddress($parser->getFrom())); $this->mail->setRcpt($this->createPartAddress($parser->getRcpt())); $this->mail->setPlainBody($parser->getPlainBody()); $this->mail->setHtmlBody($parser->getHtmlBody()); $att = $parser->getAttachments(); foreach ($att as $a) { $this->mail->addAttachment($a); } unset($parser); } /** * Create address parts * * Instantiate address objects from mail address string * * @param string $string * @return WBMail_Mime_Part_Address */ protected function createPartAddress( $string ) { $add = WBClass::create('WBMail_Mime_Part_Address'); if (!preg_match('/(.+) \<(.+)\>/', $string, $match)) { // set plain e-mail address $string = trim($string, '<> '); $add->setEmailAddress($string); return $add; } // set e-mail address as well as name $add->setEmailAddress($match[2]); $name = trim($match[1], '"'); $name = explode(' ', $name, 2); if (count($name) == 2) { $add->setForename($name[0]); array_shift($name); } $add->setSurname($name[0]); return $add; } }