Wednesday, May 30, 2012

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

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.
·         Create a z report program.
·         Get the image path from MIME repository (SE80): '/SAP/PUBLIC/image.jpg' (Path of image in mime repository which need to be embedded)
·         Get the binary content of the image.
  DATA: o_mr_api  type ref to if_mr_api,
                 is_folder  type boole_d,
                 l_current type xstring,
                 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 
·         Convert the binary image data into Base64.
  CALL FUNCTION 'SSFC_BASE64_ENCODE'
    EXPORTING
      bindata                        = l_current
   IMPORTING
     b64data                        = b64data.
  IF sy-subrc <> 0.  
  ENDIF.
·         Create mail body using HTML.
     In email body, the image will be displayed using its binary content.
    DATA lv_length TYPE I,
                 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,
              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.
·         Send Mail.
           DATA : lt_mail_body TYPE soli_tab,
         lwa_mail_body LIKE LINE OF lt_mail_body.

  l_subject = 'Test : Image in mail'.

  lr_email_body = cl_document_bcs=>create_document(
                                   i_type = 'HTM'
                                   i_text = gt_mail_body
                                   i_subject = l_subject ).

  lr_email = cl_bcs=>create_persistent( ).
  lr_email->set_document( lr_email_body ).

  l_mail_address = 'test@test.com'.

  lr_receiver = cl_cam_address_bcs=>create_internet_address( l_mail_address ).
  lr_email->add_recipient( i_recipient = lr_receiver ).

  "Set Sender and send mail
  l_sender = cl_sapuser_bcs=>create( sy-uname ).
  lr_email->set_sender( l_sender ).
  lr_email->set_send_immediately( 'X' ).  "Send email directly
  l_send_result = lr_email->send( i_with_error_screen = 'X' ).

  COMMIT WORK.
Output:
Go to SAP inbox check mail for the image embedded in the mail.

5 comments:

  1. Ram Kishore PasalaJune 20, 2018 at 2:44 AM

    I sued the same code, but could not able to view image in SOST and outlook. Could u let me know the solution. Thank you.

    ReplyDelete
  2. what is the type of gt_mail_body??

    ReplyDelete
  3. lr_email_body??? how to define lr_email_body?

    ReplyDelete