Example #1
0
 /**
  * @param $stringOrView
  * @return mixed|string
  */
 public function parseTemplate($stringOrView)
 {
     if ($stringOrView instanceof ViewModel) {
         $stringOrView = $this->getView()->render($stringOrView);
     }
     // find inline images.
     $xml = new \DOMDocument();
     $xml->loadHTML($stringOrView);
     $images = $xml->getElementsByTagName('img');
     $attachments = [];
     /* @var DomElement $image */
     foreach ($images as $image) {
         $file = $image->getAttribute('src');
         $binary = file_get_contents($file);
         $mime = $this->mimeByExtension($file);
         $fileName = pathinfo($file, PATHINFO_BASENAME);
         $attachment = new MimePart($binary);
         $attachment->setType($mime);
         $attachment->setDisposition(Mime::DISPOSITION_ATTACHMENT);
         $attachment->setEncoding(Mime::ENCODING_BASE64);
         $attachment->setFileName($fileName);
         $attachment->setId('cid_' . md5($fileName));
         $stringOrView = str_replace($file, 'cid:' . $attachment->getId(), $stringOrView);
         $attachments[] = $attachment;
     }
     $this->attachments = $attachments;
     return $stringOrView;
 }