Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

Author: Devendra R Paralkar
Submitted: July 6, 2007
Related Links:

Description

In many cases it is required to send notifications/messages to Users depending upon some event/action that has occurred in SAP. There are standard function modules available in SAP for achieving this functionality. Here we will discuss in addition to sending mails how we can achieve text formatting using a combination of powerful SAP function modules and simple HTML tags.

Program Logic

SAP has provided us many function modules for this purpose. We will use "SO_DOCUMENT_SEND_API1" in our case. One of the parameters that need to be passed to this Function is the structure document_data. Here we have a field doc_type, in order to have required text formatting we pass this parameter as 'HTM'. By using this extension we can format the body of this mail using HTML tags.

REPORT Z_SEND_HTML_MAIL .

* Selection screen: enter receiver (default is yourself)
PARAMETERS p_rec TYPE so_recname OBLIGATORY DEFAULT sy-uname.
PARAMETERS p_typ TYPE so_escape OBLIGATORY DEFAULT 'B'. "sap user

* Data Declaration
DATA: wa_docdata TYPE sodocchgi1,
      wa_objpack TYPE sopcklsti1 ,
      it_objpack TYPE TABLE OF sopcklsti1,
      wa_objtxt TYPE solisti1,
      it_objtxt TYPE TABLE OF solisti1,
      wa_reclist TYPE somlreci1,
      it_reclist TYPE TABLE OF somlreci1.
DATA: tab_lines TYPE i.

* Start program
PERFORM fill_text.
PERFORM fill_object_details.
*create receiver list
PERFORM create_receivers_list.
*send mail
PERFORM send_mail.
WRITE: / 'End of Program'.
*---------------------------------------------------------------------*
*Form fill_text
*---------------------------------------------------------------------*
FORM fill_text .
  wa_objtxt = '<SPAN style="font-family:arial;font-size:10.5pt;color:blue">'.
  APPEND wa_objtxt TO it_objtxt.
  wa_objtxt = '<B><U>Test Program for Send Mail.</U></B> <BR>'.
  APPEND wa_objtxt TO it_objtxt.
  wa_objtxt = '<A HREF="http://www.sap.com">Best wishes</A>'.
  APPEND wa_objtxt TO it_objtxt.
  wa_objtxt = '<I>This code is formatted using HTML.</I>'.
  APPEND wa_objtxt TO it_objtxt.
  wa_objtxt = 'Have a nice day.'.
  APPEND wa_objtxt TO it_objtxt.
  wa_objtxt = '<SPAN>'.
  APPEND wa_objtxt TO it_objtxt.
ENDFORM. " fill_text
*---------------------------------------------------------------------*
*Form fill_object_details
*---------------------------------------------------------------------*
FORM fill_object_details .
  DESCRIBE TABLE it_objtxt LINES tab_lines.
  READ TABLE it_objtxt INTO wa_objtxt INDEX tab_lines.
  wa_docdata-obj_name = 'TEST_HTML'.
  wa_docdata-obj_descr = 'Test for HTML Code'.
  wa_docdata-doc_size = ( tab_lines - 1 ) * 255 + STRLEN( wa_objtxt ).
  CLEAR wa_objpack-transf_bin.
  wa_objpack-head_start = 1.
  wa_objpack-head_num = 0.
  wa_objpack-body_start = 1.
  wa_objpack-body_num = tab_lines.
  wa_objpack-doc_type = 'HTM'.
  APPEND wa_objpack TO it_objpack.
ENDFORM. " fill_object_details
*---------------------------------------------------------------------*
*Form create_receivers_list
*---------------------------------------------------------------------*
FORM create_receivers_list .
  wa_reclist-receiver = p_rec.
  wa_reclist-rec_type = p_typ.
  APPEND wa_reclist TO it_reclist.
ENDFORM. " create_receivers_list
*---------------------------------------------------------------------
*Form send_mail
*---------------------------------------------------------------------\
FORM send_mail .
  CALL FUNCTION 'SO_DOCUMENT_SEND_API1'
    EXPORTING
      document_data              = wa_docdata
      put_in_outbox              = ' '
      commit_work                = 'X'
    TABLES
      packing_list               = it_objpack
      contents_txt               = it_objtxt
      receivers                  = it_reclist
    EXCEPTIONS
      too_many_receivers         = 1
      document_not_sent          = 2
      document_type_not_exist    = 3
      operation_no_authorization = 4
      parameter_error            = 5
      x_error                    = 6
      enqueue_error              = 7
      OTHERS                     = 8.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE 'S' NUMBER sy-msgno
    WITH sy-msgv1.
  ELSE.
    WRITE : / 'Mail sent to', p_rec.
  ENDIF.
ENDFORM. " send_mail