SAP/ABAP

sending SAP Office Mail ( SO01 )

Denise 2015. 9. 1. 16:54
FUNCTION zmgg_send_office_mail.
*"----------------------------------------------------------------------
*"*"Local interface:
*"  IMPORTING
*"     REFERENCE(IV_SENDER) TYPE  SYUNAME
*"     REFERENCE(IV_OBJNM) TYPE  SO_OBJ_NAM DEFAULT 'OFFICE_MAIL'
*"     REFERENCE(IV_TITLE) TYPE  SO_OBJ_DES
*"     REFERENCE(IT_TEXT) TYPE  TTTEXT255
*"     REFERENCE(IT_RECEIVER) TYPE  SPERS_ULST
*"  EXCEPTIONS
*"      MAIL_ERROR
*"----------------------------------------------------------------------

  CONSTANTS : lc_highest TYPE so_obj_pri VALUE '1'.

  DATA : ls_folder    TYPE soodk,
         ls_folder_rc TYPE soodk,
         ls_header    TYPE sood1,
         lv_obj_type  TYPE soodk-objtp,
         ls_object    TYPE soodk.

  DATA : lt_objcont  TYPE TABLE OF soli  WITH HEADER LINE,
         lt_objhead  TYPE TABLE OF soli  WITH HEADER LINE,
         lt_objpara  TYPE TABLE OF selc  WITH HEADER LINE,
         lt_objparb  TYPE TABLE OF soop1 WITH HEADER LINE.

  DATA : ls_text     LIKE LINE OF it_text,
         ls_receiver LIKE LINE OF it_receiver.
  DATA : lt_receiver TYPE TABLE OF soos1 WITH HEADER LINE.

  DATA : ls_user_send TYPE souc,
         ls_user_recv TYPE souc.

  DATA : lv_message   TYPE char255.

*--- Get Office Sender info.
  PERFORM get_office_user USING    iv_sender
                          CHANGING ls_user_send.

*--- Get Folder info.
  PERFORM get_folder_info USING    ls_user_send-usrtp
                                   ls_user_send-usryr
                                   ls_user_send-usrno
                          CHANGING ls_folder.

  CHECK ls_folder IS NOT INITIAL.

  lv_obj_type = 'RAW'.

  ls_header-objla  = sy-langu.
  ls_header-objnam = iv_objnm.
  ls_header-objdes = iv_title.

*--- Expiration Date
*  ls_header-dldat  = sy-datum.
*  ls_header-dltim  = sy-uzeit.

  ls_header-objpri = lc_highest.
  ls_header-objsns = 'F'.

*--- Set Content text.
  LOOP AT it_text INTO ls_text.
    lt_objcont-line = ls_text.
    APPEND lt_objcont.
    CLEAR  lt_objcont.
  ENDLOOP.

  LOOP AT it_receiver INTO ls_receiver.

*--- Get Office Sender info.
    PERFORM get_office_user USING    ls_receiver
                            CHANGING ls_user_recv.
    CHECK ls_user_recv IS NOT INITIAL.

    lt_receiver-rcdat  = sy-datum.
    lt_receiver-rctim  = sy-uzeit.
    lt_receiver-recnam = ls_user_recv.
    lt_receiver-sndtp  = ls_user_send-usrtp.
    lt_receiver-sndyr  = ls_user_send-usryr.
    lt_receiver-sndno  = ls_user_send-usrno.
    lt_receiver-sndnam = iv_sender.

    lt_receiver-recno  = ls_user_recv-usrno.
    lt_receiver-rectp  = ls_user_recv-usrtp.
    lt_receiver-recyr  = ls_user_recv-usryr.

    lt_receiver-sndpri  = lc_highest.
    lt_receiver-deliver = 'X'.
*    lt_receiver-sndex   = 'X'. " Send as Express
    lt_receiver-read    = 'X'.
    lt_receiver-adr_name = ls_receiver.
    APPEND lt_receiver.
    CLEAR  lt_receiver.
  ENDLOOP.

  CHECK lt_receiver[] IS NOT INITIAL.
  CALL FUNCTION 'SO_OBJECT_SEND'
    EXPORTING
       folder_id                  = ls_folder
       object_hd_change           = ls_header
       object_id                  = ls_object
       object_type                = lv_obj_type
       outbox_flag                = 'X'
       owner                      = iv_sender
    TABLES
       objcont                    = lt_objcont
       receivers                  = lt_receiver
    EXCEPTIONS
       active_user_not_exist      = 1
       communication_failure      = 2
       component_not_available    = 3
       folder_not_exist           = 4
       folder_no_authorization    = 5
       forwarder_not_exist        = 6
       note_not_exist             = 7
       object_not_exist           = 8
       object_not_sent            = 9
       object_no_authorization    = 10
       object_type_not_exist      = 11
       operation_no_authorization = 12
       owner_not_exist            = 13
       parameter_error            = 14
       substitute_not_active      = 15
       substitute_not_defined     = 16
       system_failure             = 17
       too_much_receivers         = 18
       user_not_exist             = 19
       x_error                    = 20
       OTHERS                     = 21.

  IF sy-subrc IS NOT INITIAL.
    CALL FUNCTION 'MESSAGE_TEXT_BUILD'
      EXPORTING
        msgid               = sy-msgid
        msgnr               = sy-msgno
        msgv1               = sy-msgv1
        msgv2               = sy-msgv2
        msgv3               = sy-msgv3
        msgv4               = sy-msgv4
      IMPORTING
        message_text_output = lv_message.
  ENDIF.

  IF sy-msgid IS INITIAL.
    CASE sy-subrc.
      WHEN 1.  lv_message = 'active_user_not_exist     '.
      WHEN 2.  lv_message = 'communication_failure     '.
      WHEN 3.  lv_message = 'component_not_available   '.
      WHEN 4.  lv_message = 'folder_not_exist          '.
      WHEN 5.  lv_message = 'folder_no_authorization   '.
      WHEN 6.  lv_message = 'forwarder_not_exist       '.
      WHEN 7.  lv_message = 'note_not_exist            '.
      WHEN 8.  lv_message = 'object_not_exist          '.
      WHEN 9.  lv_message = 'object_not_sent           '.
      WHEN 10. lv_message = 'object_no_authorization   '.
      WHEN 11. lv_message = 'object_type_not_exist     '.
      WHEN 12. lv_message = 'operation_no_authorization'.
      WHEN 13. lv_message = 'owner_not_exist           '.
      WHEN 14. lv_message = 'parameter_error           '.
      WHEN 15. lv_message = 'substitute_not_active     '.
      WHEN 16. lv_message = 'substitute_not_defined    '.
      WHEN 17. lv_message = 'system_failure            '.
      WHEN 18. lv_message = 'too_much_receivers        '.
      WHEN 19. lv_message = 'user_not_exist            '.
      WHEN 20. lv_message = 'Internal error            '.
    ENDCASE.
  ENDIF.

  IF lv_message IS NOT INITIAL.
    MESSAGE lv_message TYPE 'S' RAISING mail_error.
  ENDIF.

ENDFUNCTION.
*----------------------------------------------------------------------*
*   GET_FOLDER_INFO
*----------------------------------------------------------------------*
FORM get_office_user USING    pv_uname
                     CHANGING ps_user TYPE souc.

  CLEAR : ps_user.
  SELECT SINGLE
       usrtp
       usryr
       usrno
  INTO CORRESPONDING FIELDS OF ps_user
  FROM souc AS s
 WHERE s~sapnam EQ pv_uname.

  IF sy-subrc IS NOT INITIAL.
    EXIT.
  ENDIF.

ENDFORM.
*----------------------------------------------------------------------*
*   GET_FOLDER_INFO
*----------------------------------------------------------------------*
FORM get_folder_info USING    pv_usrtp
                              pv_usryr
                              pv_usrno
                     CHANGING ps_folder TYPE soodk.

  SELECT SINGLE
       f~foltp AS objtp
       f~folyr AS objyr
       f~folno AS objno
  INTO CORRESPONDING FIELDS OF ps_folder
  FROM sofd AS f
 WHERE f~owntp  EQ pv_usrtp
   AND f~ownyr  EQ pv_usryr
   AND f~ownno  EQ pv_usrno
   AND f~folrg  EQ 'O'.

  IF sy-subrc IS NOT INITIAL.
    EXIT.
  ENDIF.

ENDFORM.

# Sample code.

  DATA : lv_title TYPE so_obj_des.
  DATA : lt_text  TYPE tttext255  WITH HEADER LINE.
  DATA : lt_recv  TYPE spers_ulst WITH HEADER LINE.

*--- 제목
  lv_title = '테스트 메일' .

*--- 내용.
  APPEND '테스트 내용' TO lt_text.

*--- 수신자.
    lt_recv = sy-uname.
    APPEND lt_recv.

  READ TABLE lt_zmgbiwfd INDEX 1.
  CALL FUNCTION 'ZMGG_SEND_OFFICE_MAIL'
    EXPORTING
      iv_sender         = sy-uname
      iv_title          = lv_title
      it_text           = lt_text[]
      it_receiver       = lt_recv[].

'SAP > ABAP' 카테고리의 다른 글

Material Characteristics Basic 2 / MRP 3  (0) 2016.03.09
CTS Table length check Function.  (0) 2015.11.25
Long Text 처리  (0) 2015.08.11
Check if Function call is RFC or Not.  (0) 2015.07.09
Set initial Screen Variant for Report  (0) 2014.12.03