Converting HTML to Plain Text in PHP For E-Mail

Converting HTML to plain text in PHP for e-mail

Use html2text (example HTML to text), licensed under the Eclipse Public License. It uses PHP's DOM methods to load from HTML, and then iterates over the resulting DOM to extract plain text. Usage:

// when installed using the Composer package
$text = Html2Text\Html2Text::convert($html);

// usage when installed using html2text.php
require('html2text.php');
$text = convert_html_to_text($html);

Although incomplete, it is open source and contributions are welcome.

Issues with other conversion scripts:

  • Since html2text (GPL) is not EPL-compatible.
  • lkessler's link (attribution) is incompatible with most open source licenses.

HTML to plain text (for email)

I'd suggest using a HTML to Markdown converter.

  • https://github.com/Pixel418/Markdownify
  • https://code.google.com/p/pandoc/source/browse/trunk/html2markdown?r=1651

Problem while converting html code to plain text

By default, html_entity_decode only converts double quotes and not single quotes. Use the following to convert both:

$message = html_entity_decode($message, ENT_QUOTES);

https://www.php.net/manual/en/function.html-entity-decode.php

Converting PHPMailer HTML message to Text Message

Er, you do know that PHPMailer has two built-in HTML to plain text converters? Strip_tags is a pretty poor solution - it often results in an unreadable mess. The best results I've got come from using a real text-based browser (specifically elinks) which can make a great job of rendering HTML as text, able to do things like preserve multi-column layouts, underlining, link references and some CSS. However, shelling out isn't always an option, so PHP solutions exist.

PHPMailer does an automatic conversion for the plain text version in msgHTML(), so your code would simply be this:

$mail = new PHPMailer();
$htmlMessage="Hello John,<br /><br />How are you?";
$mail->msgHTML($htmlMessage);

There is an option for a slightly more advanced converter (bundled in the extras folder) which you can trigger by setting the 'advanced' param in msgHTML:

$mail->msgHTML($htmlMessage, '', true);

Try them both, see what works best for you - but it's always worth reading the docs and source code!

Update Since I posted this answer, PHPMailer has had to remove the bundled html2text class for license reasons, but the mechanism for plugging in your own converter has been improved - the advanced parameter can now be a closure through which you can implement your own converter, such as any of those you might find on packagist.

Convert HTML output into a plain text using php

Use php strip_tags

If strip_tags is not working for then maybe you can use regex to extract the info you want.

Try using PHP preg_match with /(<td>.*?<\/td>)/ as the pattern

Phpmailer sending email as plain text to emails

The issue was solved by enabling smtp login details

Send html and plain text email simultaneously with PHP Mailer

See below:

$mail = new PHPMailer();

$mail->IsHTML(true);
$mail->CharSet = "text/html; charset=UTF-8;";
$mail->IsSMTP();

$mail->WordWrap = 80;
$mail->Host = "smtp.thehost.com";
$mail->SMTPAuth = false;

$mail->From = $from;
$mail->FromName = $from; // First name, last name
$mail->AddAddress($to, "First name last name");
#$mail->AddReplyTo("reply@thehost.com", "Reply to address");

$mail->Subject = $subject;
$mail->Body = $htmlMessage;
$mail->AltBody = $textMessage; # This automatically sets the email to multipart/alternative. This body can be read by mail clients that do not have HTML email capability such as mutt.

if(!$mail->Send())
{
throw new Exception("Mailer Error: " . $mail->ErrorInfo);
}

Thanks to http://snippets.aktagon.com/snippets/129-how-to-send-both-html-and-text-emails-with-php-and-phpmailer and Google. SMTP use might be optional for you.



Related Topics



Leave a reply



Submit