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

Sending Mails - Home Page

         The input to this program is : Document number (DOKNR ) Document type (DOKAR) and Documentversion (DOKVR) and Documentpart(DOKTL).

Error rendering macro 'code': Invalid value specified for parameter 'lang'
DATA  : BEGIN OF drawkey,
     dokar LIKE draw-dokar,
     doknr LIKE draw-doknr,
     dokvr LIKE draw-dokvr,
     doktl LIKE draw-doktl,
   END OF drawkey.
DATA : v_object_key(33) TYPE c.

DATA : comps TYPE STANDARD TABLE OF scms_doinf,
       wa_comps TYPE scms_doinf.

DATA : v_file_name TYPE scms_doinf-file_name.

DATA : v_docfile TYPE bapi_doc_files2-docfile.
DATA : v_zdm_file TYPE c.
DATA : v_extension(3) TYPE c.
DATA : v_filepath(255) TYPE c.
DATA : v_uname TYPE sy-uname.
DATA : v_objdes(50) TYPE c.

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


DATA: send_request       TYPE REF TO cl_bcs.
DATA: text               TYPE bcsy_text.
DATA: binary_content     TYPE solix_tab.
DATA: document           TYPE REF TO cl_document_bcs.
DATA: sender             TYPE REF TO cl_sapuser_bcs.
DATA: recipient          TYPE REF TO if_recipient_bcs.
DATA: bcs_exception      TYPE REF TO cx_bcs.
DATA: sent_to_all        TYPE os_boolean.
DATA: path               TYPE string.
DATA: filelength         TYPE i.
DATA: i_text             TYPE soli_tab.
DATA: wa_emailbody       TYPE soli.
DATA: it_emailbody       TYPE  soli_tab.
DATA: it_text            TYPE TABLE OF soli_tab.
DATA: v_dktxt            TYPE drat-dktxt.
DATA: v_dokst            TYPE draw-dokst,
      v_zdm_doc_owner_id TYPE draw-zdm_doc_owner_id,
      v_authgroup TYPE draw-begru.
DATA : wa_text           TYPE soli.
DATA : v_ext(3)          TYPE c.
DATA : v_filename        TYPE draw-filep.
DATA : v_extension_type  TYPE soodk-objtp.
DATA : v_attachname      TYPE sood-objdes.
DATA : v_mail            TYPE adr6-smtp_addr.
DATA : it_lo_objid TYPE TABLE OF sdok_loid,
       wa_lo_objid TYPE sdok_loid.

DATA : v_phio_id TYPE sdok_phid,
       v_ph_class TYPE sdok_phcl.
DATA : it_object_id TYPE sdokobject,
       wa_object_id TYPE  sdokobject.
DATA : it_file_content_binary TYPE STANDARD TABLE OF sdokcntbin,
       wa_file_content_binary TYPE sdokcntbin.

DATA: objcont TYPE TABLE OF soli.
DATA: it_context TYPE STANDARD TABLE OF sdokpropty.

DATA: it_content_binary TYPE solix_tab,
      it_content_binary_solix TYPE solix,
      wa_content_binary TYPE solix.

DATA: count TYPE i,
      data_string TYPE xstring.

DATA : v_counter TYPE sy-tabix.

DATA : v_dappl TYPE dappl.

TRY.
*     ------- create persistent send request -----------------------
    send_request = cl_bcs=>create_persistent( ).

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

   concatenate 'ATTN:this is ' documentnumber into v_objdes separated by space.
    APPEND  v_objdes TO text.
    document = cl_document_bcs=>create_document(
                    i_type    = 'RAW'
                    i_text    = i_text
                    i_subject = v_objdes ).
*     add attachment to document
*     BCS expects document content here e.g. from document upload
*     binary_content = ...

* create the attachment for the mail :

    SELECT  lo_objid
             INTO TABLE it_lo_objid
             FROM dms_doc2loio
            WHERE dokar = documenttype
              AND  doknr = documentnumber
              AND dokvr  = documentversion
              AND doktl = '000'.

    v_counter = 1.

    LOOP AT it_lo_objid INTO wa_lo_objid.
      SELECT SINGLE phio_id
                    ph_class
             INTO   (wa_object_id-objid ,wa_object_id-class)
             FROM    dms_ph_cd1
             WHERE   loio_id = wa_lo_objid.

      CALL FUNCTION 'SDOK_PHIO_LOAD_CONTENT'
       EXPORTING
         object_id                 = wa_object_id
        client                    = sy-mandt
*       AS_IS_MODE                =
*       RAW_MODE                  =
*       TEXT_AS_STREAM            =
      TABLES
*       FILE_ACCESS_INFO          =
*       FILE_CONTENT_ASCII        =
        file_content_binary       =   it_file_content_binary
      EXCEPTIONS
        not_existing              = 1
        not_authorized            = 2
        no_content                = 3
        bad_storage_type          = 4
        OTHERS                    = 5
               .
      IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
      ENDIF.

      DESCRIBE TABLE it_file_content_binary LINES count.

      count = count * 1022.

      CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
        EXPORTING
          input_length       = count
*   FIRST_LINE         = 0
*   LAST_LINE          = 0
       IMPORTING
         buffer             = data_string
        TABLES
          binary_tab         = it_file_content_binary[]
       EXCEPTIONS
         failed             = 1
         OTHERS             = 2
                .
      IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
      ENDIF.

      CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
        EXPORTING
          buffer                = data_string
*   APPEND_TO_TABLE       = ' '
* IMPORTING
*   OUTPUT_LENGTH         =
        TABLES
          binary_tab            = it_content_binary[]
                .
*

* get the extension and the names of the files ********************

      SELECT SINGLE dappl
                    filename
        INTO (v_dappl, v_filename )

        FROM dms_doc_files
        WHERE dokar = documenttype
         AND  doknr = documentnumber
         AND dokvr = documentversion
         AND file_idx = v_counter.

      v_extension_type = v_dappl.

      v_attachname  = v_filename.

      CLEAR v_filename.
*-----------------------------------------------------------------------------
*---------------------------------------------------------------------------

      CALL METHOD document->add_attachment
        EXPORTING
          i_attachment_type    = v_extension_type
          i_attachment_subject = v_attachname
          i_att_content_hex    = it_content_binary.

*     add document to send request
      CALL METHOD send_request->set_document( document ).

      v_counter = v_counter + 1.
    ENDLOOP.

    CLEAR v_counter.
*     -------- set sender ------------------------------------------
*     note: this is necessary only if you want to set the sender
*           different from actual user (SY-UNAME). Otherwise sender is
*           set automatically with actual user.

    sender = cl_sapuser_bcs=>create( sy-uname ).
    CALL METHOD send_request->set_sender
      EXPORTING
        i_sender = sender.

*     -------- add recipient (e-mail address) ----------------------
*     create recipient - please replace e-mail address !!!

    recipient = cl_cam_address_bcs=>create_internet_address(
                                           'papiya@gmail.com' ).
*     add recipient with its respective attributes to send request
    CALL METHOD send_request->add_recipient
      EXPORTING
        i_recipient = recipient
        i_express   = 'X'.
    CLEAR v_mail.
  ENDLOOP.
*     --------- send document --------------------------------------
  CALL METHOD send_request->send(
    EXPORTING
      i_with_error_screen = 'X'
    RECEIVING
      result              = sent_to_all ).
  IF sent_to_all = 'X'.
    WRITE text-003.
  ENDIF.

  COMMIT WORK.

* -----------------------------------------------------------
* *                     exception handling
* -----------------------------------------------------------
* -----------------------------------------------------------
CATCH cx_bcs INTO bcs_exception.
  WRITE: 'This is error.'(001).
  WRITE: 'Error'(002), bcs_exception->error_type.
  EXIT.

ENDTRY.
  • No labels

2 Comments

  1. Guest

    Nice one.....it should help many coders a lot..

  2. Former Member

    Papiya,

    Excellent !!! It's going to help many folks.

    Keep it up (thumbs up)