SAP ABAP Code to send email with HTML Table and Body

 We can use this program concept, to send email with html body and table.


 

REPORT ZCRM_NEW_BOOKING_EMAILS.

TABLES ZCRM_BOOKINGADR6.

DATA GT_BOOKING TYPE STANDARD TABLE OF ZCRM_BOOKING,
       GS_BOOKING TYPE ZCRM_BOOKING,
       GT_COAPPL  TYPE STANDARD TABLE OF ZCRM_COAPPL,
       GS_COAPPL  TYPE ZCRM_COAPPL,
       GS_MASTER  TYPE ZCRM_MASTER.


DATA IT_CONTENTS TYPE STANDARD TABLE OF SOLISTI1,
       WA_CONTENTS TYPE SOLISTI1,
       W_DOCUMENT  TYPE REF TO CL_DOCUMENT_BCS.

DATA:  "li_otf            type table of itcoo,
  "li_pdf_tab        type table of tline,
  "li_content_txt    type soli_tab,
*       li_content_hex    type solix_tab,
  LI_OBJHEAD   TYPE SOLI_TAB,
  GI_MAIN_TEXT TYPE STANDARD TABLE OF SOLISTI1,        "type bcsy_text.
  WA_MAIN_TEXT TYPE SOLISTI1.

DATA "IT_CONTENTS TYPE STANDARD TABLE OF solisti1,
  "wa_contents TYPE solisti1,
  TEXT      TYPE CHAR100,
  TEXT3     TYPE CHAR100,
  TEXT1     TYPE STRING,
  TEXT2     TYPE STRING,
  LV_AMT1   TYPE CHAR15,
  LV_AMT2   TYPE CHAR15,
  LV_AMT3   TYPE CHAR15,
  LV_AMT4   TYPE CHAR15,
  LV_DMBTR  TYPE DMBTR,
  lv_date(10),
  LV_CNT(5).
*/.. Work Area Declarations

DATALW_CONTROL_PARAMETERS TYPE SSFCTRLOP,
      LW_OUTPUT_OPTIONS     TYPE SSFCOMPOP,
      LW_SSFCRESCL          TYPE SSFCRESCL,
      LW_CONTENT            TYPE SOLI,
      LW_OTF                TYPE ITCOO.

*/..Variables Decalrations

DATAGV_FNAME        TYPE RS38L_FNAM,       "Fucntion Module
      GV_SUBJECT      TYPE SO_OBJ_DES,
      GV_TITLE        TYPE SO_OBJ_DES,
      LV_BIN_FILESIZE TYPE I,
      LV_TRANSFER_BIN TYPE SX_BOOLEAN,
      LV_LEN          TYPE SO_OBJ_LEN,
      LV_EMAIL        TYPE AD_SMTPADR,
      LV_SENT_TO_ALL  TYPE OS_BOOLEAN,
      LV_ATT_NAME     TYPE CHAR50,
      LV_SUB          TYPE STRING.         "so_obj_des.

*/.. Class Declarations

DATAGO_SEND_REQUEST  TYPE REF TO  CL_BCS,
      GO_DOCUMENT      TYPE REF TO  CL_DOCUMENT_BCS,
      LO_RECIPIENT     TYPE REF TO  IF_RECIPIENT_BCS,
      LO_BCS_EXCEPTION TYPE REF TO  CX_BCS.

DATASEND_REQUEST       TYPE REF TO CL_BCS.
*DATA: TEXT               TYPE BCSY_TEXT.
DATADOCUMENT           TYPE REF TO CL_DOCUMENT_BCS.
DATASENDER             TYPE REF TO CL_SAPUSER_BCS.
DATASENDER1            TYPE REF TO CL_CAM_ADDRESS_BCS.
DATARECIPIENT          TYPE REF TO IF_RECIPIENT_BCS.
DATABCS_EXCEPTION      TYPE REF TO CX_BCS.
DATASENT_TO_ALL        TYPE OS_BOOLEAN.
DATATL_CONTENTS        TYPE STANDARD TABLE OF SOLI.

CONSTANTS:
*-- Constants used in the body of the Email (HTML)
  C_HTM      TYPE CHAR3   VALUE 'HTM',
  C_SPACE(6TYPE C       VALUE ' ',
  C_NEW_LINE TYPE CHAR255 VALUE '<br>'.

DATA GV_FNAME1       TYPE RS38L_FNAM.

*/.. Internal Table Declaration

DATA:  LI_OTF         TYPE TABLE OF ITCOO,
       LI_OTF1        TYPE TABLE OF ITCOO,
       LI_PDF_TAB     TYPE TABLE OF TLINE,
       LI_CONTENT_TXT TYPE SOLI_TAB,
       LI_CONTENT_HEX TYPE SOLIX_TAB.

DATA:   BEGIN OF WA,
          DATA(255),
        END OF WA.
DATA:  ITAB LIKE STANDARD TABLE OF WA WITH HEADER LINE.

REFRESH LI_CONTENT_HEX,
          LI_OBJHEAD.

CLEAR   LV_TRANSFER_BINLV_TRANSFER_BIN,
          LV_LENIT_CONTENTS[].

SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.
SELECT-OPTIONS S_PID FOR ZCRM_BOOKING-PROJ_ID NO INTERVALS NO-EXTENSION OBLIGATORY,
                 S_BKNUM FOR ZCRM_BOOKING-BKNUM,
                 S_BKDATE FOR ZCRM_BOOKING-BKDATE OBLIGATORY DEFAULT SY-DATUM.
SELECTION-SCREEN SKIP.
PARAMETERS     P_FROMID TYPE ADR6-SMTP_ADDR OBLIGATORY DEFAULT '123@xyz.com'.
SELECT-OPTIONS S_TOID FOR ADR6-SMTP_ADDR NO INTERVALS.
SELECT-OPTIONS S_CCID FOR ADR6-SMTP_ADDR NO INTERVALS.
SELECT-OPTIONS S_BCCID FOR ADR6-SMTP_ADDR NO INTERVALS.
SELECTION-SCREEN END OF BLOCK B1.

INITIALIZATION.


START-OF-SELECTION.
  PERFORM GET_DATA.

END-OF-SELECTION.
*&---------------------------------------------------------------------*
*&      Form  GET_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM GET_DATA.

  SELECT FROM ZCRM_BOOKING INTO CORRESPONDING FIELDS OF TABLE GT_BOOKING
  WHERE PROJ_ID IN S_PID
    AND BKNUM IN S_BKNUM
  AND BKDATE IN S_BKDATE.

  sort gt_booking DESCENDING by BKDATE.

  IF GT_BOOKING[] IS NOT INITIAL.

    SELECT FROM ZCRM_COAPPL INTO CORRESPONDING FIELDS OF TABLE GT_COAPPL
    FOR ALL ENTRIES IN GT_BOOKING WHERE BKNUM GT_BOOKING-BKNUM.

    SELECT SINGLE FROM ZCRM_MASTER INTO CORRESPONDING FIELDS OF GS_MASTER
    WHERE PROJ_ID S_PID-LOW.

  ENDIF.

  "first letter caps
  IF GS_MASTER-PROJ_NAME IS NOT INITIAL.

    CALL FUNCTION 'ISP_CONVERT_FIRSTCHARS_TOUPPER'
      EXPORTING
        INPUT_STRING  GS_MASTER-PROJ_NAME
        SEPARATORS    ' -.,;:'
      IMPORTING
        OUTPUT_STRING GS_MASTER-PROJ_NAME.

  ENDIF.

  IF GS_MASTER-PROJ_ADDRESS IS  NOT INITIAL.

    CALL FUNCTION 'ISP_CONVERT_FIRSTCHARS_TOUPPER'
      EXPORTING
        INPUT_STRING  GS_MASTER-PROJ_ADDRESS
        SEPARATORS    ' -.,;:'
      IMPORTING
        OUTPUT_STRING GS_MASTER-PROJ_ADDRESS.

  ENDIF.

  IF GT_BOOKING[] IS NOT INITIAL.
    PERFORM SEND_EMAIL.
  ENDIF.

ENDFORM.
*&---------------------------------------------------------------------*
*&      Form  SEND_EMAIL
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM SEND_EMAIL.

  CLEAR IT_CONTENTS[].

  "email body
  WA_CONTENTS-LINE '<HTML> <BODY>'.
  APPEND WA_CONTENTS TO IT_CONTENTS.
  CLEAR WA_CONTENTS.

  CONCATENATE '<p style="font-family:Calibri;font-size:15;">' 'Dear Sir / Madam,' INTO WA_CONTENTS-LINE.
  APPEND WA_CONTENTS TO IT_CONTENTS.
  CLEAR WA_CONTENTS.

  PERFORM LINE_BREAK.
  PERFORM LINE_BREAK.

  "We wish to inform you booking details for 


  CONCATENATE 'We wish to inform you booking details for our project –'
  GS_MASTER-PROJ_NAME
  'situated at'
  GS_MASTER-PROJ_ADDRESS INTO WA_CONTENTS-LINE SEPARATED BY SPACE.
  APPEND WA_CONTENTS TO IT_CONTENTS.
  CLEAR WA_CONTENTS.


  "html body
  WA_CONTENTS-LINE '<table style="font-family:calibri;font-size:15;MARGIN:10px;"'.   " bordercolor="blue"'.
  APPEND WA_CONTENTS TO IT_CONTENTS.
  CLEAR WA_CONTENTS.

  WA_CONTENTS-LINE 'cellspacing="0" cellpadding="1" width="75%" '.
  APPEND WA_CONTENTS TO IT_CONTENTS.
  CLEAR WA_CONTENTS.

  WA_CONTENTS-LINE 'border="1"><tbody><tr>'.
  APPEND WA_CONTENTS TO IT_CONTENTS.
  CLEAR WA_CONTENTS.

  WA_CONTENTS-LINE '<th bgcolor="#C0C0C0">Sr. No.</th>'.                    "66CCFF
  APPEND WA_CONTENTS TO IT_CONTENTS.
  CLEAR WA_CONTENTS.

  WA_CONTENTS-LINE '<th bgcolor="#C0C0C0">Booking Date</th>'.
  APPEND WA_CONTENTS TO IT_CONTENTS.
  CLEAR WA_CONTENTS.


  WA_CONTENTS-LINE '<th bgcolor="#C0C0C0">Project Name</th>'.
  APPEND WA_CONTENTS TO IT_CONTENTS.
  CLEAR WA_CONTENTS.

  WA_CONTENTS-LINE '<th bgcolor="#C0C0C0">Customer Name</th>'.
  APPEND WA_CONTENTS TO IT_CONTENTS.
  CLEAR WA_CONTENTS.

  WA_CONTENTS-LINE '<th bgcolor="#C0C0C0">Unit No</th>'.
  APPEND WA_CONTENTS TO IT_CONTENTS.
  CLEAR WA_CONTENTS.

  WA_CONTENTS-LINE '<th bgcolor="#C0C0C0">Agreement Value</th>'.
  APPEND WA_CONTENTS TO IT_CONTENTS.
  CLEAR WA_CONTENTS.

  WA_CONTENTS-LINE '<th bgcolor="#C0C0C0">Customer Contact No</th>'.
  APPEND WA_CONTENTS TO IT_CONTENTS.
  CLEAR WA_CONTENTS.

  WA_CONTENTS-LINE '<th bgcolor="#C0C0C0">Customer Email ID</th>'.
  APPEND WA_CONTENTS TO IT_CONTENTS.
  CLEAR WA_CONTENTS.

  "table content
  CLEAR LV_CNT.
  LOOP AT GT_BOOKING INTO GS_BOOKING.
    CLEAR GS_COAPPL.

    WA_CONTENTS-LINE '<tr align = "center">'.
    APPEND WA_CONTENTS TO IT_CONTENTS.
    CLEAR WA_CONTENTS.

    LV_CNT LV_CNT + 1.
    CONCATENATE '<td>' LV_CNT '</td>' INTO WA_CONTENTS-LINE SEPARATED BY SPACE.
    APPEND WA_CONTENTS TO IT_CONTENTS.
    CLEAR WA_CONTENTS.

    clear LV_DATE.
    write gs_booking-BKDATE to lv_date.

    CONCATENATE '<td>' lv_date '</td>' INTO WA_CONTENTS-LINE SEPARATED BY SPACE.
    APPEND WA_CONTENTS TO IT_CONTENTS.
    CLEAR WA_CONTENTS.

    CONCATENATE '<td>' GS_MASTER-PROJ_NAME '</td>' INTO WA_CONTENTS-LINE SEPARATED BY SPACE.
    APPEND WA_CONTENTS TO IT_CONTENTS.
    CLEAR WA_CONTENTS.

    CONCATENATE '<td>' GS_BOOKING-NAME1 '</td>' INTO WA_CONTENTS-LINE SEPARATED BY SPACE.
    APPEND WA_CONTENTS TO IT_CONTENTS.
    CLEAR WA_CONTENTS.

    CONCATENATE '<td>' GS_BOOKING-UNIT_NO '</td>' INTO WA_CONTENTS-LINE SEPARATED BY SPACE.
    APPEND WA_CONTENTS TO IT_CONTENTS.
    CLEAR WA_CONTENTS.

    write gs_booking-agvalue to wa_contents-line.
    condense wa_contents-line.

    CONCATENATE '<td>' WA_CONTENTS-LINE '</td>' INTO WA_CONTENTS-LINE SEPARATED BY SPACE.
    APPEND WA_CONTENTS TO IT_CONTENTS.
    CLEAR WA_CONTENTS.

    CLEAR GS_COAPPL.
    LOOP AT GT_COAPPL INTO GS_COAPPL WHERE BKNUM GS_BOOKING-BKNUM
                                       AND MOBILE IS NOT INITIAL.
      EXIT.
    ENDLOOP.

    CONCATENATE '<td>' GS_COAPPL-MOBILE '</td>' INTO WA_CONTENTS-LINE SEPARATED BY SPACE.
    APPEND WA_CONTENTS TO IT_CONTENTS.
    CLEAR WA_CONTENTS.

    CLEAR GS_COAPPL.
    LOOP AT GT_COAPPL INTO GS_COAPPL WHERE BKNUM GS_BOOKING-BKNUM
                                       AND EMAIL IS NOT INITIAL.
      EXIT.
    ENDLOOP.

    CONCATENATE '<td>' GS_COAPPL-EMAIL '</td>' INTO WA_CONTENTS-LINE SEPARATED BY SPACE.
    APPEND WA_CONTENTS TO IT_CONTENTS.
    CLEAR WA_CONTENTS.

    CLEAR GS_BOOKING.
  ENDLOOP.

*----------------------------end on 17.07.2017--------------------------------------*
  WA_CONTENTS-LINE '</tbody> </table>'.
  APPEND WA_CONTENTS TO IT_CONTENTS.
  CLEAR WA_CONTENTS.

  PERFORM LINE_BREAK.
  PERFORM LINE_BREAK.

  WA_CONTENTS-LINE 'Kindly note:'.
  APPEND WA_CONTENTS TO IT_CONTENTS.
  CLEAR WA_CONTENTS.

  PERFORM LINE_BREAK.

  CONCATENATE C_SPACE C_SPACE C_SPACE C_SPACE '-  this is system generated email' INTO WA_CONTENTS-LINE SEPARATED BY SPACE.
  APPEND WA_CONTENTS TO IT_CONTENTS.
  CLEAR WA_CONTENTS.

  PERFORM LINE_BREAK.

  CONCATENATE C_SPACE C_SPACE C_SPACE C_SPACE
  '-  status of booking is ‘accepted’ as per the terms of booking form' INTO WA_CONTENTS-LINE SEPARATED BY SPACE.
  APPEND WA_CONTENTS TO IT_CONTENTS.
  CLEAR WA_CONTENTS.

  PERFORM LINE_BREAK.

  CONCATENATE C_SPACE C_SPACE C_SPACE C_SPACE
  '-  booking confirmation is subject to clearing of cheque / DD and compliance of booking conditions by customers'
  INTO WA_CONTENTS-LINE SEPARATED BY SPACE.
  APPEND WA_CONTENTS TO IT_CONTENTS.
  CLEAR WA_CONTENTS.

  PERFORM LINE_BREAK.
  PERFORM LINE_BREAK.

  WA_CONTENTS-LINE 'Regards,'.
  APPEND WA_CONTENTS TO IT_CONTENTS.
  CLEAR WA_CONTENTS.
  PERFORM LINE_BREAK.

  WA_CONTENTS-LINE = xyz'.
  APPEND WA_CONTENTS TO IT_CONTENTS.
  CLEAR WA_CONTENTS.
  PERFORM LINE_BREAK.


  TRY.
*     -------- create persistent send request ------------------------
      GO_SEND_REQUEST CL_BCS=>CREATE_PERSISTENT).

*     -------- create and set document -------------------------------
*     create document from internal table with text

      GO_DOCUMENT CL_DOCUMENT_BCS=>CREATE_DOCUMENT(
                      I_TYPE    'HTM'
                      I_TEXT    IT_CONTENTS
*                      i_length  = '12'
                      I_SUBJECT '' ).             "'


      CALL METHOD GO_SEND_REQUEST->SET_MESSAGE_SUBJECT
        EXPORTING
          IP_SUBJECT 'New Bookings'.

*      IF C_NOTICE IS NOT INITIAL.
*        LV_ATT_NAME = 'Notice of Termination'.
*      ENDIF.

*      IF NOT LI_CONTENT_HEX IS INITIAL.
*        GO_DOCUMENT->ADD_ATTACHMENT(
*             I_ATTACHMENT_TYPE      = 'PDF'
*             I_ATTACHMENT_SUBJECT   = LV_ATT_NAME    "gv_title   "attachment subject
*             I_ATTACHMENT_SIZE      = LV_LEN
*             I_ATT_CONTENT_HEX      = LI_CONTENT_HEX ).
*      ENDIF.

      CALL METHOD GO_SEND_REQUEST->SET_DOCUMENTGO_DOCUMENT ).


*** Send mail using Email_ID
*      SENDER1 = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( 'zafar.k@xrbia.com' ).
      SENDER1 CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESSP_FROMID ).
      CALL METHOD GO_SEND_REQUEST->SET_SENDER
        EXPORTING
          I_SENDER SENDER1.

      LOOP AT S_TOID.

      LO_RECIPIENT CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS(
                                         S_TOID-LOW ).

*     add recipient with its respective attributes to send request
      CALL METHOD GO_SEND_REQUEST->ADD_RECIPIENT
        EXPORTING
          I_RECIPIENT LO_RECIPIENT
          I_EXPRESS   'X'.

      ENDLOOP.

      "cc
      LOOP AT S_CCID.

        LO_RECIPIENT CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS(
                                       S_CCID-LOW ).

*     add recipient with its respective attributes to send request
        CALL METHOD GO_SEND_REQUEST->ADD_RECIPIENT
          EXPORTING
            I_RECIPIENT LO_RECIPIENT
            I_COPY      'X'.

      ENDLOOP.

      "cc
      LOOP AT S_BCCID.

        LO_RECIPIENT CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS(
                                       S_BCCID-LOW ).

*     add recipient with its respective attributes to send request
        CALL METHOD GO_SEND_REQUEST->ADD_RECIPIENT
          EXPORTING
            I_RECIPIENT  LO_RECIPIENT
            I_BLIND_COPY 'X'.

      ENDLOOP.


* set outbox flag
*  try.
*      if outbox = 'X'.
      CALL METHOD GO_SEND_REQUEST->SEND_REQUEST->SET_LINK_TO_OUTBOX'X' ).
*      endif.
*    catch cx_bcs.
*  endtry.


*     ---------- send document ---------------------------------------
*    TRY.
      CALL METHOD GO_SEND_REQUEST->SET_SEND_IMMEDIATELY
        EXPORTING
          I_SEND_IMMEDIATELY 'X'.
*     CATCH cx_send_req_bcs .
*    ENDTRY.

      CALL METHOD GO_SEND_REQUEST->SEND(
        EXPORTING
          I_WITH_ERROR_SCREEN 'X'
        RECEIVING
          RESULT              LV_SENT_TO_ALL ).

      IF LV_SENT_TO_ALL 'X'.
        MESSAGE S000(8IWITH 'Email send successfully'.
      ELSEIF LV_SENT_TO_ALL IS INITIAL.
        MESSAGE S000(8IWITH 'Email not send'.
      ENDIF.

      COMMIT WORK.

    CATCH CX_BCS INTO LO_BCS_EXCEPTION.

  ENDTRY.

ENDFORM.

FORM LINE_BREAK.
  WA_CONTENTS-LINE '<br>'.
  APPEND WA_CONTENTS TO IT_CONTENTS.
  CLEAR WA_CONTENTS.
ENDFORM.

output



Post a Comment

2 Comments

  1. ZCRM_BOOKING what are the fields of this table. please share a pic of this table also

    ReplyDelete

Total Pageviews