Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

Adding multiple spool request into one PDF document and send mail with PDF attachment

*----Suppose we have more than one spool corresponding to one user and we need to append all the spools data into one PDF, and further we need to send that PDF as attachment to that user.
For adding Spool we have function module 'RSPO_RETURN_SPOOLJOB' and further to convert that data to PDF we have function module 'CONVERT_OTF_2_PDF'.
*---declarations for spool
DATA:  SIZE    TYPE I,
        DOC    LIKE TABLE OF DOCS,
        PDF    LIKE TABLE OF TLINE WITH HEADER LINE,
        TAB    TYPE TABLE OF SOLI WITH HEADER LINE,
        SPOOL1 LIKE TSP01-RQIDENT,
        OTF    TYPE TABLE OF ITCOO WITH HEADER LINE,
        EOF    TYPE SOLI,
        TAB1   TYPE TABLE OF SOLI,
        FILE   TYPE STRING,
        LINES  TYPE I.
*---internal table to store the spool_id and receiver.
DATA: BEGIN OF IT_MAIL OCCURS 0,
      RECEIVER LIKE I_ADR6-EMAIL,
      NAME1    TYPE  NAME1,
      SPOOL_ID LIKE TSP01-RQIDENT,
      END OF IT_MAIL.
*---Lets assume internal table will contain these records
      IT_MAIL-RECEIVER = abc@134.com .
      IT_MAIL-NAME1    = Trishna.
      IT_MAIL-SPOOL_ID = 20001.
      APPEND IT_MAIL.
      CLEAR  IT_MAIL.
      IT_MAIL-RECEIVER = xyz@134.com .
      IT_MAIL-NAME1    = Trishna.
      IT_MAIL-SPOOL_ID = 20003.
      APPEND IT_MAIL.
      CLEAR  IT_MAIL.
      IT_MAIL-RECEIVER = abc@134.com .
      IT_MAIL-NAME1    = Trishna.
      IT_MAIL-SPOOL_ID = 20009.
      APPEND IT_MAIL.
      CLEAR  IT_MAIL.
PERFORM SEND_SPOOL_MAIL.
*---Logic for sending multiple spools into one spool and send mail
FORM SEND_SPOOL_MAIL.
  DATA : IT_MAIL1 LIKE TABLE OF IT_MAIL WITH HEADER LINE.
  IT_MAIL1[] = IT_MAIL[].
  SORT IT_MAIL1 BY NAME1 RECEIVER .
  DELETE ADJACENT DUPLICATES FROM IT_MAIL1 COMPARING NAME1 RECEIVER.
  SORT IT_MAIL BY NAME1.
  DELETE ADJACENT DUPLICATES FROM IT_MAIL COMPARING NAME1 SPOOL_ID.
  LOOP AT IT_MAIL.
    SPOOL1 = IT_MAIL-SPOOL_ID.
*---Merging multiple spool requests into one
    PERFORM SPOOL_MERGE .
    AT END OF NAME1.
      GV_BNAME = IT_MAIL-NAME1.
      REFRESH I_MAILLIST[].
      LOOP AT IT_MAIL1 WHERE NAME1 = IT_MAIL-NAME1.
        I_MAILLIST-RECEIVER = IT_MAIL1-RECEIVER.
        I_MAILLIST-COM_TYPE = 'INT' .
        I_MAILLIST-EXPRESS  = 'X' .
        I_MAILLIST-REC_TYPE = 'U' .
        APPEND I_MAILLIST .
        CLEAR  I_MAILLIST .
      ENDLOOP.
**pass the email id for CC.
      I_MAILLIST-RECEIVER = P_CCMAILID.
      I_MAILLIST-COM_TYPE = 'INT' .
      I_MAILLIST-EXPRESS  = 'X' .
      I_MAILLIST-REC_TYPE = 'U' .
      I_MAILLIST-COPY     = 'X'.  "cc
      APPEND I_MAILLIST .
      CLEAR I_MAILLIST.
      SORT I_MAILLIST  BY RECEIVER.
      DELETE ADJACENT DUPLICATES FROM I_MAILLIST COMPARING RECEIVER.
      LOOP AT TAB.
        CLEAR OTF.
        OTF = TAB.
        APPEND OTF.
      ENDLOOP.
*---Convert OTF into PDF
      CALL FUNCTION 'CONVERT_OTF_2_PDF'
        EXPORTING
          USE_OTF_MC_CMD         = 'X'
        IMPORTING
          BIN_FILESIZE           = SIZE
        TABLES
          OTF                    = OTF
          DOCTAB_ARCHIVE         = DOC
          LINES                  = PDF
        EXCEPTIONS
          ERR_CONV_NOT_POSSIBLE  = 1
          ERR_OTF_MC_NOENDMARKER = 2
          OTHERS                 = 3.
*---sending pdf in mail
      PERFORM SEND_PDF_MAIL.
*---refresh internal tables 'tab', 'OTF' for appending other Spool
      REFRESH: TAB, OTF, I_MAILLIST.
    ENDAT.
  ENDLOOP.
ENDFORM.    "send_spool_mail
*&---------------------------------------------------------------------*
*       Merging multiple spool requests into one
*----------------------------------------------------------------------*
FORM SPOOL_MERGE .
  CALL FUNCTION 'RSPO_RETURN_SPOOLJOB'
    EXPORTING
      RQIDENT              = SPOOL1
    TABLES
      BUFFER               = TAB1
    EXCEPTIONS
      NO_SUCH_JOB          = 1
      JOB_CONTAINS_NO_DATA = 2
      SELECTION_EMPTY      = 3
      NO_PERMISSION        = 4
      CAN_NOT_ACCESS       = 5
      READ_ERROR           = 6
      TYPE_NO_MATCH        = 7
      OTHERS               = 8.
  IF SY-SUBRC = 0.
  ENDIF.
  DESCRIBE TABLE TAB1 LINES LINES.
  READ TABLE TAB1 INDEX LINES INTO EOF.
  DELETE TAB1 INDEX LINES.
  APPEND LINES OF TAB1 TO TAB.
  APPEND EOF TO TAB.
ENDFORM.                    " SPOOL_MERGE
*&---------------------------------------------------------------------*
*       Logic for sending PDF in mail
*----------------------------------------------------------------------*
FORM SEND_PDF_MAIL .
  DATA : GD_BUFFER TYPE STRING.
  CLEAR IT_MESS_ATT.
  REFRESH IT_MESS_ATT[].
* Transfer the 132-long strings to 255-long strings
  LOOP AT PDF.
    TRANSLATE PDF USING ' ~'.
    CONCATENATE GD_BUFFER PDF INTO GD_BUFFER.
  ENDLOOP.
  TRANSLATE GD_BUFFER USING '~ '.
  DO.
    IT_MESS_ATT = GD_BUFFER.
    APPEND IT_MESS_ATT.
    SHIFT GD_BUFFER LEFT BY 255 PLACES.
    IF GD_BUFFER IS INITIAL.
      EXIT.
    ENDIF.
  ENDDO.
*get SENDER's (SY-UNAME) email id
  PERFORM GET_SENDER.
  DATA: GD_RECSIZE TYPE I.
  DESCRIBE TABLE IT_MESS_ATT LINES GD_RECSIZE.
  CHECK GD_RECSIZE > 0.
*& Subroutine to send the email to the RECIPIENT.
  PERFORM SEND_EMAIL.
ENDFORM.                    " SEND_PDF_MAIL
 *&---------------------------------------------------------------------*
* Get sender's email id from USR21 and ADR6
* sendor  name is sy-uname
*----------------------------------------------------------------------*
FORM GET_SENDER .
  DATA: BEGIN OF USER_INFO,
          BNAME TYPE USR21-BNAME,
          SMTP_ADDR TYPE ADR6-SMTP_ADDR,
        END OF USER_INFO.
* Get User's EMAIL ADDRESS( sender)
  CLEAR USER_INFO.
  SELECT SINGLE USR21~BNAME
                ADR6~SMTP_ADDR
                INTO CORRESPONDING FIELDS OF  USER_INFO
                FROM USR21
                INNER JOIN ADR6
                ON  USR21~ADDRNUMBER = ADR6~ADDRNUMBER
                AND USR21~PERSNUMBER = ADR6~PERSNUMBER
                WHERE USR21~BNAME = SY-UNAME.
  IF SY-SUBRC = 0.
    P_SENDER =   USER_INFO-SMTP_ADDR.
  ENDIF.
ENDFORM.                    " GET_SENDER
 *&---------------------------------------------------------------------*
*       Sending Mail
*----------------------------------------------------------------------*
FORM SEND_EMAIL.
  CLEAR   IT_MESS_BOD.
  REFRESH IT_MESS_BOD[].
* Default subject matter
  GD_SUBJECT         = TEXT-001 .
  DATA : V_DATE(6) TYPE C.
  CONCATENATE SY-DATUM+4(2) SY-DATUM+6(2) SY-DATUM+2(2) INTO V_DATE .
  CONCATENATE TEXT-002 V_DATE INTO GD_ATTACHMENT_NAME SEPARATED BY SPACE.
  .
  CONCATENATE TEXT-002 V_DATE INTO GD_ATTACHMENT_DESC SEPARATED BY SPACE.
  .
  CONCATENATE 'To ' GV_BNAME INTO IT_MESS_BOD SEPARATED BY SPACE.
  APPEND IT_MESS_BOD.
  APPEND INITIAL LINE TO IT_MESS_BOD.
  IT_MESS_BOD        = TEXT-004.  "Your invoice(s) for this week are attached.
  APPEND IT_MESS_BOD.
  APPEND INITIAL LINE TO IT_MESS_BOD.
  IT_MESS_BOD        = TEXT-005.  "If you currently pay by check please use the
                                  "following address:
  APPEND IT_MESS_BOD.
  APPEND INITIAL LINE TO IT_MESS_BOD.
  IT_MESS_BOD        = TEXT-006.  "PO Box 110004
  APPEND IT_MESS_BOD.
  IT_MESS_BOD        = TEXT-007.  "New Delhi, CA 95164
  APPEND IT_MESS_BOD.
  APPEND INITIAL LINE TO IT_MESS_BOD.
  APPEND INITIAL LINE TO IT_MESS_BOD.
  IT_MESS_BOD        = TEXT-008.  "Thank you,
  APPEND IT_MESS_BOD.
  APPEND INITIAL LINE TO IT_MESS_BOD.
  APPEND INITIAL LINE TO IT_MESS_BOD.
  IT_MESS_BOD        = TEXT-009. "Manoj Dixit
  APPEND IT_MESS_BOD.
  APPEND INITIAL LINE TO IT_MESS_BOD.
  IT_MESS_BOD        = TEXT-010. "R/ Revenue Accountant
  APPEND IT_MESS_BOD.
 
**title  TEXT-016 for 'ABCD Ltd.'
  CONCATENATE TEXT-016 GV_BNAME INTO GV_TITLE SEPARATED BY SPACE.
* If no sender specified - default blank
  IF P_SENDER EQ SPACE.
    GD_SENDER_TYPE  = SPACE.
  ELSE.
    GD_SENDER_TYPE  = 'INT'.
  ENDIF.
* Send file by email as PDF Atachment
  PERFORM SEND_FILE_AS_EMAIL_ATTACHMENT
                               TABLES IT_MESS_BOD
                                      IT_MESS_ATT
                                USING GV_TITLE
                                      'PDF'
                                      GD_ATTACHMENT_NAME
                                      GD_ATTACHMENT_DESC
                                      P_SENDER
                                      GD_SENDER_TYPE
                             CHANGING GD_ERROR
                                      GD_RECIEVER.
ENDFORM.                    " SEND_EMAIL
*&---------------------------------------------------------------------*
*&      Form  SEND_FILE_AS_EMAIL_ATTACHMENT
*&---------------------------------------------------------------------*
*   subroutine to sending the attachment in the mail
*----------------------------------------------------------------------*
FORM SEND_FILE_AS_EMAIL_ATTACHMENT  TABLES   IT_MESSAGE
                                             IT_ATTACH
                                    USING    P_MTITLE
                                             P_FORMAT
                                             P_FILENAME
                                             P_ATTDESCRIPTION
                                             P_SENDER_ADDRESS
                                             P_SENDER_ADDRES_TYPE
                                    CHANGING PERROR
                                             P_RECIEVER.
  DATA: LD_ERROR               TYPE SY-SUBRC,
        LD_MTITLE              LIKE SODOCCHGI1-OBJ_DESCR,
        LD_FORMAT              TYPE  SO_OBJ_TP ,
        LD_ATTDESCRIPTION      TYPE  SO_OBJ_NAM ,
        LD_ATTFILENAME         TYPE  SO_OBJ_DES ,
        LD_SENDER_ADDRESS      LIKE  SOEXTRECI1-RECEIVER,
        LD_SENDER_ADDRESS_TYPE LIKE  SOEXTRECI1-ADR_TYP,
        LD_RECEIVER            LIKE  SY-SUBRC.
  DATA:   T_PACKING_LIST       LIKE SOPCKLSTI1 OCCURS 0 WITH HEADER LINE,
*          T_CONTENTS LIKE SOLISTI1 OCCURS 0 WITH HEADER LINE,
          T_RECEIVERS          LIKE SOMLRECI1 OCCURS 0 WITH HEADER LINE,
          T_ATTACHMENT         LIKE SOLISTI1 OCCURS 0 WITH HEADER LINE,
          T_OBJECT_HEADER      LIKE SOLISTI1 OCCURS 0 WITH HEADER LINE,
          W_CNT                TYPE I,
          W_SENT_ALL(1)        TYPE C,
          W_DOC_DATA           LIKE SODOCCHGI1.
  CLEAR: LD_MTITLE,
         LD_FORMAT,
         LD_ATTDESCRIPTION,
         LD_ATTFILENAME,
         LD_SENDER_ADDRESS,
         W_DOC_DATA.
  LD_MTITLE         = P_MTITLE.
  LD_FORMAT         = P_FORMAT.   "PDF
  LD_ATTDESCRIPTION = P_ATTDESCRIPTION.
  LD_ATTFILENAME    = P_FILENAME.
  LD_SENDER_ADDRESS = P_SENDER_ADDRESS.
  LD_SENDER_ADDRESS_TYPE = P_SENDER_ADDRES_TYPE.
* Add the recipients email address
  CLEAR T_RECEIVERS.
  REFRESH T_RECEIVERS.
  T_RECEIVERS[] = I_MAILLIST[].
* Fill the document data.
  W_DOC_DATA-DOC_SIZE   = 1.
* Populate the subject/generic message attributes
  W_DOC_DATA-OBJ_LANGU  = SY-LANGU.
  W_DOC_DATA-OBJ_NAME   = 'SAPRPT'.
  W_DOC_DATA-OBJ_DESCR  = LD_MTITLE .
  W_DOC_DATA-SENSITIVTY = 'F'.
* Fill the document data and get size of attachment
  CLEAR W_DOC_DATA.
  READ TABLE IT_ATTACH INDEX W_CNT.
  W_DOC_DATA-DOC_SIZE =
     ( W_CNT - 1 ) * 255 + STRLEN( IT_ATTACH ).
  W_DOC_DATA-OBJ_LANGU  = SY-LANGU.
  W_DOC_DATA-OBJ_NAME   = 'SAPRPT'.
  W_DOC_DATA-OBJ_DESCR  = LD_MTITLE.
  W_DOC_DATA-SENSITIVTY = 'F'.
  CLEAR T_ATTACHMENT.
  REFRESH T_ATTACHMENT.
  T_ATTACHMENT[] = IT_ATTACH[].
* Describe the body of the message
  CLEAR T_PACKING_LIST.
  REFRESH T_PACKING_LIST.
  T_PACKING_LIST-TRANSF_BIN = SPACE.
  T_PACKING_LIST-HEAD_START = 1.
  T_PACKING_LIST-HEAD_NUM   = 0.
  T_PACKING_LIST-BODY_START = 1.
  DESCRIBE TABLE IT_MESSAGE LINES T_PACKING_LIST-BODY_NUM.
  T_PACKING_LIST-DOC_TYPE   = 'RAW'.
  APPEND T_PACKING_LIST.
* Create attachment notification
  T_PACKING_LIST-TRANSF_BIN = 'X'.
  T_PACKING_LIST-HEAD_START = 1.
  T_PACKING_LIST-HEAD_NUM   = 1.
  T_PACKING_LIST-BODY_START = 1.
  DESCRIBE TABLE T_ATTACHMENT LINES T_PACKING_LIST-BODY_NUM.
  T_PACKING_LIST-DOC_TYPE   =  LD_FORMAT.
  T_PACKING_LIST-OBJ_DESCR  =  LD_ATTDESCRIPTION.
  T_PACKING_LIST-OBJ_NAME   =  LD_ATTFILENAME.
  T_PACKING_LIST-DOC_SIZE   =  T_PACKING_LIST-BODY_NUM * 255.
  APPEND T_PACKING_LIST.
* call the subroutine to send the mail
  CALL FUNCTION 'SO_DOCUMENT_SEND_API1'
    EXPORTING
      DOCUMENT_DATA              = W_DOC_DATA
      PUT_IN_OUTBOX              = 'X'
      SENDER_ADDRESS             = LD_SENDER_ADDRESS
      SENDER_ADDRESS_TYPE        = LD_SENDER_ADDRESS_TYPE
      COMMIT_WORK                = 'X'
    IMPORTING
      SENT_TO_ALL                = W_SENT_ALL
    TABLES
      PACKING_LIST               = T_PACKING_LIST
      CONTENTS_BIN               = T_ATTACHMENT
      CONTENTS_TXT               = IT_MESSAGE
      RECEIVERS                  = T_RECEIVERS
    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.
*---clearing contents from I_MAILLIST
  REFRESH I_MAILLIST.
ENDFORM.                    " SEND_FILE_AS_EMAIL_ATTACHMENT
  

1 Comment

  1. Former Member

    This is nice when all the spool are regular spool.

     But how can we concatenate spools into one PDF when those spools are PDF themself.