I am not so experienced yet in SwiftMailer, but I can tell you how I did it myself, without having the problem you describe. Simplified, it looks like:
Code: Select all
$Msg->setBody($Content, "text/html");
$Msg->addPart(StripHtml ($Content), "text/plain");
Note that for the second "part" I use
instead of your
Code: Select all
$message->attach(new Swift_Message_Part($html_message), "text/html");
From your question, it does not seem to be clear whether the message with the "visible" tags as you show it, is actually the HTML part being wrongly displayed, or the TEXT part still containing the tags.
My function StripHtml (), is as follows:
Code: Select all
//------ StripHtml(): -------------------
function StripHtml ($Text)
{
$T = str_replace (" ", " ", $Text); // " " to spatie
$T = str_replace ("/\r\n/", "\n", $T); // "\r\n" to "\n"
$T = preg_replace ("/[\x20\x9]*<td[^>]*>[\n]?/", "\t", $T); // "<td>" to "\t"
$T = preg_replace ("/[\x20\x9]*<\/tr[^>]*>[\n]?/", "\n", $T); // "</tr>\n" to "\n"
$T = preg_replace ("/<\/t[drh][^>]*>[\n]?/", "", $T); // remove "</td>", "</tr>", "</th>"
$T = preg_replace ("/<\/title[^>]*>[\n]?/", "\n", $T); // "</title>" to "\n"
$T = preg_replace ("/<\/table[^>]*>[\n]?/", "\n", $T); // "</table>" to "\n"
$T = preg_replace ("/[\x20\x9]*<table[^>]*>[\n]?/", "", $T); // remove "<table>"
$T = preg_replace ("/[\x20\x9]*<tbody[^>]*>[\n]?/", "", $T); // remove "<tbody>"
$T = preg_replace ("/[\x20\x9]*<thead[^>]*>[\n]?/", "", $T); // remove "<thead>"
$T = preg_replace ("/[\x20\x9]*<tfoot[^>]*>[\n]?/", "", $T); // remove "<tfoot>"
$T = preg_replace ("/<br[^>]*>[\n]?/", "\n", $T); // "<br>\n" to "\n"
$T = preg_replace ("/<\/p[^>]*>[\n]?/", "\n", $T); // "</p>\n" to "\n"
$T = preg_replace ("/<\/h\d[^>]*>[\n]?/", "\n", $T); // "</h1>" etc. to "\n"
$T = preg_replace ("/<\/?b>/", "*", $T); // "<b>" and "</b>" to "*"
$T = preg_replace ("/<\/?i>/", "/", $T); // "<i>" and "</i>" to "/"
$T = preg_replace ("/<[^>]*>[\n]?/", "", $T); // remove all other HTML-tags
return ($T);
} // "StripHtml()"
It may not be quite optimised, but I dislike the unreadability of complex regular expressions.