Embedding Image in mail body instead of sending it as an attachment

By Varsha Avtani, L&T InfoTech

Solution:

Get the path of image residing in MIME repository, fetch the binary content of the image, convert into BASE64 data and attach the base64 data into HTML mail body to embed image.

Step1: Create a z report program.

Step 2: Get the image path from MIME repository (SE80):

'/SAP/PUBLIC/image.jpg' (Path of image in mime repository which need to be embedded)

Step 3: Get the binary content of the image.

  DATA: o_mr_api         TYPE REF TO if_mr_api.

  
DATA is_folder TYPE boole_d.
  
DATA l_current TYPE xstring.
  
DATA l_loio TYPE skwf_io.

  
IF o_mr_api IS INITIAL.

    o_mr_api = cl_mime_repository_api=>if_mr_api~get_api ( ).

  
ENDIF.

      CALL METHOD o_mr_api->get
    
EXPORTING
      i_url              = 
'/SAP/PUBLIC/image.jpg'
    
IMPORTING
      e_is_folder        = is_folder
      e_content          = l_current
      e_loio             = l_loio
    
EXCEPTIONS
      parameter_missing = 
1
      error_occured      = 
2
      not_found          = 
3
      permission_failure = 
4
      
OTHERS             = 5.

*l_current will hold the image in a XSTRING 

Step 4: Convert the binary image data into Base64.

  CALL FUNCTION 'SSFC_BASE64_ENCODE'
    
EXPORTING
      bindata                        = l_current
   
IMPORTING
     b64data                        = b64data.
  
IF sy-subrc <> 0.  

  ENDIF.

Step 5: Create mail body using HTML.

     In email body, the image will be displayed using its binary content.

    DATA lv_length TYPE i.
  
DATA lv_len2 TYPE i.


  
CLEAR wa_mail_body.
  
MOVE '<html>' TO wa_mail_body.
  
APPEND wa_mail_body TO gt_mail_body.

  
CLEAR wa_mail_body.
  
MOVE '<head>' TO wa_mail_body.
  
APPEND wa_mail_body TO gt_mail_body.

  
CLEAR wa_mail_body.
  
MOVE '<title>Hello</title>' TO wa_mail_body.
  
APPEND wa_mail_body TO gt_mail_body.

  
CLEAR wa_mail_body.
  
MOVE '<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-   1">' TO wa_mail_body.
  
APPEND wa_mail_body TO gt_mail_body.

  
CLEAR wa_mail_body.
  
MOVE '</head>' TO wa_mail_body.
  
APPEND wa_mail_body TO gt_mail_body.

  
CLEAR wa_mail_body.
  
MOVE '<body>' TO wa_mail_body.
  
APPEND wa_mail_body TO gt_mail_body.

  
CLEAR wa_mail_body.
  wa_mail_body  = 
'<em><font'  .
  
APPEND wa_mail_body TO gt_mail_body.

  
CLEAR wa_mail_body.
  wa_mail_body  = 
'color="#0000FF" size="+7" face="Arial,'.
  
APPEND wa_mail_body TO gt_mail_body.

  
CLEAR wa_mail_body.
  wa_mail_body  = 
'Helvetica, sans-serif">Test Image</font></em>'.
  
APPEND wa_mail_body TO gt_mail_body.

  
CLEAR wa_mail_body.
 *Add image Base64 content
  wa_mail_body = 
'<img src="data:image/gif;base64,'.

  
APPEND wa_mail_body TO gt_mail_body.

  CLEAR wa_mail_body.

  lv_length = 
STRLEN (b64data).
  lv_len2 = lv_length / 
255.

  wa_mail_body = b64data.

  
APPEND wa_mail_body TO gt_mail_body.

  
CLEAR wa_mail_body.
  
DATA lv_len3 TYPE i.
  
DATA: temp1 TYPE i,
        temp2 
TYPE i.
  
DO lv_len2 TIMES.
    lv_len3 = 
255 * sy-index.

    
IF lv_len3 <= lv_length.
      wa_mail_body = b64data+lv_len3.
      
IF wa_mail_body IS NOT INITIAL.
        
APPEND wa_mail_body TO gt_mail_body.
        
CLEAR wa_mail_body.
      
ELSE.
        
EXIT.
      
ENDIF.
    
ELSEif lv_len3 > lv_length.

        
EXIT.

    
ENDIF.
  
ENDDO.

  wa_mail_body = 
'"alt="Happy birthday" align="middle" width="304" height="228" />'.
  
APPEND wa_mail_body TO gt_mail_body.

  
CLEAR wa_mail_body.
  
MOVE '</body>' TO wa_mail_body.
  
APPEND wa_mail_body TO gt_mail_body.


  
CLEAR wa_mail_body.
  
MOVE '</html>' TO wa_mail_body.
  
APPEND wa_mail_body TO gt_mail_body.

Click here to continue..

Please send us your feedback/suggestions at webmaster@SAPTechnical.COM 

HomeContribute About Us Privacy Terms Of Use • Disclaimer • SafeCompanies: Advertise on SAPTechnical.COM | Post JobContact Us  

Graphic Design by Round the Bend Wizards

footer image footer image