Thursday, December 9, 2021

Send Email with XLS as Attachment Using ABAP

 In this Post will Show you how to Send XLS attachment via email using ABAP Code.

REPORT zemail.
*&---------------------------------------------------------------------*
*   In this Sample program will show you step by step process of preparing
*   an E-mail with attached XLS.
*
*   There are few old founction module which are used to send email.
*   But In this program will show you class concept of sending email.
*   Basically there are two class are used:
*   CL_BCS: This class for Serves as interface from BCS to applications.
*   CL_DOCUMENT_BCS: This class for attachment and also for creating
*   the body of the mail
*----------------------------------------------------------------------*

*&---------------------------------------------------------------------*
*  Local refrence variable declaration
*----------------------------------------------------------------------*
DATAlref_sender_request TYPE REF TO cl_bcs,
      lref_document       TYPE REF TO cl_document_bcs,
      lref_sender         TYPE REF TO if_sender_bcs,
      lref_recipent       TYPE REF TO if_recipient_bcs.

*&---------------------------------------------------------------------*
*   Local variable declaration
*----------------------------------------------------------------------*
DATAlv_type      TYPE so_obj_tp,
      lv_subject   TYPE so_obj_des,
      lv_length    TYPE so_obj_len,
      lv_lang      TYPE so_obj_la,
      lv_import    TYPE bcs_docimp,
      lv_recipent  TYPE adr6-smtp_addr,
      lv_item_data TYPE string,
      lv_string    TYPE string,
      lv_xstring   TYPE xstring,
      lv_out_len   TYPE i,
      lv_attc_type TYPE soodk-objtp,
      lv_attc_sub  TYPE sood-objdes.

*&---------------------------------------------------------------------*
*   Local Internal Table and structure declaration
*----------------------------------------------------------------------*
DATAlt_text       TYPE soli_tab,
      lt_attachment TYPE solix_tab,
      ls_text       TYPE soli.

*&---------------------------------------------------------------------*
*  Selection Screen
*----------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK a1 WITH FRAME TITLE text-001.
PARAMETERSp_email TYPE adr6-smtp_addr.
SELECTION-SCREEN END OF BLOCK a1.

"&... Create Persistent
TRY.
    lref_sender_request cl_bcs=>create_persistent).
  CATCH cx_send_req_bcs.  "
ENDTRY.

"&... Assign Mail document type
*& (basically we have ROH and HTM two type of document type)
lv_type 'HTM'.

"&... Assign defualt language
lv_lang sy-langu.

"&... Assign Mail importance(1-High, 5 -Average, 9 - Low)
lv_import 1.

"&... Fill Mail Body Content
ls_text-line 'Hi ABAP Code'.
APPEND ls_text TO lt_textCLEAR ls_text.

"&... using BR tag give the next line.
ls_text-line '<BR>'.
APPEND ls_text TO lt_textCLEAR ls_text.

ls_text-line '<BR>Email Attchment Testing Sample Program<BR><BR>'.
APPEND ls_text TO lt_textCLEAR ls_text.

ls_text-line 'Thanks & Regards,<BR>'.
APPEND ls_text TO lt_textCLEAR ls_text.

ls_text-line 'Team ABAP Code'.
APPEND ls_text TO lt_textCLEAR ls_text.

"&... Email Subject Name
lv_subject 'XML Attchment Test Program'.
lv_lang sy-langu.

"&... Create xls Attchment
"&... Header Data
"&... cl_abap_char_utilities=>horizontal_tab - This is used for Horizontal Line with Tab delimiter
"&... cl_abap_char_utilities=>newline. - This is used for Verticle line(New Line)
CONCATENATE 'Ord.No' 'Ord.Type' 'Message' INTO DATA(lv_head)
SEPARATED BY cl_abap_char_utilities=>horizontal_tab.
lv_head |{ lv_head }{ cl_abap_char_utilities=>newline }|.

"&&& Loop Item Data.
CONCATENATE '001000001' 'ZFI' 'XLS Attchment test 1' INTO DATA(lv_item)
SEPARATED BY cl_abap_char_utilities=>horizontal_tab.
lv_item |{ lv_item }{ cl_abap_char_utilities=>newline }|.
lv_item_data |{ lv_item_data }{ lv_item }|.

CLEAR lv_item.
CONCATENATE '001000002' 'ZFI' 'XLS Attchment test 2' lv_string INTO lv_item
SEPARATED BY cl_abap_char_utilities=>horizontal_tab.
lv_item |{ lv_item }{ cl_abap_char_utilities=>newline }|.
lv_item_data |{ lv_item_data }{ lv_item }|.
CLEAR lv_item.
CONCATENATE '001000003' 'ZFI' 'XLS Attchment test 3' lv_string INTO lv_item
SEPARATED BY cl_abap_char_utilities=>horizontal_tab.
lv_item |{ lv_item }{ cl_abap_char_utilities=>newline }|.
lv_item_data |{ lv_item_data }{ lv_item }|.
"&&& End loop

"&... Concate Header and Item data
lv_string |{ lv_head }{ lv_item_data }|.

"&... Convert String to XString
CALL FUNCTION 'HR_KR_STRING_TO_XSTRING'
  EXPORTING
*   CODEPAGE_TO      = '8500'
    unicode_string   lv_string
    out_len          lv_out_len
  IMPORTING
    xstring_stream   lv_xstring
  EXCEPTIONS
    invalid_codepage 1
    invalid_string   2.

"&... Convert XString to Binary
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
  EXPORTING
    buffer        lv_xstring
  IMPORTING
    output_length lv_out_len
  TABLES
    binary_tab    lt_attachment.

"&... Create Document method
TRY.

    lref_document cl_document_bcs=>create_document(
                        i_type          lv_type
                        i_subject       lv_subject
                        i_length        lv_length
                        i_language      lv_lang
*                       i_importance    = lv_import
                        i_text          lt_text ).
  CATCH cx_document_bcs.  "

ENDTRY.

"&... Add Attchment
IF lt_attachment[] IS NOT INITIAL.

  "&... Attchment type
  lv_attc_type 'XLS'.

  "&... Attchment Name
  lv_attc_sub 'XLS attachment'.

  TRY.
      lref_document->add_attachment(
        EXPORTING
          i_attachment_type     lv_attc_type         " Attachment Document type
          i_attachment_subject  lv_attc_sub          " Attachment Title
*         i_attachment_size     = i_attachment_size    " Size of Document Content
          i_attachment_language lv_lang              " Language in Which Attachment Is Created
*         i_att_content_text    = i_att_content_text   " Content (Textual)
          i_att_content_hex     lt_attachment        " Content (Binary)
*         i_attachment_header   = i_attachment_header  " Attachment Header Data
*         iv_vsi_profile        = iv_vsi_profile       " Virus Scan Profile
      ).
    CATCH cx_document_bcs.    "
  ENDTRY.

ENDIF.

"&... Set document
TRY.
    lref_sender_request->set_documenti_document lref_document ).
  CATCH cx_send_req_bcs.
ENDTRY.

"&... Set Sender
TRY.
    lref_sender_request->set_senderi_sender lref_sender ).
  CATCH cx_send_req_bcs.
ENDTRY.

"&... Add Sender Email ID or set SAP User id as sender email id
IF p_email IS NOT INITIAL.

  "&... Add Sender email id
  TRY.
      lref_sender cl_cam_address_bcs=>create_internet_address(
                      i_address_string p_email ).
    CATCH cx_address_bcs.
  ENDTRY.

ELSE.

  "&... Set SAP User ID
  TRY.
      lref_sender cl_sapuser_bcs=>createi_user sy-uname ).
    CATCH cx_address_bcs.
  ENDTRY.

ENDIF.

"&... Set Recipient
"&... lv_recipent - In this variable add receipient email id
*loop at
lv_recipent 'abapteam@gmail.com'.
TRY.
    lref_recipent cl_cam_address_bcs=>create_internet_address(
                        i_address_string lv_recipent ).
  CATCH cx_address_bcs.
ENDTRY.

"&... Add Recipient Email
"i_express    - This parameter for when the message needs to be send as express message(To)
"i_copy       - This parameter for when we need to send the copy of the message(CC)
"i_blind_copy - This parameter for when we need to send the message as a blind copy(BCC)
"i_no_forward - If you want to send email as forwarded then use this parameter(FW)
TRY.
    lref_sender_request->add_recipient(
      EXPORTING
        i_recipient     lref_recipent  " Recipient of Message
        i_express       abap_true      " Send As Express Message
*       i_copy          = i_copy         " Send Copy
*       i_blind_copy    = i_blind_copy   " Send As Blind Copy
*       i_no_forward    = i_no_forward   " No Forwarding
    ).
  CATCH cx_send_req_bcs.    "
ENDTRY.
*endloop.

"&... Set Send Immediately
TRY.
    lref_sender_request->set_send_immediatelyi_send_immediately abap_true ).
  CATCH cx_send_req_bcs.
ENDTRY.

"&... Send Email to All Recipient
TRY.
    lref_sender_request->send(
        i_with_error_screen abap_true ).

    COMMIT WORK.

  CATCH cx_send_req_bcs.
ENDTRY.

Download code in txt format: Click

Download code in PDF format: Click

Output:










Email body with attachment 





SALV 8: Adding Custom PF STATUS in ALV

Note: First create Custom PF Status using below Link: https://sapabapc.blogspot.com/2022/08/create-custom-pf-status.html *&-------------...