/**
  * @param MimePart $mimePart
  */
 function SearchAttachParts(&$mimePart)
 {
     if ($mimePart->_subParts == null) {
         if ($mimePart->IsMimePartAttachment()) {
             $this->AddToCollection($mimePart);
         }
     } else {
         for ($i = 0, $c = $mimePart->_subParts->List->Count(); $i < $c; $i++) {
             $subPart =& $mimePart->_subParts->List->Get($i);
             $this->SearchAttachParts($subPart);
         }
     }
 }
 /**
  * @return string
  */
 function ToString()
 {
     $retval = '';
     $lineStr = '--';
     if ($this->List->Count() > 0) {
         $bound = $this->_parent->GetBoundary();
         foreach ($this->List->Instance() as $mimePart) {
             $retval .= $lineStr . $bound . CRLF;
             $retval .= $mimePart->ToString() . CRLF;
         }
         $retval .= $lineStr . $this->_parent->GetBoundary() . $lineStr . CRLF;
     }
     return $retval;
 }
 /**
  * Reads header lines up to an empty line, adding them to the passed $part.
  * 
  * @param resource $handle the resource handle to read from
  * @param \ZBateson\MailMimeParser\MimePart $part the current part to add
  *        headers to
  */
 protected function readHeaders($handle, MimePart $part)
 {
     $header = '';
     do {
         $line = fgets($handle, 1000);
         if ($line[0] !== "\t" && $line[0] !== ' ') {
             if (!empty($header) && strpos($header, ':') !== false) {
                 $a = explode(':', $header, 2);
                 $part->setRawHeader($a[0], trim($a[1]));
             }
             $header = '';
         } else {
             $line = ' ' . ltrim($line);
         }
         $header .= rtrim($line, "\r\n");
     } while (!empty($header));
 }
 /**
  * Constructor. Also generates a boundary of the form
  * <pre>
  * ----=_Alternative_10424693873e22d20b43b490.00112051
  * </pre>
  *
  * @param   peer.mail.MimePart* parts
  */
 public function __construct()
 {
     parent::__construct();
     $this->charset = '';
     for ($i = 0, $s = func_num_args(); $i < $s; $i++) {
         $this->addPart(func_get_arg($i));
     }
     $this->setBoundary('----=_Alternative_' . uniqid(time(), TRUE));
 }
 /**
  * Creates a part stream handle for the start and end position of the
  * message stream, and attaches it to the passed MimePart.
  * 
  * @param MimePart $part
  * @param Message $message
  * @param int $start
  * @param int $end
  */
 public function attachPartStreamHandle(MimePart $part, Message $message, $start, $end)
 {
     $id = $message->getObjectId();
     if (empty($this->registeredHandles[$id])) {
         return null;
     }
     $handle = fopen('mmp-mime-message://' . $id . '?start=' . $start . '&end=' . $end, 'r');
     $encoding = strtolower($part->getHeaderValue('Content-Transfer-Encoding'));
     if ($encoding === 'quoted-printable') {
         stream_filter_append($handle, 'convert.quoted-printable-decode', STREAM_FILTER_READ);
     } elseif ($encoding === 'base64') {
         stream_filter_append($handle, 'convert.base64-decode', STREAM_FILTER_READ);
     }
     $contentType = strtolower($part->getHeaderValue('Content-Type'));
     if (empty($contentType) || strpos($contentType, 'text/') === 0) {
         stream_filter_append($handle, 'mailmimeparser-encode.' . $part->getHeaderParameter('Content-Type', 'charset'));
     }
     $this->registeredPartStreamHandles[$id] = $handle;
     $part->attachContentResourceHandle($handle);
 }
 public function testMimeMail()
 {
     $mimeMail = new MimeMail();
     $mimeMail->setBoundary('MIME_MAIL_TEST');
     $mimeMail->addPart(MimePart::create()->setEncoding(MailEncoding::base64())->setCharset('UTF-8')->loadBodyFromFile(dirname(__FILE__) . '/data/mimeMail/message.html')->setContentType('text/html'));
     $mimeMail->addPart(MimePart::create()->setContentId('picture')->setEncoding(MailEncoding::base64())->setFilename('picture.jpg')->loadBodyFromFile(dirname(__FILE__) . '/data/mimeMail/picture.jpg')->setContentType('image/jpeg'));
     $mimeMail->build();
     //			file_put_contents(dirname(__FILE__).'/data/mimeMail/headers.txt', $mimeMail->getHeaders());
     //			file_put_contents(dirname(__FILE__).'/data/mimeMail/encodedBody.txt', $mimeMail->getEncodedBody());
     $this->assertEquals($mimeMail->getHeaders(), file_get_contents(dirname(__FILE__) . '/data/mimeMail/headers.txt'));
     $this->assertEquals($mimeMail->getEncodedBody(), file_get_contents(dirname(__FILE__) . '/data/mimeMail/encodedBody.txt'));
 }
 public function build()
 {
     if (!$this->parts) {
         throw new UnimplementedFeatureException();
     }
     if (!$this->boundary) {
         $this->boundary = '=_' . md5(microtime(true));
     }
     $mail = MimePart::create()->setContentType('multipart/mixed')->setBoundary($this->boundary);
     $this->headers = "MIME-Version: 1.0\n" . $mail->getHeaders();
     foreach ($this->parts as $part) {
         $this->body .= '--' . $this->boundary . "\n" . $part->getHeaders() . "\n\n" . $part->getEncodedBody() . "\n";
     }
     $this->body .= '--' . $this->boundary . "--" . "\n\n";
 }
 /**
  * @param MimePart $mimePart
  */
 function AddToCollection(&$mimePart)
 {
     $TEXTLIMIT = 150000;
     if ($mimePart->IsMimePartTextBody()) {
         if ($mimePart->IsMimePartAttachment()) {
             return;
         }
         $contentType = strtolower($mimePart->GetContentType());
         $charset = new HeaderParameterCollection($contentType);
         $charset = $charset->GetByName(MIMEConst_CharsetLower);
         $contentCharset = $charset ? $charset->Value : '';
         if ($GLOBALS[MailInputCharset] == '') {
             $GLOBALS[MailInputCharset] = $contentCharset;
             $this->HasCharset = $contentCharset;
         }
         if (strpos($contentType, MIMETypeConst_TextPlain) !== false || $contentType == '') {
             $this->PlainTextBodyPart .= trim($mimePart->GetBody($TEXTLIMIT));
         } elseif (strpos($contentType, MIMETypeConst_TextHtml) !== false) {
             $preStr = '/(<meta\\s.*)(charset\\s?=)([^"\'>\\s]*)/i';
             $this->HtmlTextBodyPart .= trim(preg_replace($preStr, '$1$2' . $GLOBALS[MailOutputCharset], $mimePart->GetBody($TEXTLIMIT)));
         }
     }
 }
 /**
  * @param string $rawData
  * @return MailMessage
  */
 function MailMessage($rawData = null, $holdOriginalBody = false)
 {
     @ini_set('memory_limit', MEMORYLIMIT);
     @set_time_limit(TIMELIMIT);
     $GLOBALS[MailInputCharset] = isset($GLOBALS[MailInputCharset]) ? $GLOBALS[MailInputCharset] : '';
     MimePart::MimePart($rawData);
     $null = null;
     $this->Attachments = new AttachmentCollection($null);
     $this->TextBodies = new TextBodyCollection($null);
     //$this->Headers->SetHeaderByName(MIMEConst_XMailer, 'MailBee WebMail Pro');
     $this->OriginalMailMessage = '';
     if ($rawData) {
         if ($holdOriginalBody) {
             $this->OriginalMailMessage =& $rawData;
         }
         $this->_setAllParams();
     }
 }
Example #10
0
 /**
  * @param MailMessage $paren
  * @return MailMessage
  */
 function &CreateNewTextMail($paren = null)
 {
     $newMail = new MimePart();
     if ($paren) {
         $newMail->Headers =& $paren->Headers;
     }
     $newMail->_sourceCharset = $GLOBALS[MailOutputCharset];
     if ($this->HasPlainText()) {
         $newMail->Headers->SetHeaderByName(MIMEConst_ContentType, MIMETypeConst_TextPlain . '; ' . MIMEConst_CharsetLower . '="' . $GLOBALS[MailOutputCharset] . '"');
         $newMail->SetEncodedBodyFromText($this->TextBodies->PlainTextBodyPart);
     }
     if ($this->HasHtmlText()) {
         $newMail->Headers->SetHeaderByName(MIMEConst_ContentType, MIMETypeConst_TextHtml . '; ' . MIMEConst_CharsetLower . '="' . $GLOBALS[MailOutputCharset] . '"');
         $newMail->SetEncodedBodyFromText($this->TextBodies->HtmlTextBodyPart);
     }
     return $newMail;
 }
 /**
  * @return MimePart
  */
 function ToHtmlMime()
 {
     $newHtmlMimePart = new MimePart();
     $newHtmlMimePart->Headers->SetHeaderByName(MIMEConst_ContentType, MIMETypeConst_TextHtml . '; ' . MIMEConst_CharsetLower . '="' . $GLOBALS[MailOutputCharset] . '"');
     $newHtmlMimePart->Headers->SetHeaderByName(MIMEConst_ContentTransferEncoding, MIMEConst_QuotedPrintable);
     /* $newHtmlMimePart->Headers->SetHeaderByName(MIMEConst_ContentTransferEncoding, MIMEConst_Base64); */
     $newHtmlMimePart->SetEncodedBodyFromText($this->HtmlTextBodyPart);
     $newHtmlMimePart->_sourceCharset = $GLOBALS[MailOutputCharset];
     return $newHtmlMimePart;
 }
Example #12
0
 /**
  * Creates a document from a MIME string.
  *
  * Note this currently only supports a single level of MIME - no nesting.
  *
  * @param $documentString
  * @return MimeDocument
  */
 public static function fromString($documentString)
 {
     $document = new MimeDocument($documentString);
     $lines = explode("\n", $documentString);
     $boundary = false;
     $nextPartLines = [];
     $firstBoundaryFound = false;
     $mimeMessage = "";
     foreach ($lines as $line) {
         $line = trim($line);
         if (!$boundary) {
             if (preg_match("/Content-Type: (multipart\\/.+);\\s+boundary=\"([^\"]+)\"/", $line, $match)) {
                 $document->boundary = $match[2];
                 $document->setContentType($match[1]);
                 $boundary = $match[2];
                 continue;
             }
         } else {
             if ($line == "--" . $boundary . "--") {
                 $part = MimePart::fromLines($nextPartLines);
                 $document->addPart($part);
                 break;
             }
             if ($line == "--" . $boundary) {
                 if (!$firstBoundaryFound) {
                     $firstBoundaryFound = true;
                 } else {
                     $part = MimePart::fromLines($nextPartLines);
                     $document->addPart($part);
                     $nextPartLines = [];
                 }
             } else {
                 if ($firstBoundaryFound) {
                     $nextPartLines[] = $line;
                 } else {
                     $mimeMessage .= $line . "\r\n";
                 }
             }
         }
     }
     $document->setMessage(trim($mimeMessage));
     return $document;
 }
Example #13
0
 /**
  * Gets the collection of the attachment headers.
  * @return HeaderCollection
  */
 function &GetHeaders()
 {
     return $this->MimePart->GetHeaders();
 }