Example #1
0
 function sendEmail($to, $subject, $html, $text, $attachments = null)
 {
     // HTML part
     $htmlPart = new Mime\Part($html);
     $htmlPart->setEncoding(Mime\Mime::ENCODING_QUOTEDPRINTABLE);
     $htmlPart->setType(Mime\Mime::TYPE_HTML);
     // Plain text part
     $textPart = new Mime\Part($text);
     $textPart->setEncoding(Mime\Mime::ENCODING_QUOTEDPRINTABLE);
     $textPart->setType(Mime\Mime::TYPE_TEXT);
     $body = new Mime\Message();
     if ($attachments) {
         // With attachments, we need a multipart/related email. First part
         // is itself a multipart/alternative message
         $content = new Mime\Message();
         $content->addPart($textPart);
         $content->addPart($htmlPart);
         $contentPart = new Mime\Part($content->generateMessage());
         $contentPart->setType(Mime\Mime::MULTIPART_ALTERNATIVE);
         $contentPart->setBoundary($content->getMime()->boundary());
         $body->addPart($contentPart);
         $messageType = Mime\Mime::MULTIPART_RELATED;
         // Add each attachment
         foreach ($attachments as $thisAttachment) {
             $attachment = new Mime\Part($thisAttachment['content']);
             $attachment->filename = $thisAttachment['filename'];
             $attachment->type = Mime\Mime::TYPE_OCTETSTREAM;
             $attachment->encoding = Mime\Mime::ENCODING_BASE64;
             $attachment->disposition = Mime\Mime::DISPOSITION_ATTACHMENT;
             $body->addPart($attachment);
         }
     } else {
         // No attachments, just add the two textual parts to the body
         $body->setParts([$textPart, $htmlPart]);
         $messageType = Mime\Mime::MULTIPART_ALTERNATIVE;
     }
     // attach the body to the message and set the content-type
     $message = new Message();
     $message->addTo($to);
     $message->setEncoding($this->encoding);
     $message->addFrom($this->fromEmail, $this->fromName);
     $message->setSubject($subject);
     $message->setBody($body);
     $message->getHeaders()->get('content-type')->setType($messageType);
     $this->getTransport()->send($message);
 }
Example #2
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;
 }