protected function manipulateBody($body, $mimeType = null)
    {
        // Make sure we have a string.
        if ($body instanceof ViewModel) {
            $body               = $this->viewRenderer->render($body);
            $detectedMimeType   = 'text/html';
        } elseif (null === $body) {
            $detectedMimeType   = 'text/plain';
            $body               = '';
        }

        if (null !== ($layout = $this->getLayout())) {
            $layout->setVariables(array(
                'content' => $body,
            ));

            $detectedMimeType   = 'text/html';
            $body               = $this->viewRenderer->render($layout);
        }


        if (null === $mimeType && !isset($detectedMimeType)) {
            $mimeType = preg_match("/<[^<]+>/", $body) ? 'text/html' : 'text/plain';
        } elseif (null === $mimeType) {
            $mimeType = $detectedMimeType;
        }

        $mimePart       = new MimePart($body);
        $mimePart->type = $mimeType;

        if (!empty($this->config['charset'])) {
            $mimePart->charset = $this->config['charset'];
        }

        $message        = new MimeMessage();

        if (!isset($this->config['message']['generate_alternative_body'])) {
            $this->config['message']['generate_alternative_body'] = true;
        }

        if ($this->config['message']['generate_alternative_body'] && $mimeType === 'text/html') {
            $generatedBody  = $this->renderTextBody($body);
            $altPart        = new MimePart($generatedBody);
            $altPart->type  = 'text/plain';

            if (!empty($this->config['charset'])) {
                $altPart->charset = $this->config['charset'];
            }

            $message->addPart($altPart);
        }

        $message->addPart($mimePart);

        return $message;
    }