Multipart Emails
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 😛
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 🙁
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.
Hi,
Tabular data can be sent as excel attachments in an email using BCS classes with some plain text email body. You can use CL_SALV_TABLE to generate report and use to_xml() method to export and attach the content directly to email without saving them to disk.
I don't know much about sending email in HTML format.
Thanks.
An interesting approach...
Would you please consider running the spellchecker for the text (there is a button in the blog editor)? Thanks so much!
🙂 Hi Jelena,
Did I make any mistake? My first language is not English.
I made some changes in the comment.
The comment was meant for OP and general public, not specifically for you.
Unfortunately the spell checker won't be able to spot contextual error like "just past it here".
Manish got it right - the comment was for the OP (and I see he followed it already - much better now! 🙂 ).
I bet for most of the community members first language is not English. The text with many spelling errors would be even more difficult to understand and using the spellchecker resolves this problem (it's even mentioned in this document under Start Writing). But I'm guilty myself of forgetting this step sometimes. 😳 I wish there was a more discrete way of pointing this out to the author but unfortunately we can only send direct messages when we follow each other.
I know, I could be able to test it at the system, but I'd like to know - does this multipart email show (e.g. in outlook)? Does it know that it should show only html if it is available and only the text if it is not? Sorry for this question, but I quite did not understand
You are delivering two parts in one email - called multipart/alternative. Depending how Outlook or any other EMailreader is configured it displays either the plain text part or the html part. I tested this succesfully with outlook, thunderbird and roundcube. Roundcube is a web based email reader. As far as i know outlook has this option only for composing messages, its always displaying the HTML part if available.