E-MAIL

Send HTML Email with Image in ABAP

This article demonstrates how to send an HTML email with an embedded image from the MIME repository using ABAP. Both the processing of the content, the integration of the image via Content-ID, and the transmission via BCS are explained step-by-step.

Sending HTML emails from ABAP is well documented using the cl_gbt_multirelated_service class. Less obvious is how to embed an image directly from the MIME Repository – without an external URL and without manual file storage. This article shows the complete workflow: from loading the image object and preparing the data to the completed email dispatch with an embedded image.

Program

The following ABAP program demonstrates the complete workflow in a standalone report. It can be used as a template for custom implementations – such as in classes, forms, or background processing programs.

Show Program Code (68 lines)
REPORT zstkoes_send_html_mail.
 
DATA(lo_mime_helper) = NEW cl_gbt_multirelated_service( ).
 
" Get MIME-Repository Object
DATA(gv_filename) = |ABDOC_SOC_bad_DE.jpg|.
cl_mime_repository_api=>get_api( )->get(
  EXPORTING
    i_url                  = |SAP/PUBLIC/BC/ABAP/{ gv_filename }|
    i_check_authority      = abap_true
  IMPORTING
    e_content              = DATA(gv_content)
    e_mime_type            = DATA(gv_mime_type)
  EXCEPTIONS
    parameter_missing      = 1
    error_occured          = 2
    not_found              = 3
    permission_failure     = 4
    OTHERS                 = 5 ).
 
IF sy-subrc EQ 0 AND gv_content IS NOT INITIAL.
*... split xstring into lt_solix table (which is need for email)
  DATA(lt_solix) = VALUE solix_tab( LET lv_total_length = xstrlen( gv_content ) lv_max_off = lv_total_length - 255 IN
                                    FOR lv_off = 0 THEN lv_off + 255  UNTIL lv_off GT lv_total_length
                                    ( line = COND #( WHEN lv_off LE lv_max_off
                                                     THEN gv_content+lv_off(255)
                                                     ELSE gv_content+lv_off ) ) ).
*Attach image to HTML body
  DATA(gv_obj_len) = xstrlen( gv_content ).
 
  lo_mime_helper->add_binary_part( content      = lt_solix
                                   content_type = CONV #( gv_mime_type )
                                   length       = CONV #( gv_obj_len )
                                   content_id   = gv_filename ).
ENDIF.
 
*... create the HTML content for the body of the email
DATA(lt_soli) = VALUE soli_tab(
  ( |<html xmlns="http://www.w3.org/1999/xhtml" xmlns:xfa="http://www.xfa.org/schema/xfa-template/2.1/"><head></head>| )
  ( |<body>| )
  ( |<p>Manchmal ist es notwendig ein Bild in eine E-Mail einzubinden.</p>| )
  ( COND #( WHEN lt_solix IS NOT INITIAL
            THEN |<p><img alt="logo" src="cid:{ gv_filename }" /></p>| ) )
  ( |<p>Und hier ist, wie es funktioniert.</p>| )
  ( |</body></html>| ) ).
 
*... set the HTML with description
lo_mime_helper->set_main_html( content     = lt_soli
                               filename    = 'message.htm'      " filename for HMTL form
                               description = 'Email message' ). " Title
 
"Create HTML using BCS class and attach html and image part to it.
DATA(lo_doc_bcs) = cl_document_bcs=>create_from_multirelated( i_subject          = 'E-Mail mit MIME-Repository Bild'
                                                              i_multirel_service = lo_mime_helper ).
 
DATA(lo_bcs) = cl_bcs=>create_persistent( ).
 
"Create Document
lo_bcs->set_document( i_document = lo_doc_bcs ).
 
* Set sender
lo_bcs->set_sender( cl_cam_address_bcs=>create_internet_address( 'abap@koester-consulting.eu' ) ).
 
"Set Recipient
lo_bcs->add_recipient( cl_cam_address_bcs=>create_internet_address( 'abap@koester-consulting.eu' ) ).
 
lo_bcs->send( ).
COMMIT WORK.

Workflow Details

Loading the Image from the MIME Repository

The desired image object is loaded based on its path in the MIME Repository via the cl_mime_repository_api class. The method returns both the binary content (gv_content as XSTRING) and the associated MIME type – e.g., image/jpeg. Both values are required for embedding the image into the email later.

Data Preparation: Converting XSTRING to SOLIX

The BCS dispatch framework expects the image content in the solix_tab format – an internal table with rows of 255 bytes each. The XSTRING content is therefore split into individual chunks and transferred row by row into the table. This conversion step is mandatory so that the image can be correctly attached to the email as a Binary Part.

Content-ID: The Key to Image Linking

When adding the image as a Binary Part, a Content-ID (content_id) is assigned – in this example, the filename of the image. This ID is the key to referencing the image within the HTML body. In the HTML tag, the linkage is established using the cid: prefix:

<img src="cid:ABDOC_SOC_bad_DE.jpg">

If the content_id matches the cid value in the HTML exactly, the image will be displayed directly inside the email – without any external linking.

Dispatch via BCS

The finished multirelated document is instantiated via cl_document_bcs and integrated into a BCS instance. After configuring the sender and recipient, lo_bcs->send() triggers the transmission. Important: Only the concluding COMMIT WORK statement actually transfers the email to the SAP mail server.

Email

Figure 1: Successfully received HTML email showcasing graphic objects rendered directly via Content-ID evaluation.

Back to Blog Overview