+ Start a Discussion
ArunaAruna 

Parsing Email body plain Text and HTML in Apex class

Hi Guys,

 

I need to parse the Email body in Apex class and get  CaseId , OrderNumber, Address ,Email, and  ComplaintID etc

 

from below text. (below text includes both text and HTML content)

 

Hello XXX,

 

Thank you for contacting xxxxx.

 

test email from ttttt.com

 

Please let us know if we can be of further assistance.

 

Sincerely,

xxxxxx

yyyyy

 

Case ID: {5555555}

ComplaintID: {888888888}

OrderNo: {133333}

 

some text here also hgkkkkkkkkkkkkkkkkkkkkkfjhkhghllglglklkl;;oh;oh;oioii;'isdrtgrrdgdsgdgdg

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxyyyyyyyyyyyyyydfhdhreuhdfhryurtyhrtyuryutrytryutryutrttrutr

 

 

Original Message Follows: ------------------------

 

Subject:

xxxxxxx

Customer:

yyyy

E-mail Address:

xxxxx@ccc.com

Phone:

3333333

Address:

22 xxxxx,hhhhh,dd

Other:

xxxxx

Question/Comments:

ddddddd

 

 

please any one suggest me how to proceed with this , If any one having sample code for how to parse the email  body "PlainText:" and "HTMl" content, I will be thank full to them.

 

Thank you

Aruna

 

 

RJPalomboRJPalombo

Hi Aruna, did you have any luck with this?

ArunaAruna

Hi RJP,

 

I got the solution.

 

Can you tell me what you want exactly.

 

Thanks

RJPalomboRJPalombo

I created a parser that works great with Plain Text email, but when I get a version with HTML (Images, msword like markup,  ext..) I need to just get the text from the email. I found some solutions online using pattern matching, but nothing works perfect to get me just the text. If you have something I would love to see it. :)

 

Thanks in advance!

ArunaAruna

Hi

 

Parse out the HML veraision first like and try to find out the html tages position of the text which you want to get from the html version email body for example like

 

in our case after parseing the html version email bisy need to get the customer name so look at the below code , I hope this is what u r looking for , if not could you please \explain to me i will try to suggest what ever i can .

string custLabel =

'>customer: </';

string custStartTag =

'<font size="-2">';

string custEndTag =

'</font>';

string customer;

integer custLabelIndex = -1;

integer custStartTagIndex = -1;

integer custEndTagIndex = -1;

list< string > names;

custLabelIndex = eHTMLBody.indexOf(custLabel);

if(custLabelIndex != -1) {

custStartTagIndex = eHTMLBody.indexOf(custStartTag, custLabelIndex);

if(custStartTagIndex != -1) {

custEndTagIndex = eHTMLBody.indexOf(custEndTag, custStartTagIndex);

if(custEndTagIndex != -1) {

customer = eHTMLBody.substring(custStartTagIndex + custStartTag.length(), custEndTagIndex);

if (customer != null && customer.trim() != '') {

names = customer.split(

'\\s', 0);

 

if(names.size() > 1) {

ContactFirstName =

'';

 

for(integer i = 0; i < names.size() - 1; i++) {

ContactFirstName = (ContactFirstName +

' '+ names[i]).trim();

}

}

else{

ContactFirstName = customer;

}

if(names.size() > 1) {

ContactLastName = names[names.size() - 1];

}

else{

ContactLastName = customer;

}

}

}

}

}

}