public function setBody($body, $charset = 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;
$body = new Mime\Message();
$body->setParts([$mimePart]);
} 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 = new Mime\Message();
$mimeMessage->setParts([$body]);
$body = $mimeMessage;
}
// If the body is not a string or a Mime\Message at this point, it is not a valid argument
if (!is_string($body) && !$body instanceof Mime\Message) {
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']), 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($body);
return $this;
}