Sunday, December 5, 2021

Step by Step Process of Preparing E-mail

 REPORT zemail.

*&---------------------------------------------------------------------*
*   In this Sample program will show you step by step process of preparing
*   an E-mail with an HTML body and sending it to multiple E-mail ids.
*
*   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.

*&---------------------------------------------------------------------*
*   Local Internal Table and structure declaration
*----------------------------------------------------------------------*
DATAlt_text TYPE soli_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> Sample Email trigggeting program testing. <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.

lv_subject 'Email Test Program'.
lv_lang sy-langu.

"&... 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.

"&... 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.

Output:

We can see the Email has sent to Recipient using SOST T-Code. If your SCOT Confirmation is not completed in this case email will not sent to recipient mail id as we can see below:

Email Body content:









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 *&-------------...