Skip to content

Instantly share code, notes, and snippets.

@weiserman
Created February 18, 2014 06:54
Show Gist options
  • Save weiserman/9065846 to your computer and use it in GitHub Desktop.
Save weiserman/9065846 to your computer and use it in GitHub Desktop.
This is an example ABAP program which sends an HTML email from SAP system. It includes an image which is uploaded via transaction SMW0
*&---------------------------------------------------------------------*
*& Report YMAIL
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ymail.
"Image to Xstring Table form
DATA : lv_obj_len TYPE so_obj_len,
lv_graphic_length TYPE tdlength,
gr_xstr TYPE xstring,
lv_offset TYPE i,
lv_length TYPE i,
lv_diff TYPE i,
ls_solix TYPE solix,
lt_solix TYPE solix_tab.
*Attach image to HTML body
DATA: lv_filename TYPE string,
lv_content_id TYPE string,
lt_soli TYPE soli_tab,
ls_soli TYPE soli.
*Class for cobining HMTL & Image
DATA : lo_mime_helper TYPE REF TO cl_gbt_multirelated_service.
*BCS class for sending mail
DATA: lo_bcs TYPE REF TO cl_bcs,
lo_doc_bcs TYPE REF TO cl_document_bcs,
lo_sender TYPE REF TO if_sender_bcs,
lo_recipient TYPE REF TO if_recipient_bcs,
lv_subject TYPE so_obj_des.
* Data Declaration
DATA: lfd_xstring TYPE xstring,
lfd_objid TYPE w3objid,
lv_obkey TYPE wwwdatatab,
lt_mime_raw TYPE STANDARD TABLE OF w3mime,
ls_mime_raw TYPE w3mime.
* Import the Image File, these are maintained in transaction SMW0
lv_obkey-objid = 'ZUCT_ROUND_LOGO'.
lv_obkey-relid = 'MI'.
CALL FUNCTION 'WWWDATA_IMPORT'
EXPORTING
key = lv_obkey
TABLES
mime = lt_mime_raw
EXCEPTIONS
wrong_object_type = 1
import_error = 2
OTHERS = 3.
*... build a long xstring
IF sy-subrc EQ 0.
LOOP AT lt_mime_raw INTO ls_mime_raw.
CONCATENATE lfd_xstring ls_mime_raw-line INTO lfd_xstring IN BYTE MODE.
ENDLOOP.
ENDIF.
*... split xstring into lt_solix table (which is need for email)
CLEAR gr_xstr.
lv_obj_len = xstrlen( lfd_xstring ).
lv_graphic_length = xstrlen( lfd_xstring ).
gr_xstr = lfd_xstring(lv_obj_len).
lv_offset = 0.
lv_length = 255.
CLEAR lt_solix[].
WHILE lv_offset < lv_graphic_length.
lv_diff = lv_graphic_length - lv_offset.
IF lv_diff > lv_length.
ls_solix-line = gr_xstr+lv_offset(lv_length).
ELSE.
ls_solix-line = gr_xstr+lv_offset(lv_diff).
ENDIF.
APPEND ls_solix TO lt_solix.
ADD lv_length TO lv_offset.
ENDWHILE.
*Attach image to HTML body
lv_filename = 'UCT_LOGO.gif'.
lv_content_id = 'UCT_LOGO.gif'.
CREATE OBJECT lo_mime_helper.
CALL METHOD lo_mime_helper->add_binary_part
EXPORTING
content = lt_solix "Xstring in table form
filename = lv_filename "file name to be given to image
extension = 'GIF' "type of file
description = 'Graphic in GIF format' "description
content_type = 'image/gif' "content type / Mime type. If mime type not present in system then need to add through tcode : SMW0
length = lv_obj_len "length of image
content_id = lv_content_id. "content id would be used in html part
*... create the HTML content for the body of the email
REFRESH lt_soli[].
CLEAR ls_soli.
ls_soli = '<html xmlns="http://www.w3.org/1999/xhtml" xmlns:xfa="http://www.xfa.org/schema/xfa-template/2.1/"><head></head>'.
APPEND ls_soli TO lt_soli.
CLEAR ls_soli.
ls_soli = '<body>'.
APPEND ls_soli TO lt_soli.
CLEAR ls_soli.
CONCATENATE '<p>Dear Warren<br>' ' ' INTO ls_soli SEPARATED BY space.
APPEND ls_soli TO lt_soli.
CLEAR ls_soli.
ls_soli = '<br><img alt="company logo" src="cid:UCT_LOGO.gif" /><br>'.
APPEND ls_soli TO lt_soli.
CLEAR ls_soli.
CONCATENATE '<br>Regards</p></body>' '</html>' INTO ls_soli SEPARATED BY space.
APPEND ls_soli TO lt_soli.
*... set the HTML with description
CALL METHOD lo_mime_helper->set_main_html
EXPORTING
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.
lv_subject = 'This is a test email for including graphics'. "subject
lo_doc_bcs = cl_document_bcs=>create_from_multirelated(
i_subject = lv_subject
i_multirel_service = lo_mime_helper ).
lo_bcs = cl_bcs=>create_persistent( ).
"Create Document
lo_bcs->set_document( i_document = lo_doc_bcs ).
"create Sender
lo_sender = cl_cam_address_bcs=>create_internet_address( 'warren.eiserman@uct.ac.za' ).
* Set sender
lo_bcs->set_sender(
EXPORTING
i_sender = lo_sender ).
"Create Recipient
lo_recipient = cl_cam_address_bcs=>create_internet_address( i_address_string = 'warren.eiserman@uct.ac.za' ).
lo_bcs->add_recipient( i_recipient = lo_recipient ).
lo_bcs->send( ).
COMMIT WORK AND WAIT.
@sureshguttula83
Copy link

sureshguttula83 commented Feb 23, 2018

Image not appears in mail body and moreover , converted as a attachment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment