In order to avoid Postfix from wrapping ultra long lines after smtp_line_length_limit
(usually 998 characters) I am currently using php's tidy library to wrap long lines in HTML emails (see related question):
$oTidy = new tidy();
$message = $oTidy->repairString($message,
array("show-errors" => 0, "show-warnings" => false, "force-output" => true,
"alt-text" => "Please display images", "bare" => true, "doctype" => "auto",
"drop-empty-paras" => false, "fix-bad-comments" => false, "fix-uri" => true,
"join-styles" => false, "merge-divs" => true, "merge-spans" => true,
"preserve-entities" => true, "wrap" => 68),
"utf8"
);
Tidy is really good in wrapping long lines with respect to leaving HTML and CSS valid.
Unfortunately it does a lot more like trying to fix invalid HTML markup, changing HTML tags, doctypes, etc.
I only need the line wrapping however - the other stuff tidy does is overhead and sometimes rather annoying than anything else.
Now I have tried to use PHPMailer's wrapText()
function. Unfortunately I have found a bug that makes it useless for me.
PHPMailer converts this source code
<html>
<body>
Loremipsumdolorsitametconsetetursadipscing<span style="font-family:'Courier New',sans-serif">lorem</span>
</body>
</html>
to
<html>
<body>
Loremipsumdolorsitametconsete<span style="font-family:'Courier
New',sans-serif">lorem</span>
</body>
</html>
breaking the font formatting (Courier New) for the word lorem in some clients.
Now my questions:
How can I wrap HTML lines safely without damaging HTML and CSS?
How does Tidy do it? Should I use a DOM parser? Is there a php version of the Tidy source code (I haven't found one)?