function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Lee_CampbellLee_Campbell 

Converting HTML to Plain Text in Inbound Email

Hi folks,

 

Is there any way I can convert the body of an inbound email from html to Plain Text? I've set up a service to email directly into chatter, but html emails sent from my organisation into Chatter are all of over 1,000 characters in length (even if I send a blank email). So, I'm looking for a way to strip the body text from the emails and discard all the html "nonsense", rather than force users to send emails in Plain Text format, which they'll be disinclined to do.

 

Any help anyone can give would be massively appreciated.

 

Thanks,

Lee

Best Answer chosen by Admin (Salesforce Developers) 
Lee_CampbellLee_Campbell

I found a parsing method somewhere else on the forum. You asnwered my query without moving a muscle! Thanks!

All Answers

Lee_CampbellLee_Campbell

I found a parsing method somewhere else on the forum. You asnwered my query without moving a muscle! Thanks!

This was selected as the best answer
MrTikMrTik

Where is the parsing method you found?

David Roberts 4David Roberts 4

But be aware:

The stripHtmlTags function does not recursively strip tags; therefore, tags may still exist in the returned string. Do not use the stripHtmlTags function to sanitize input for inclusion as a raw HTML page. The unescaped output is not considered safe to include in an HTML document. The function will be deprecated in a future release.

Adrian Larson suggests:

Pattern tagPattern = Pattern.compile('<\/?\w*\b[^>]*>');
String plainTextBody = tagPattern.matcher(htmlBody).replaceAll('');

in stack exchange (https://salesforce.stackexchange.com/questions/92828/how-can-i-convert-my-htmlbody-to-plaintextbody-using-apex-regex).
 
David Roberts 4David Roberts 4

You'll also need to escape the backslash with double backslashes:

Pattern tagPattern = Pattern.compile('<\/?\\w*\\b[^>]*>');
 
String regExp = '<style>[^<]*<\\/style>|<\\/?\\w*\\b[^>]*>';
Pattern tagPattern = Pattern.compile(regExp);
String plainTextBody = tagPattern.matcher(emailContent).replaceAll('');