예제 #1
0
파일: Mail.php 프로젝트: WasabiLib/Mail
 /**
  * @param string | \Zend\View\Model\ViewModel | Mime\Part $body
  * @return $this
  */
 public function setBody($body, $charset = null)
 {
     $mimeMessage = new Mime\Message();
     $finalBody = null;
     if (is_string($body)) {
         // Create a Mime\Part and wrap it into a Mime\Message
         $mimePart = new Mime\Part($body);
         $mimePart->type = $body != strip_tags($body) ? Mime\Mime::TYPE_HTML : Mime\Mime::TYPE_TEXT;
         $mimePart->charset = $charset ?: self::DEFAULT_CHARSET;
         $mimeMessage->setParts([$mimePart]);
         $finalBody = $mimeMessage;
     } elseif ($body instanceof Mime\Part) {
         // Overwrite the charset if the Part object if provided
         if (isset($charset)) {
             $body->charset = $charset;
         }
         // The body is a Mime\Part. Wrap it into a Mime\Message
         $mimeMessage->setParts([$body]);
         $finalBody = $mimeMessage;
     } elseif ($body instanceof ViewModel) {
         $view = new View();
         $view->setResponse(new Response());
         $view->getEventManager()->attach(new PhpRendererStrategy($this->renderer));
         $view->render($body);
         $content = $view->getResponse()->getContent();
         $mimePart = new Mime\Part($content);
         $mimePart->type = Mime\Mime::TYPE_HTML;
         $mimePart->charset = $charset ?: self::DEFAULT_CHARSET;
         $mimeMessage->setParts([$mimePart]);
         $finalBody = $mimeMessage;
     } else {
         throw new InvalidArgumentException(sprintf('Provided body is not valid. It should be one of "%s". %s provided', implode('", "', ['string', 'Zend\\Mime\\Part', 'Zend\\Mime\\Message', 'Zend\\View\\Model\\ViewModel']), is_object($body) ? get_class($body) : gettype($body)));
     }
     // The headers Content-type and Content-transfer-encoding are duplicated every time the body is set.
     // Removing them before setting the body prevents this error
     $this->message->getHeaders()->removeHeader('contenttype');
     $this->message->getHeaders()->removeHeader('contenttransferencoding');
     $this->message->setBody($finalBody);
     return $this;
 }