notice
SAP Analysts! If you haven’t noticed yet there is a new banner ad above. It is for SAP Analyst positions with Johns Manville. Click on the ad to apply.

Examples – Send Email with Attachment

January 8th, 2010 | Categories: ABAP, API | Tags: ,

In the ABAP development, we always get the requirement to send emails outside with attachment. Below is the example to send email with a SAP shortcut as an attachment.

************************************************************************
*  Program ID     : ZRPCA_EMAIL_TEST                                   *
*  Program Title  : Email Test Report                                  *
*  Created By     : XXXXX XXXXX                                        *
*  Creation Date  : 01/07/2010                                         *
*  REQ # / CRF #  : REQ # 100                                          *
*                                                                      *
*  Description    :                                                    *
*                                                                      *
*                                                                      *
*  Additional Information :                                            *
*                                                                      *
************************************************************************
*                     Modification History                             *
************************************************************************
*  Date       User ID       REQ # CRF #     Transport # / Description  *
*  ---------- ------------  -------------   -------------------------  *
*  01/07/2010 XXXXXXX       REQ 100         XXXK900003                 *
*             Initial Version                                          *
************************************************************************
REPORT ZRPCA_EMAIL_TEST.
 
CONSTANTS:  c_charx(1)  TYPE c VALUE 'X',
            c_charu(1)  TYPE c VALUE 'U',
            c_yes(3)    TYPE c VALUE 'Yes', "#EC NEEDED
            c_no(2)     TYPE c VALUE 'No'.  "#EC NEEDED
 
DATA: gv_zowner     TYPE zowner,  "Login user id of SAP Shortcut
* Comment data comes from function module CATSXT_SIMPLE_TEXT_EDITOR
      wa_comments TYPE LINE OF catsxt_longtext_itab,
      it_comments TYPE catsxt_longtext_itab.
 
*-- Work area for all the internal tables used for Sending Mail
DATA: wa_packing_list TYPE sopcklsti1,
      wa_contents_txt TYPE solisti1,
      wa_reclist      TYPE somlreci1.
 
*-- Variables used for Sending Mail
DATA: gv_smtp_addr  TYPE adr6-smtp_addr,
      sender        TYPE soextreci1-receiver,
      send_adr_type TYPE so_adr_typ.
 
DATA: gv_count    TYPE sy-index,
      gv_line_no  TYPE sy-index.              "#EC NEEDED
 
*-- MAIL related Internal tables
DATA: wa_doc_chng     TYPE sodocchgi1,                   " document attributes
      it_packing_list TYPE STANDARD TABLE OF sopcklsti1, " attachment table
      it_contents_txt TYPE STANDARD TABLE OF solisti1,   " object text
      it_reclist      TYPE STANDARD TABLE OF somlreci1.  " mail recipients
 
* For GUI shortcut attachment
  DATA: it_shortcut TYPE soli_tab.
 
* Initialize some data
  gv_zowner = 'BRUCE.LI'.
  gv_smtp_addr = 'bruce.li@receiver.com'.
  sender = 'andy@sender.com'.
  send_adr_type = 'SMTP'.
 
**Put the Email ID of approver in list of Recepients of Notification Email.
* Get the mail Id for Purchase Group Owner
 
  CLEAR: wa_reclist, it_reclist.
 
*-- Populate Mail Id's
  wa_reclist-rec_type = c_charu. "U = Internet address
  wa_reclist-express  = c_charx. "Send express "X = Value for activated
  wa_reclist-receiver = gv_smtp_addr.
  APPEND wa_reclist TO it_reclist.
 
  CLEAR: gv_smtp_addr, wa_reclist.
 
*-- If receiver table is not initial
  CHECK it_reclist[] IS NOT INITIAL.
*--To check Mail ID's internal table should not be Empty.
*--populate document attributes
 
*-- Mail subject line
  CLEAR: wa_doc_chng.
  wa_doc_chng-obj_name = 'HEADING'.
  wa_doc_chng-obj_descr = 'How is going?'.
*-- Mail body
  IF it_comments IS NOT INITIAL.
    CLEAR:  wa_contents_txt, it_contents_txt.
    wa_contents_txt = 'Comments from Requestor:'(029).
    APPEND wa_contents_txt TO it_contents_txt.
    APPEND INITIAL LINE TO it_contents_txt.
 
    LOOP AT it_comments INTO wa_comments.
      wa_contents_txt = wa_comments.
      APPEND wa_contents_txt TO it_contents_txt.
    ENDLOOP.
    APPEND INITIAL LINE TO it_contents_txt.
  ENDIF.
 
  wa_contents_txt = 'This is a system generated please do not reply'(030).
  APPEND wa_contents_txt TO it_contents_txt.
  APPEND INITIAL LINE TO it_contents_txt.
 
*-- Document size
  CLEAR: gv_line_no, gv_count.
 
  DESCRIBE TABLE it_contents_txt LINES gv_count.
  gv_line_no = gv_count.
  READ TABLE it_contents_txt INTO wa_contents_txt INDEX gv_count.
  wa_doc_chng-doc_size = ( gv_count - 1 ) * 255 + STRLEN( wa_contents_txt ).
 
*-- Populate packing list for body text
  wa_packing_list-head_start  = 1.
  wa_packing_list-head_num    = 0.
  wa_packing_list-body_start  = 1.
  wa_packing_list-body_num    = gv_count."v_table_lines.
  wa_packing_list-doc_type    = 'RAW'.
  APPEND wa_packing_list TO it_packing_list.
  CLEAR wa_packing_list.
 
*--Create Shortcut to be added to mail,Clicking which user can
*--Login into SAP
 
  CLEAR: it_shortcut.
 
  CALL FUNCTION 'SWN_CREATE_SHORTCUT'
    EXPORTING
      i_transaction           = 'SO01' "TO take user to Inbox directly
      i_sysid                 = sy-sysid
      i_client                = sy-mandt
      i_user                  = gv_zowner
      i_language              = sy-langu
      i_windowsize            = 'Normal Window' "#EC NOTEXT
    IMPORTING
      shortcut_table          = it_shortcut "Shortcut(link) created
    EXCEPTIONS
      inconsistent_parameters = 1
      OTHERS                  = 2.
 
  IF sy-subrc EQ space.
    APPEND LINES OF it_shortcut TO it_contents_txt.
  ENDIF.
 
  DESCRIBE TABLE it_shortcut LINES gv_count.
  CLEAR it_shortcut.
 
  wa_packing_list-head_start  = 1.
  wa_packing_list-head_num    = 1.
  wa_packing_list-body_start  = gv_line_no + 1 .
  wa_packing_list-body_num    = gv_count.
  wa_packing_list-doc_type    = 'SAP'.
  wa_packing_list-doc_size    = 255 * gv_count.
  wa_packing_list-obj_name    = 'ATTACHMENT'.
  wa_packing_list-obj_descr   = 'Open SAP Inbox'(031). "Name Of the link appearing
  wa_packing_list-obj_langu   = sy-langu.
  APPEND wa_packing_list TO it_packing_list.
  CLEAR wa_packing_list.
 
*-- Sending the EMail document in given format
  CALL FUNCTION 'SO_DOCUMENT_SEND_API1'
    EXPORTING
      document_data                    = wa_doc_chng
      put_in_outbox                    = c_charx  "'X'
      sender_address                   = sender
      sender_address_type              = send_adr_type
      commit_work                      = c_charx  "'X'
    TABLES
      packing_list                     = it_packing_list
      contents_txt                     = it_contents_txt
      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 sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
VN:F [1.8.4_1055]
Rating: 5.5/10 (2 votes cast)
VN:F [1.8.4_1055]
Rating: +1 (from 1 vote)
Examples - Send Email with Attachment5.5102
Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Blogplay
  • LinkedIn
  • Live
  • MySpace
  • Twitter
  • email
  • Identi.ca
  • Print
  • Yahoo! Bookmarks
No comments yet.