Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member


This Program accepts two Input Parameters :

1) Application Server Folder path

2) Customer number

It fetches Email Id of the given customer; also fetches the PDF files from the given Folder; attaches all the files in a single mail and sends.


*&---------------------------------------------------------------------*


*& Report  YS_MUL_INV_MAIL1                                         *


*&                                                                     *


*&---------------------------------------------------------------------*


*&                                                                     *


*&                                                                     *


*&---------------------------------------------------------------------*



REPORT  ys_mul_inv_mail1 .



*----------------------------------------------------------------------*


TYPE-POOLS: sx.



DATA: w_path_name          TYPE pfeflnamel,


      w_path_tmp           TYPE pfeflnamel,


      w_path               TYPE string,


      t_file_tbl           TYPE TABLE OF zsalfldir,


      w_file_tbl           TYPE zsalfldir.




DATA:


t_message_body  TYPE bcsy_text ,


g_objcont       TYPE solix_tab,


w_record        TYPE string,



w_pdf_xstring   TYPE xstring,


wo_send_request TYPE REF TO cl_bcs ,


wo_document     TYPE REF TO cl_document_bcs,


wo_sender       TYPE REF TO if_sender_bcs  ,


wo_recipient    TYPE REF TO if_recipient_bcs ,


wx_document_bcs TYPE REF TO cx_document_bcs ,



w_send          TYPE ad_smtpadr,


w_sent_to_all   TYPE os_boolean,


w_subject(50)   TYPE c,


w_adrnr         TYPE kna1-adrnr,


w_name1         TYPE kna1-name1,


w_name(40)      TYPE c,


w_subj          TYPE string, "so_obj_des,


w_invoice       TYPE vbeln_vf,


w_msg           TYPE string.



DATA: BEGIN OF t_file OCCURS 0,


            data(50000) TYPE c,


      END OF t_file.



*----------------------------------------------------------------------*


*                 Selection-Screen: Parameter Declaration              *


*----------------------------------------------------------------------*



PARAMETERS: p_kunnr TYPE kunnr OBLIGATORY,           “Customer number


      p_path TYPE rfpdo-rfbifile OBLIGATORY.  "App Serv Path



*---------------------------------------------------------------------*



CLASS cl_bcs DEFINITION LOAD.



CLEAR: w_path_name, t_file_tbl, w_path_tmp.


REFRESH: t_file_tbl.



w_path_name = p_path.



TRANSLATE w_path_name TO LOWER CASE.



CALL FUNCTION 'ZRZL_READ_DIR_LOCAL'


  EXPORTING


    name           = w_path_name


  TABLES


    file_tbl       = t_file_tbl


  EXCEPTIONS


    argument_error = 1


    not_found      = 2


    OTHERS         = 3.


IF sy-subrc <> 0.


  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno


          WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.


ELSE.


  IF t_file_tbl[] IS INITIAL.


* Files not found


    WRITE 'Files not found.'.                               "text-001.


  ENDIF.


ENDIF.



CLEAR:


      t_message_body,


      w_subj,


      w_msg,


      w_invoice,


      w_adrnr,


      w_name1,


      w_send,


      w_pdf_xstring,


      g_objcont,


      w_subject.




*  Create send request


wo_send_request = cl_bcs=>create_persistent( ).



* Fetch Name of the Customer


SELECT SINGLE name1 INTO w_name1


   FROM kna1


  WHERE kunnr = p_kunnr.



CONCATENATE w_name1 ',' INTO w_name.



* Fetch Address number


SELECT SINGLE adrnr INTO w_adrnr


   FROM kna1


  WHERE kunnr = p_kunnr.


IF sy-subrc EQ 0.


* Fetch email address


  SELECT SINGLE smtp_addr INTO w_send


    FROM adr6


    WHERE addrnumber = w_adrnr.


  IF sy-subrc NE 0.


    MESSAGE 'Email id is not updated in customer master. ' TYPE 'E'.


  ENDIF.


ENDIF.      "kna1 sy-subrc



CLEAR:


  w_msg.



CONCATENATE 'Please find the attached INVOICE ' '.' INTO w_msg SEPARATED BY space.



* Create message body and name of the attachment


APPEND w_name       TO t_message_body.


APPEND INITIAL LINE TO t_message_body.


APPEND w_msg        TO t_message_body.


APPEND INITIAL LINE TO t_message_body.


APPEND 'Thank You!' TO t_message_body.




CONCATENATE ' Customer: ' p_kunnr INTO w_subj SEPARATED BY space.




* Email Body


wo_document = cl_document_bcs=>create_document(


                              i_type = 'RAW'


                              i_text = t_message_body


                              i_subject = ' ' ).



LOOP AT t_file_tbl INTO w_file_tbl WHERE zname_len CS 'PDF'.



*  CONCATENATE 'Invoice_' w_file_tbl-zname_len INTO w_subject.


  w_subject = w_file_tbl-zname_len.


  CLEAR w_path_tmp.


  CLEAR g_objcont.



  CONCATENATE  w_path_name '\' w_file_tbl-zname_len


  INTO w_path_tmp.



  OPEN DATASET w_path_tmp FOR INPUT IN BINARY MODE.


  IF sy-subrc = 0.


    DO.


      CLEAR w_path.


      READ DATASET w_path_tmp INTO w_path.


      IF sy-subrc NE 0.


        EXIT.


      ELSE.


* Append all the Index files' Data in one Internal Table


        APPEND w_path TO t_file.


        CLEAR w_path.


      ENDIF.


    ENDDO.



    LOOP AT t_file.


      IF t_file-data IS NOT INITIAL.


        CLEAR w_record.


        w_record = t_file-data.


        CONDENSE w_record.


      ENDIF.


    ENDLOOP.



    CALL FUNCTION 'SCMS_STRING_TO_XSTRING'


    EXPORTING


      text           = w_record


    IMPORTING


      buffer         = w_pdf_xstring


   EXCEPTIONS


     failed         = 1


     OTHERS         = 2 .



* XSTRING format is then converted to Binary Format (SOLIX_TAB) to use in the attachment mail


    CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'


      EXPORTING


        buffer     = w_pdf_xstring


      TABLES


        binary_tab = g_objcont.




* Add attachment


    TRY.


        CALL METHOD wo_document->add_attachment


          EXPORTING


            i_attachment_type    = 'PDF'


            i_attachment_subject = w_subject


            i_att_content_hex    = g_objcont.



      CATCH cx_document_bcs INTO wx_document_bcs.



    ENDTRY.



    CLOSE DATASET w_path_tmp.


  ENDIF.



ENDLOOP.



* Pass the document to send request


CALL METHOD wo_send_request->set_document( wo_document ).



* Email Subject (More than 50 characters)


CALL METHOD wo_send_request->set_message_subject


  EXPORTING


    ip_subject = w_subj.



* Create Sender


wo_sender = cl_cam_address_bcs=>create_internet_address( 'email@company.com' ). " Sender Email Id



* Add sender to send request


CALL METHOD wo_send_request->set_sender


  EXPORTING


    i_sender = wo_sender.



* Create recipient


wo_recipient = cl_cam_address_bcs=>create_internet_address( w_send ).



*Set recipient


CALL METHOD wo_send_request->add_recipient


  EXPORTING


    i_recipient = wo_recipient


    i_express   = 'X'.



* Set send immediately


wo_send_request->set_send_immediately( 'X' ).



* Send email


wo_send_request->send(


EXPORTING


i_with_error_screen = 'X'


RECEIVING


result = w_sent_to_all ).



IF sy-subrc EQ space AND w_sent_to_all EQ 'X'.


  MESSAGE 'Mail sent successfully.' TYPE 'S'.


  COMMIT WORK.


ELSE.


  MESSAGE 'Error sending Email' TYPE 'E'.


  ROLLBACK WORK.


ENDIF.


Hope this Program will be helpful.

2 Comments