Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
rainer_hbenthal
Active Contributor

One of our customers requested data from SAP sent to him via Email. The data was a table and the customer complained that the presentation wasn't a in tabular form. Well, that is how plain text emails are working, you are sending text and the client software decides about the presentation. You do not have control over the used font and only with a monospaced font the table will be readable. A proportional font will distort the whole layout. The next try was to use tabs (0x09) but again you do not have control about the width of a tab. The result was a distorted table again.

Attachments like excel or PDF files were out of the question, too. So i had the idea of sending HTML emails, but they should have a fall back or at least a hint if the client software is unable to show HTML mails or was configured to only show text mails. In that case the body of the email appears empty. So i looked around and was sure to find something about multipart Emails containing a plain text part as well as a HTML part. But what a surprise, i was only able to find some fragments even in SCN and some postings on how to do it in PI with JAVA.

My intention was to use to the BCS classes as we have a central class wrapping all the necessary stuff in own methods. I will show the solution as own report which works fine in our environment, it's up to you to build a method for reusing the functionality :razz:

At least you need two tables containing the plain text and the html code. Both must be of type SOLI_TAB. That might be double work if you want to a have a fall back when only the plain text is displayed. At least there should be a hint that the main content is only visible in HTML mode. Those two tables needs to combined to a multipart/alternative document which can be achieved by the class CL_GBL_MULTIRELATED_SERVICE calling the methods set_main_text and set_main_html.

Instead of binding the text to the email with create_document from CL_DOCUMENT_BCS you just call the method create_from_multirelated.

The rest is now business as usual using the BCS classes to compose an email and send it to the recipients.

It was really easier as in my first thoughts and if i miss someones blog i apologize, but i really have bad search skills :sad:

As the source is really short i just past it here.

*&---------------------------------------------------------------------*
*& Report  Z_TEST_EMAIL
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT  z_test_email.
DATA:
  subject               TYPE so_obj_des,
  it_body_text         TYPE soli_tab,
  it_body_html         TYPE soli_tab,
  from                 TYPE ad_smtpadr,
  to                   TYPE ad_smtpadr.
DATA:
  document             TYPE REF TO cl_document_bcs,
  request              TYPE REF TO cl_bcs,
  mime_helper          TYPE REF TO cl_gbt_multirelated_service,
  sender               TYPE REF TO if_sender_bcs,
  recipient            TYPE REF TO if_recipient_bcs.
to      = 'receiver@example.org'.
from    = 'sender@example.org'.
subject = 'Tabular data'.
APPEND 'Hello World'           TO it_body_text.
APPEND 'Column 1     Column 2' TO it_body_text.
APPEND 'a            b'        TO it_body_text.
APPEND 'xyz          test'     TO it_body_text.
APPEND '4711         0815'     TO it_body_text.
APPEND '<html>'                      TO it_body_html.
APPEND '<head>'                      TO it_body_html.
APPEND '<title>MY HTML part</title>' TO it_body_html.
APPEND '</head>'                     TO it_body_html.
APPEND '<body>'                      TO it_body_html.
APPEND '<h1>Hello World!</h1>'       TO it_body_html.
APPEND '<table border="1">'          TO it_body_html.
APPEND '<tr><td>Column 1</td><td>Column 2</td></tr>' TO it_body_html.
APPEND '<tr><td>a</td><td>b</td></tr>'               TO it_body_html.
APPEND '<tr><td>xyz</td><td>test</td></tr>'          TO it_body_html.
APPEND '<tr><td>4711</td><td>0815</td></tr>'         TO it_body_html.
APPEND '</table>' TO it_body_html.
APPEND '</body>'  TO it_body_html.
APPEND '</html>'  TO it_body_html.
CREATE OBJECT mime_helper.
mime_helper->set_main_text( content = it_body_text ).
mime_helper->set_main_html( content = it_body_html ).
document = cl_document_bcs=>create_from_multirelated(
  i_subject          = subject
  i_multirel_service = mime_helper ).
request = cl_bcs=>create_persistent( ).
request->set_document( document ).
IF from IS NOT INITIAL.
  sender = cl_cam_address_bcs=>create_internet_address( from ).
ELSE.
  sender = cl_sapuser_bcs=>create( sy-uname ).
ENDIF.
request->set_sender( sender ).
recipient = cl_cam_address_bcs=>create_internet_address( to ).
request->add_recipient( EXPORTING i_recipient = recipient ).
request->set_send_immediately( 'X' ).
IF request->send( i_with_error_screen = 'X' ) = 'X'.
  WRITE:/ 'Email sent succesfully'.
ELSE.
  WRITE:/ ' Error sending email'.
ENDIF.
COMMIT WORK.
7 Comments