SAP ABAP Automatic eMail Sending Program




*&---------------------------------------------------------------------*
*& Report  ZEMAILPRG
*&
*&---------------------------------------------------------------------*
*& Authors      : 
*&---------------------------------------------------------------------*

REPORT  ZEMAILPRG.

TABLES: ADR6.
TYPE-POOLS: SLIS.

*&---------------------------------------------------------------------*
*&      Form  SUB_SEND_EMAILN
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM SUB_SEND_EMAILN.

  PERFORM FILL_TEXT.
  PERFORM FILL_OBJECT_DETAILS.

  *create receiver list
  PERFORM CREATE_RECEIVERS_LIST.

  *send mail
  PERFORM SEND_MAIL.

ENDFORM.                    "SUB_SEND_EMAILN

*&---------------------------------------------------------------------*
*&      Form  fill_text
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM FILL_TEXT .
  WRITE SY-DATUM TO WA_OBJTXT.
  CONCATENATE "Dear Sir,<br/><br/>MIS Report for YTD as on"(011)
              WA_OBJTXT "<br/><br/>"
              INTO WA_OBJTXT SEPARATED BY SPACE.

  APPEND WA_OBJTXT TO IT_OBJTXT.
  CLEAR WA_OBJTXT.

  CONCATENATE "<table border=""1"">"
              "<tr style=""background-color:silver;color:blue;"">"
                "<td style=""font-weight:bold;"">Particulars</td>"
                "<td style=""font-weight:bold;"">Yesterday</td>"
                "<td style=""font-weight:bold;"">Month</td>"
                "<td style=""font-weight:bold;"">As on " SY-DATUM+6(2) "." SY-DATUM+4(2) "." SY-DATUM+0(4) "</td>"
              "</tr>" INTO WA_OBJTXT.
  APPEND WA_OBJTXT TO IT_OBJTXT.
  CLEAR WA_OBJTXT.

  LOOP AT T_STRU_FINAL INTO WA_STRU_FINAL.
    WRITE: WA_STRU_FINAL-COLUMN1 TO V_TCOL1,
           WA_STRU_FINAL-COLUMN2 TO V_TCOL2 ROUND 0 DECIMALS 0,
           WA_STRU_FINAL-COLUMN3 TO V_TCOL3 ROUND 0 DECIMALS 0,
           WA_STRU_FINAL-COLUMN4 TO V_TCOL4 ROUND 0 DECIMALS 0.

    CONCATENATE "<tr>"
                  "<td style=""font-weight:bold;"">" V_TCOL1 "</td>"
                  "<td style=""text-align:right;"">" V_TCOL2 "</td>"
                  "<td style=""text-align:right;"">" V_TCOL3 "</td>"
                  "<td style=""text-align:right;"">" V_TCOL4 "</td>"
                "</tr>"
                INTO WA_OBJTXT-LINE.
    APPEND WA_OBJTXT TO IT_OBJTXT.
    CLEAR: WA_OBJTXT, WA_STRU_FINAL, V_TCOL1, V_TCOL2, V_TCOL3, V_TCOL4.
  ENDLOOP.

  WA_OBJTXT = "</table>".
  APPEND WA_OBJTXT TO IT_OBJTXT.
  CLEAR WA_OBJTXT.

ENDFORM. " fill_text


*&---------------------------------------------------------------------*
*&      Form  fill_object_details
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM FILL_OBJECT_DETAILS .
  DESCRIBE TABLE IT_OBJTXT LINES TAB_LINES.
  READ TABLE IT_OBJTXT INTO WA_OBJTXT INDEX TAB_LINES.
  WA_DOCDATA-OBJ_NAME = "".  "TEXT_HTML"
  WRITE SY-DATUM TO WA_DOCDATA-OBJ_DESCR.
  CONCATENATE "MIS Report YTD"(010)
              WA_DOCDATA-OBJ_DESCR INTO WA_DOCDATA-OBJ_DESCR SEPARATED BY SPACE.
  WA_DOCDATA-DOC_SIZE = ( TAB_LINES - 1 ) * 255 + STRLEN( WA_OBJTXT ).
  CLEAR WA_OBJPACK-TRANSF_BIN.
  WA_OBJPACK-HEAD_START = 1.
  WA_OBJPACK-HEAD_NUM = 0.
  WA_OBJPACK-BODY_START = 1.
  WA_OBJPACK-BODY_NUM = TAB_LINES.
  WA_OBJPACK-DOC_TYPE = "HTM".
  APPEND WA_OBJPACK TO IT_OBJPACK.
ENDFORM. " fill_object_details

*---------------------------------------------------------------------*
*Form create_receivers_list
*---------------------------------------------------------------------*
FORM CREATE_RECEIVERS_LIST .

  LOOP AT S_RECP.
    WA_RECLIST-RECEIVER = S_RECP-LOW.
    WA_RECLIST-REC_TYPE   = "U".
    WA_RECLIST-COM_TYPE   = "INT".
    WA_RECLIST-NOTIF_DEL  = "X".
    WA_RECLIST-NOTIF_NDEL = "X".
    APPEND WA_RECLIST TO IT_RECLIST.
    CLEAR WA_RECLIST.
    CLEAR S_RECP.
  ENDLOOP.

ENDFORM. " create_receivers_list

*---------------------------------------------------------------------
*Form send_mail
*---------------------------------------------------------------------\
FORM SEND_MAIL .
  CALL FUNCTION "SO_DOCUMENT_SEND_API1"
    EXPORTING
      DOCUMENT_DATA              = WA_DOCDATA
      PUT_IN_OUTBOX              = " "
      SENDER_ADDRESS             = "FICOMANAGER"  "SD1K905439
      COMMIT_WORK                = "X"
    TABLES
      PACKING_LIST               = IT_OBJPACK
      CONTENTS_TXT               = IT_OBJTXT
      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 "S" NUMBER SY-MSGNO
    WITH SY-MSGV1.
  ENDIF.
ENDFORM. " send_mail

CREATE VARIANT FOR THE REPORT INPUT SCREEN.
CREATE JOB SCHEDULE BY USING TCODE SM36 AND GIVE PROGRAM NAME AND VARIANT IN THAT SCHEDULE.
REPORT WILL BE AUTOMATICALLY TRIGGERED.