In particular, it tries to maintain the following features:
static function convert($html)
{
// replace with spaces
$html = str_replace(" ", " ", $html);
$html = str_replace(" ", " ", $html);
if (static::isOfficeDocument($html)) {
// remove office namespace
$html = str_replace(array("<o:p>", "</o:p>"), "", $html);
}
$html = static::fixNewlines($html);
if (mb_detect_encoding($html, "UTF-8", true)) {
$html = mb_convert_encoding($html, "HTML-ENTITIES", "UTF-8");
}
$doc = new \DOMDocument();
if (!$doc->loadHTML($html)) {
throw new Html2TextException("Could not load HTML - badly formed?", $html);
}
if (static::isOfficeDocument($html)) {
// remove office namespace
$doc = static::fixMSEncoding($doc);
}
$output = static::iterateOverNode($doc);
// remove leading and trailing spaces on each line
$output = preg_replace("/[ \t]*\n[ \t]*/im", "\n", $output);
$output = preg_replace("/ *\t */im", "\t", $output);
// remove unnecessary empty lines
$output = preg_replace("/\n\n\n*/im", "\n\n", $output);
// remove leading and trailing whitespace
$output = trim($output);
return $output;
}
/** * Sends an email * @param \Swift_Message $message An email message * @param string $content The message body in HTML * @return int The number of recipients the message was delivered to */ protected static function sendEmail(\Swift_Message $message, $content) { // Lazy mailer instantiation if (!self::$mailer) { self::$mailer = \Swift_Mailer::newInstance(\Swift_SmtpTransport::newInstance(SMTP_SERVER, SMTP_SERVER_PORT)); } $message->setBody($content, 'text/html', 'utf-8'); $message->addPart(\Html2Text\Html2Text::convert(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8')), 'text/plain', 'utf-8'); return self::$mailer->send($message); }