"remove figure tags  REPLACE ALL OCCURRENCES OF REGEX '\[[^\s]+\]' IN lv_body_cont WITH space. "remove HMTL tags <> </> REPLACE ALL OCCURRENCES OF REGEX '<[a-zA-Z\/][^>]*>' IN l_html_string WITH space.
Inbound Email processing for MHT document type
SAP provides TCODE SO50 to deal with inbound emails with customized function modules. There’re many good articles that explain the whole process of email inbound processing including real scenarios very well:
- Receiving E-Mail and processing it with ABAP from Thomas Jung
- Advanced Inbound Email Handling from Christian Drumm
- Offline/External Mail Approval Process without using SAP
It’s very common to collect user comments from feedback emails and keep those texts as kind of notes updated on related transactions in SAP. Usually, user feedback email will be in plain text format or HTML format which easy to fetch the contents from the email body. But what if the user sends one email back with one figure even several figures mixed with text (not as an attachment) tailed with a complex email signature?
Where to get MHT email contents?
This question gives us a general method to decode email contents in MHT format. If we check the SOIN workflow trace log, it’s clear that every figure has already been parsed and processed separately by CL_BCOM_MIME~as_reference even before IF_INBOUND_EXIT_BCS~PROCESS been called like below:
The contents of the email body in MHT format are stored in input parameter IO_SREQ->MIME_DOC of method IF_INBOUND_EXIT_BCS~PROCESS_INBOUND.
Or share the same process as the HTML format email body like below as we can’t guarantee email must be in MHT format:
DATA: lo_document TYPE REF TO if_document_bcs, lo_mail_content TYPE bcss_dbpc, lt_solix TYPE solix_tab, l_xlength TYPE i, l_html_string TYPE string, l_html_string_c(4000), binary_file type xstring. FIELD-SYMBOLS : <bin_line> TYPE solix. "whole document lo_document = io_sreq->get_document( ). "all email contents lo_mail_content = lo_document->get_body_part_content(). "email contents lt_solix = lo_mail_content-cont_hex. "contents in binary CLEAR binary_file. LOOP AT lt_solix ASSIGNING <bin_line>. CONCATENATE binary_file <bin_line>-line INTO binary_file IN BYTE MODE. ENDLOOP. l_xlength = xstrlen( binary_file ). " get string of Email body from binary CALL FUNCTION 'SCMS_BINARY_TO_STRING' EXPORTING input_length = l_xlength IMPORTING text_buffer = l_html_string " <===what we need TABLES binary_tab = lt_solix.
How to decode the MHT email body?
The method cl_crm_email_utility_base=>get_mail_data_from_mime can parse the HTML string fetched from last step.
*----------could mix with text/picture/html~ only collect text part DATA: lo_email_data TYPE REF TO cl_crm_email_data, ls_mht_body TYPE crms_email_mime_struc, lv_body_cont TYPE string, lv_body_cont_c(4000). lo_email_data = cl_crm_email_utility_base=>get_mail_data_from_mime( l_html_string ). LOOP AT lo_email_data->body INTO ls_mht_body. IF ls_mht_body-is_attachment EQ 'X'. "skip attachemnt as only collect comments CONTINUE. ENDIF. IF ls_mht_body-mime_type = 'text/plain'. "only collect comments from this part! lv_body_cont = ls_mht_body-content_ascii. EXIT. ELSEIF ls_mht_body-mime_type = 'text/html'. "... * lv_body_cont = ls_mht_body-CONTENT_ASCII. ELSE. CONTINUE. ENDIF. ENDLOOP.
Inside the LO_EMAIL_DATA->BODY, we can separate plain/HTML or images directly with MIME_TYPE, and also its contents in characters for plain text where comments are!
I find one blog from Jerry which use table BCSD_BREAKLOOP to trigger but doesn’t know how to maintain this table entry. Then get this video tutorial which really helps and is easy to trigger.
PS: If SAP Blog provides a private flag, maybe I’ll set it for many of my posts because the same topic has been discussed many times and I couldn’t write a better one compared with the existing one. Still, sometimes it’s difficult to figure the issue out with help of GOOGLE and the community, or it may take hours to validate various solutions. So in most cases, I summarize related details and record my finding as a reminder. I’m glad if it helps others by chance, if it doesn’t, please forgive me keep posting such content without any buzzword.
I followed through your blog, but in my case when i received data from outlook to SAP in Process Inbound method
iam getting data as below, could you please help me to read only highlighted data from this.
and this data is coming into cont_text instead of cont_hexa ( its only normal comments in body not attachments)
Could you please help me to fix this issue, i serched many did't get any clue.
Using the below replacement statement will get rid of those useless texts. Please have a try.
Thanks for immediate reply,
In my case the data is coming into cont_text internal table in that html format instead of cont_hex
Unable to convert SOLI data into byte , if i tried with character mode it is coming in different format.
i need to get only Rejecting as amount is wrong so that i can update in transaction notes section.
Please help me if you get any thoughts.