Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Sendin Spool reports to an e-mail address.

Former Member
0 Kudos

Just wondering how one would go about developing a process to look for a specific spool report and send it to a specific e-mail address. I would place the reports and associated e-mail address in a Z table to be maintained by a user group. I have no clue on how to read the spool and collect a specific report to be sent via e-mail. Any help or suggestions would be greatly appreciated.

Thanks,

Scott

3 REPLIES 3

former_member214131
Active Contributor
0 Kudos

Hello,

FM RS_SEND_MAIL_FOR_SPOOLLIST would help you.

Regards, Murugesh AS

Former Member
0 Kudos

Hi Scot,

You don't have to create a Z-table. You can use Distribution lists to accomplish this. You can assign the distribution list either when creating a job, or go to the spool (SP01) and go via menu Spool Request -> Forward -> Send using SAP-Office.

Regards,

John.

thomas_jung
Developer Advocate
Developer Advocate
0 Kudos

Here is some sample code. This program takes the spool from a previous job step. It converts this spool to PDF and then inserts this PDF as an attachment in an E-mail.


************************************************************************
* Program Name: KEG Job Sched Run with PDF Output Creation: 03/22/2001
*
*                                                                      *
* SAP Name    : ZESU_JOB_RUN_WITH_PDF_OUTPUT      Application: U       *
*                                                                      *
* Author      : Thomas Jung                       Type: 1              *
*______________________________________________________________________*
* Description :  This Program will read the spool produced
* by the specified previous job step and converts it to
* PDF.  This PDF-Formatted report is then output to a file.            *
*______________________________________________________________________*
* Inputs: File name, job step                                          *
*                                                                      *
* Outputs: File formatted in PDF with the report output.               *
*______________________________________________________________________*
* External Routines
* FILE_GET_NAME
* CONVERT_ABAPSPOOLJOB_2_PDF
* CONVERT_OTFSPOOLJOB_2_PDF
* Z_E_OPEN_FILE_FOR_OUTPUT_BIN
* Z_E_WRITE_TO_FILE_WITH_LENGTH
* SO_NEW_DOCUMENT_ATT_SEND_API1
* BAPI_USER_GET_DETAIL
*______________________________________________________________________*
* Return Codes:
* The program uses the standard function modules for File manipulation
* therefore it has all the standard error routines for Files.          *
*______________________________________________________________________*
* Ammendments:                                                         *
*    Programmer        Date     Req. #            Action               *
* ================  ==========  ======  ===============================*
* Thomas Jung       03/27/2001  ------  Initial Program.               *
*                                                                      *
************************************************************************

report  zesu_job_run_with_pdf_output message-id zes_job.

*----------------------------------------------------------------------*
* DATA DICTIONARY TABLES                                               *
*----------------------------------------------------------------------*
tables: raldb. "Transfer Structure for Logical Databases
tables: tbtcp.
tables: icon,   "Icons table
        bapiadsmtp, "BAPI structure for e-mail addresses (central
        bapiadpag.


*----------------------------------------------------------------------*
* TYPE POOLS                                                           *
*----------------------------------------------------------------------*
type-pools: sbdst.

*----------------------------------------------------------------------*
* CONSTANTS                                                            *
*----------------------------------------------------------------------*
constants: logical_file_name like filename-fileintern
              value 'ZEX_STANDARD_FILENAME',
           pdf_ext(4) type c
              value '.pdf',
           pdf_type(3) type c
              value 'PDF',
           internet(1) type c
              value 'U',
           dist_list(1) type c
              value 'C',
           kimball_domain(12) type c value '@kimball.com'.

constants: kim_ftp(80) type c
value
'ftp://kww.kog.kimball.com/webtools/acrobat_reader/acroread40w95.exe'.

constants: ext_ftp(80) type c
value
'http://www.adobe.com/products/acrobat/readermain.html'.


*----------------------------------------------------------------------*
* VARIABLES                                                            *
*----------------------------------------------------------------------*
data: length type i.
data: f4_field like trdir-name.
data: pathleng type i,
      nameleng type i,
      physleng type i.
data jselect like btcselect.
data: report like sy-repid.

data: doc_chng         like sodocchgi1.

data: tab_lines        like sy-tabix.


*----------------------------------------------------------------------*
* INTERNAL TABLES                                                      *
*----------------------------------------------------------------------*
data: begin of html_tab occurs 0.
        include structure w3html.
data: end of html_tab.

data: icontab(32) occurs 10 with header line.

data: itab like abaplist occurs   0 with header line.

data: ipdf        like tline      occurs 0 with header line.

data: objpack          like sopcklsti1 occurs 2 with header line.

data: objhead          like solisti1   occurs 1 with header line.

data: objbin           like solisti1   occurs 10 with header line.

data: objtxt           like solisti1   occurs 10 with header line.

data: reclist          like somlreci1  occurs 5 with header line.

*----------------------------------------------------------------------*
* SELECTION SCREEN LAYOUT                                              *
*----------------------------------------------------------------------*
selection-screen begin of block one with frame title text-001.
parameters: fileintn like filename-fileintern obligatory
                default logical_file_name,
            filepath like rlgrap-filename lower case.

parameters: file1    like rlgrap-filename obligatory lower case,
            physname like rlgrap-filename lower case.
parameters: date1 as checkbox.
parameters: time1 as checkbox.
selection-screen end of block one.

selection-screen begin of block two with frame title text-002.
parameter: step like tbtcp-stepcount.
parameter: abap radiobutton group one.
parameter: oft  radiobutton group one.
selection-screen end of block two.

selection-screen begin of block three with frame title text-003.
parameter: file  radiobutton group two.
parameter: email radiobutton group two.

selection-screen skip 1.

parameter: int radiobutton group thre.
parameter: dis radiobutton group thre.

selection-screen skip 1.

parameter: ext_user like somlreci1-receiver.

selection-screen end of block three.
*----------------------------------------------------------------------*
* SELECTION SCREEN PROCESSING - OUTPUT                                 *
*----------------------------------------------------------------------*
at selection-screen output.
  perform filename_processing.
  loop at screen.
    if screen-name eq 'FILEPATH' or
       screen-name eq 'PHYSNAME'.
      screen-input = 0.
      modify screen.
    endif.
  endloop.


*----------------------------------------------------------------------*
* START OF SELECTION                                                   *
*----------------------------------------------------------------------*
start-of-selection.

  move sy-repid to report.
  perform auth_check.

  perform filename_processing.

  perform get_spool.

  perform convert_list_to_pdf.

  if file = 'X'.
    perform write_to_file.
  else.
    perform send_pdf_in_email.
  endif.


*&---------------------------------------------------------------------*
*&      Form  filename_processing
*&---------------------------------------------------------------------*
*       Prepair to conver to the logical file name to a physical name
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form filename_processing.
  perform get_filename using fileintn file1 physname.
  nameleng = strlen( file1 ).
  physleng = strlen( physname ).
  pathleng = physleng - nameleng.
  if pathleng > 0.
    filepath = physname(pathleng).
  endif.

endform.                    " filename_processing
*&---------------------------------------------------------------------*
*&      Form  get_filename
*&---------------------------------------------------------------------*
*       Convert the logical file name into a physical file name
*----------------------------------------------------------------------*
*      -->P_FILEINTN  Input Logical File Name
*      -->P_FILE1     Filename selected by input
*      -->P_PHYSNAME  Output Physical File Name
*----------------------------------------------------------------------*
form get_filename using    p_fileintn
                           p_file1
                           p_physname.

  data: p_file2 like rlgrap-filename.
  clear p_file2.

  move p_file1 to p_file2.

  if date1 = 'X'.
    concatenate p_file2
                '_'
                sy-datum
                into p_file2.
  endif.

  if time1 = 'X'.
    concatenate p_file2
                '_'
                sy-uzeit
                into p_file2.
  endif.

  concatenate p_file2
              pdf_ext
              into p_file2.

  call function 'FILE_GET_NAME'
        exporting
          client                        = sy-mandt
          logical_filename              = p_fileintn
          operating_system              = sy-opsys
          parameter_1                   = p_file2
*     PARAMETER_2                   = ' '
*     PARAMETER_3                   = ' '
*     USE_PRESENTATION_SERVER       = ' '
*     WITH_FILE_EXTENSION           = ' '
*     USE_BUFFER                    = ' '
        importing
*     EMERGENCY_FLAG                =
*     FILE_FORMAT                   =
          file_name                     = p_physname
        exceptions
          file_not_found                = 1
          others                        = 2
                .
  if sy-subrc <> 0.
    message e001(zes_general) with sy-subrc.
  endif.

endform.                    " get_filename
*&---------------------------------------------------------------------*
*&      Form  write_to_file
*&---------------------------------------------------------------------*
*       TRANSFER PDF DATA TO OUTPUT FILE
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form write_to_file.
  data: string_out type string.
  describe field objbin length length.

  call function 'Z_E_OPEN_FILE_FOR_OUTPUT_BIN'
       exporting
            filename1 = physname.

  loop at objbin.
    clear string_out.
    move objbin to string_out.
    call function 'Z_E_WRITE_TO_FILE_WITH_LENGTH'
         exporting
              filename1        = physname
              length           = length
              please_open_file = 'NO'
              string           = string_out.
  endloop.

endform.                    " write_to_file
*&---------------------------------------------------------------------*
*&      Form  convert_list_to_pdf
*&---------------------------------------------------------------------*
*       Convert to Spool to PDF
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form convert_list_to_pdf.
  data: spool_id like tsp01-rqident.
  move tbtcp-listident to spool_id.

****Is this ABAP List Processing?
  if abap = 'X'.
    call function 'CONVERT_ABAPSPOOLJOB_2_PDF'
      exporting
        src_spoolid                    = spool_id
*       NO_DIALOG                      =
*       DST_DEVICE                     =
*       PDF_DESTINATION                =
*     IMPORTING
*       PDF_BYTECOUNT                  =
*       PDF_SPOOLID                    =
*       LIST_PAGECOUNT                 =
*       BTC_JOBNAME                    =
*       BTC_JOBCOUNT                   =
      tables
        pdf                            = ipdf
     exceptions
       err_no_abap_spooljob           = 1
       err_no_spooljob                = 2
       err_no_permission              = 3
       err_conv_not_possible          = 4
       err_bad_destdevice             = 5
       user_cancelled                 = 6
       err_spoolerror                 = 7
       err_temseerror                 = 8
       err_btcjob_open_failed         = 9
       err_btcjob_submit_failed       = 10
       err_btcjob_close_failed        = 11
       others                         = 12.
    if sy-subrc <> 0.
      message i016 with sy-subrc.
    endif.
  else.
****Or is this SAPscript?
    call function 'CONVERT_OTFSPOOLJOB_2_PDF'
      exporting
        src_spoolid                    = spool_id
*       NO_DIALOG                      =
*       DST_DEVICE                     =
*       PDF_DESTINATION                =
*     IMPORTING
*       PDF_BYTECOUNT                  =
*       PDF_SPOOLID                    =
*       OTF_PAGECOUNT                  =
*       BTC_JOBNAME                    =
*       BTC_JOBCOUNT                   =
      tables
        pdf                            = ipdf
     exceptions
       err_no_otf_spooljob            = 1
       err_no_spooljob                = 2
       err_no_permission              = 3
       err_conv_not_possible          = 4
       err_bad_dstdevice              = 5
       user_cancelled                 = 6
       err_spoolerror                 = 7
       err_temseerror                 = 8
       err_btcjob_open_failed         = 9
       err_btcjob_submit_failed       = 10
       err_btcjob_close_failed        = 11
       others                         = 12.
    if sy-subrc <> 0.
      message i016 with sy-subrc.
    endif.
  endif.

  perform convert_pdf tables ipdf
                             objbin.

endform.                    " convert_list_to_pdf
*&---------------------------------------------------------------------*
*&      Form  get_spool
*&---------------------------------------------------------------------*
*   Read the job log to get the spool number for the specified step
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form get_spool.
  clear jselect.
*jselect-jobname = '*'.
  jselect-username = '*'.

****Have this program get its own Job Name
  call function 'GET_JOB_RUNTIME_INFO'
       importing
*         EVENTID                 =
*         EVENTPARM               =
*         EXTERNAL_PROGRAM_ACTIVE =
          jobcount                = jselect-jobcount
          jobname                 = jselect-jobname
*         STEPCOUNT               =
       exceptions
            no_runtime_info         = 1
            others                  = 2.

****Read the spool number for this job step.
  clear tbtcp.
  select single listident from tbtcp
         into tbtcp-listident
         where jobname   = jselect-jobname
           and jobcount  = jselect-jobcount
           and stepcount = step.
  if tbtcp-listident = 0.
    message i009 with step.
  endif.

endform.                    " get_spool
*&---------------------------------------------------------------------*
*&      Form  convert_pdf
*&---------------------------------------------------------------------*
*       Take the PDF information returned by the function module
*       and convert it to a file readable format.  It currently is
*       in a printable-only format.
*----------------------------------------------------------------------*
*      -->P_IPDF  PDF Source
*      -->P_OBJBIN  PDF Destination
*----------------------------------------------------------------------*
form convert_pdf tables   t_source_tab structure ipdf
                          t_target_tab structure objbin.


  data:  l_hfeld(1600)      type c,

         l_offset           type p,

         l_tabix            like sy-tabix,

         l_max              type i,

         l_linecount_source type i,

         l_length_source    type i,

         l_length_target    type i.



* Zeilenlänge des Quell-PDF-Files

  describe field t_source_tab length l_length_source.

* Zeilenlänge der Ziel-Mime-Tabelle

  describe field t_target_tab length l_length_target.

* Zeilenanzahl des Quell-PDF-Files

  describe table t_source_tab lines l_linecount_source.

* Die maximale Zielgröße wird durch das Feld l_hfeld begrenzt.

  l_max = l_length_target + l_length_source.

*  IF L_MAX > 1600.
*
*    RAISE CONVERT_NOT_POSSIBLE.
*
*  ENDIF.

  refresh t_target_tab.

  loop at t_source_tab.

    l_tabix = sy-tabix.

    move t_source_tab to l_hfeld+l_offset.

* falls letzte zeile der quelltabelle erreicht

    if l_tabix = l_linecount_source.

      l_length_source = strlen( t_source_tab ).

    endif.

    l_offset = l_offset + l_length_source.

    while l_offset ge l_length_target.

      clear t_target_tab.

      t_target_tab = l_hfeld(l_length_target).

      append t_target_tab.

      shift l_hfeld by l_length_target places.

      l_offset = l_offset - l_length_target.

    endwhile.                          " l_offset ge p_length_target

* falls letzte zeile der quelltabelle erreicht

    if l_tabix = l_linecount_source.

      if l_offset gt 0.

        clear t_target_tab.

        t_target_tab = l_hfeld(l_offset).

        append t_target_tab.

      endif.                           " l_offset gt 0

    endif.                             " l_tabix = l_linecount_lines

  endloop.                             " p_source_tab

endform.                    " convert_pdf
*&---------------------------------------------------------------------*
*&      Form  auth_check
*&---------------------------------------------------------------------*
*       Perform Authorization Check
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form auth_check.
  authority-check object 'Z_ABAP_CHK'
               id 'BUKRS' dummy
               id 'ACTVT' dummy
               id 'WERKS' dummy
               id 'REPID' field report.

  if sy-subrc ne 0.
    message e024.
  endif.

endform.                    " auth_check
*&---------------------------------------------------------------------*
*&      Form  send_pdf_in_email
*&---------------------------------------------------------------------*
*       Transfer the PDF file in an E-Mail
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form send_pdf_in_email.

  data: iaddsmtp type bapiadsmtp occurs 0 with header line.
  data: ireturn type bapiret2 occurs 0 with header line.

  concatenate text-010 jselect-jobname text-011
              into doc_chng-obj_descr
              separated by space.

  clear objtxt.
  objtxt = text-012.
  append objtxt.

  clear objtxt.
  objtxt = text-013.
  append objtxt.

  clear objtxt.
  concatenate text-014 jselect-jobname
              into objtxt
              separated by space.
  append objtxt.

  clear objtxt.
  objtxt = text-015.
  append objtxt.

  clear objtxt.
  objtxt = text-016.
  append objtxt.

  clear objtxt.
  append objtxt.

  clear objtxt.
  concatenate text-017 kim_ftp
              into objtxt
              separated by space.
  append objtxt.

  clear objtxt.
  concatenate text-018 ext_ftp
            into objtxt
            separated by space.
  append objtxt.

  describe table objtxt lines tab_lines.
  read table objtxt index tab_lines.
  doc_chng-doc_size = ( tab_lines - 1 ) * 255 + strlen( objtxt ).
  clear objpack-transf_bin.
  objpack-head_start = 1.
  objpack-head_num   = 0.
  objpack-body_start = 1.
  objpack-body_num   = tab_lines.
  objpack-doc_type   = 'RAW'.
  append objpack.

  describe table objbin lines tab_lines.
  concatenate jselect-jobname pdf_ext
              into objhead.
  append objhead.

  objpack-transf_bin = 'X'.
  objpack-head_start = 1.
  objpack-head_num   = 1.
  objpack-body_start = 1.
  objpack-body_num   = tab_lines.
  objpack-doc_type   = pdf_type.
  objpack-obj_name   = jselect-jobname.
  concatenate text-019 jselect-jobname
              into objpack-obj_descr
              separated by space.

  objpack-doc_size   = tab_lines * 255.
  append objpack.

  if int = 'X'.
    clear iaddsmtp.
    refresh iaddsmtp.
    clear bapiadsmtp.
    call function 'BAPI_USER_GET_DETAIL'
         exporting
              username = ext_user+0(12)
         tables
              return   = ireturn
              addsmtp  = iaddsmtp.
    loop at iaddsmtp where std_no = 'X'.
      clear bapiadsmtp.
      move iaddsmtp to bapiadsmtp.
    endloop.
    if bapiadsmtp-e_mail = ''.
      concatenate ext_user kimball_domain
                into reclist-receiver.
    else.
      move bapiadsmtp-e_mail to reclist-receiver.
    endif.
    move internet to reclist-rec_type.
  else.
    reclist-receiver = ext_user.
    move dist_list to reclist-rec_type.
  endif.
  append reclist.

  call function 'SO_NEW_DOCUMENT_ATT_SEND_API1'
    exporting
      document_data                    = doc_chng
      put_in_outbox                    = 'X'
*   IMPORTING
*     SENT_TO_ALL                      =
*     NEW_OBJECT_ID                    =
    tables
      packing_list                     = objpack
      object_header                    = objhead
      contents_bin                     = objbin
      contents_txt                     = objtxt
*     CONTENTS_HEX                     =
*     OBJECT_PARA                      =
*     OBJECT_PARB                      =
      receivers                        = 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
            .
  case sy-subrc.
    when 0.
      message i014 with reclist-receiver.
    when 1.
      message i015 with text-027.
    when 2.
      message i015 with text-028.
    when 3.
      message i015 with text-029.
    when 4.
      message i015 with text-030.
    when 5.
      message i015 with text-031.
    when 6.
      message i015 with text-032.
    when 7.
      message i015 with text-033.
    when 8.
      message i015 with text-036.
    when others.
      message i015 with text-036.
  endcase.


endform.                    " send_pdf_in_email