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
RickoT1031RickoT1031 

RE: Stripping html from HTMLBody of an inbound e-mail

Hello,

 

I need to process e-mails coming from a sharepoint form which of course does not submit both html and plaintext formatting, so I need a way to strip the HTML from the email without loosing the cariage returns of the email itself.   I have been looking thru the forums and the docs but i have not been able to figure it out

 

Any suggestions?

Best Answer chosen by Admin (Salesforce Developers) 
Ispita_NavatarIspita_Navatar

I am attaching the code for stripping HTML , it makes use of two classes viz Pattern and Matcher.

 

Please find enclose the sample code :-

string html = 'your html code';

  //first replace all <BR> tags with \n to support new lines

 string result = html.replaceAll('<br/>', '\n');

 result = result.replaceAll('<br />', '\n');

 //regular expression to match all HTML/XML tags

 string HTML_TAG_PATTERN = '<.*?>';

 // compile the pattern  

 pattern myPattern = pattern.compile(HTML_TAG_PATTERN);

 // get your matcher instance

 matcher myMatcher = myPattern.matcher(result);

 //remove the tags

 result = myMatcher.replaceAll('');

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

All Answers

Ispita_NavatarIspita_Navatar

I am attaching the code for stripping HTML , it makes use of two classes viz Pattern and Matcher.

 

Please find enclose the sample code :-

string html = 'your html code';

  //first replace all <BR> tags with \n to support new lines

 string result = html.replaceAll('<br/>', '\n');

 result = result.replaceAll('<br />', '\n');

 //regular expression to match all HTML/XML tags

 string HTML_TAG_PATTERN = '<.*?>';

 // compile the pattern  

 pattern myPattern = pattern.compile(HTML_TAG_PATTERN);

 // get your matcher instance

 matcher myMatcher = myPattern.matcher(result);

 //remove the tags

 result = myMatcher.replaceAll('');

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.

This was selected as the best answer
RickoT1031RickoT1031

Seems to have worked like a charm, thank you very much for your input!

Alex K.ax676Alex K.ax676

Ispita Saha... You're awesome!

 

It works great for me too.

 

Thanks a lot.

Alex