Beispiel #1
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;
 }