Technology Blog Posts by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
manijangiti
Participant
628

Introduction

In many SAP projects, sending emails with proper formatting and images is a common requirement—whether it’s for birthday wishes, reminders, approvals, or dashboards. Recently, I faced a real-time scenario where I had to send an email that included an inline image stored in the SAP MIME Repository. Instead of attaching the image as a file, the business wanted it to appear directly inside the email body.

This pushed me to explore how the MIME Repository works and how to embed images using ABAP and the BCS classes. To make the explanation clear and easy to understand, I used AI-assisted content creation while writing this blog—helping me present the technical steps in a simple and readable format.

In this blog, I will walk you through the complete process of fetching an image from the MIME Repository and sending it in an HTML email using ABAP.
Example Code :

REPORT ZEMAIL_TO_SEND_IMG1.

*----------------------------------------------------------------------
* Data Declarations
*----------------------------------------------------------------------
DATA: lo_mime       TYPE REF TO cl_gbt_multirelated_service,
      lo_doc        TYPE REF TO cl_document_bcs,
      lo_bcs        TYPE REF TO cl_bcs,
      lo_sender     TYPE REF TO if_sender_bcs,
      lo_recipient  TYPE REF TO if_recipient_bcs.

DATA: lv_xstr       TYPE xstring,           "Image content in XSTRING
      lv_mime       TYPE w3conttype,        "MIME type like image/png
      lv_filename   TYPE string VALUE 'Done.png', "Image name
      lt_solix      TYPE solix_tab,         "Binary table
      lt_html       TYPE soli_tab,          "HTML body lines
      lv_len        TYPE so_obj_len.        "Length of binary content


*----------------------------------------------------------------------
* Step 1: Read Image From SAP MIME Repository
*----------------------------------------------------------------------
cl_mime_repository_api=>get_api( )->get(
  EXPORTING
    i_url       = |/SAP/PUBLIC/TEST_W/{ lv_filename }|
  IMPORTING
    e_content   = lv_xstr
    e_mime_type = lv_mime
  EXCEPTIONS
    OTHERS      = 1 ).

IF sy-subrc <> 0 OR lv_xstr IS INITIAL.
  MESSAGE |Image { lv_filename } missing in MIME Repo!| TYPE 'E'.
ENDIF.


*----------------------------------------------------------------------
* Step 2: Convert XSTRING Image to SOLIX
*----------------------------------------------------------------------
lt_solix = cl_bcs_convert=>xstring_to_solix( lv_xstr ).
lv_len   = xstrlen( lv_xstr ).


*----------------------------------------------------------------------
* Step 3: Create MIME Multi-Part Object for Email
*----------------------------------------------------------------------
CREATE OBJECT lo_mime.


*----------------------------------------------------------------------
* Step 4: Add Inline Image (Content-ID Based Embedding)
*----------------------------------------------------------------------
lo_mime->add_binary_part(
  content      = lt_solix,
  content_type = lv_mime,
  length       = lv_len,
  content_id   = lv_filename ).   "Used as cid:Done.png in HTML


*----------------------------------------------------------------------
* Step 5: Build HTML Email Body
*----------------------------------------------------------------------
APPEND '<html>' TO lt_html.
APPEND '<body style="background-color:#e6f0fa; font-family:Arial; margin:0; padding:10px;">' TO lt_html.

APPEND '<div style="max-width:600px; margin:auto; background:#ffffff; padding:20px; border-radius:8px; border:1px solid #d0d7e3;">' TO lt_html.

APPEND '<div style="text-align:center; margin-bottom:15px;">' TO lt_html.
APPEND |<img src="cid:{ lv_filename }" alt="Birthday Image" style="width:80px;" />| TO lt_html.
APPEND '</div>' TO lt_html.

APPEND '<p style="font-size:12px; color:#000;">Dear <b>Mani JANGITI</b>,<br><br>' TO lt_html.
APPEND 'Wishing you a very Happy Birthday!<br><br>' TO lt_html.
APPEND 'May your day be filled with joy, laughter, and wonderful moments.<br>' TO lt_html.
APPEND 'May this new year of your life bring you happiness, success, good health, and all the blessings you deserve.<br><br>' TO lt_html.
APPEND 'Enjoy your special day to the fullest! <br><br>' TO lt_html.
APPEND '</p>' TO lt_html.

APPEND '<div style="text-align:center; margin:20px 0;">' TO lt_html.
APPEND '<a href="https://your-gift-link.com/open" style="background:#6a2ae8; color:#ffffff; padding:10px 20px; text-decoration:none; border-radius:6px; font-size:14px;">' TO lt_html.
APPEND 'Open Your Gift' TO lt_html.
APPEND '</a>' TO lt_html.
APPEND '</div>' TO lt_html.

APPEND '<p style="margin-top:15px; font-size:12px;">Warm Regards,<br> Company (IN)</p>' TO lt_html.

APPEND '</div>' TO lt_html.
APPEND '</body></html>' TO lt_html.


*----------------------------------------------------------------------
* Step 6: Attach HTML Body into MIME Object
*----------------------------------------------------------------------
lo_mime->set_main_html(
  content     = lt_html,
  filename    = 'email.htm',
  description = 'Birthday Wishes' ).


*----------------------------------------------------------------------
* Step 7: Create Email Document with Subject
*----------------------------------------------------------------------
lo_doc = cl_document_bcs=>create_from_multirelated(
            i_subject          = 'Birthday Wishes...',
            i_multirel_service = lo_mime ).


*----------------------------------------------------------------------
* Step 8: Create and Send Email Using BCS
*----------------------------------------------------------------------
lo_bcs = cl_bcs=>create_persistent( ).
lo_bcs->set_document( lo_doc ).

lo_sender = cl_sapuser_bcs=>create( sy-uname ).
lo_bcs->set_sender( lo_sender ).

lo_recipient = cl_cam_address_bcs=>create_internet_address(
                 'maniyadav2585@gmail.com' ).
lo_bcs->add_recipient( lo_recipient ).

lo_bcs->send( i_with_error_screen = abap_true ).
COMMIT WORK AND WAIT.


*----------------------------------------------------------------------
* Final Message
*----------------------------------------------------------------------
WRITE / 'Email sent successfully Check It in SOST. !'.

Output :
image.png


Explanation :

  1. In SAP emails, images help make the message look professional and easy to understand.

  2. The image is usually stored in the MIME Repository, which is SAP’s storage for logos, icons, and media files.

  3. When sending HTML emails, we convert the image to Base64 so that it appears directly inside the email body.

  4. We also add hyperlinks in the email so the user can click and open related documents or applications (like a Fiori app or a PDF).

  5. This method is often used in real-time scenarios like reports, dashboards, notifications, and approval emails.

Thanks for reading this blog! Implementing images and hyperlinks in SAP emails may look small, but it makes a huge difference in real-time business communication. I personally used AI tools to structure and refine this content, just like how we now use AI daily to speed up development and solve problems faster. In real project scenarios, features like embedded images, clickable links, and clean formatting help users quickly understand data and take action. Hope this guide helps you in your next SAP requirement!
 





1 Comment
abo
Active Contributor

I'd remove the hardcoded email address in your demo code, for your own good. 🙂