Skip to Content
Technical Articles
Author's profile photo Jigang Zhang 张吉刚

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:
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!
For my case, I don’t require the figure embedded in the email body which should be skipped by using REGEX ‘\[[^\s]+\]’ to remove pattern [*].

"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.

How to debug inbound email?

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.
Or check details of Troubleshooting Synchronous Offline Approvals at this blog. Finally, please let me know if you know how to separate user pure comments from an email’s complex signature, Thanks!

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.

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.