Application Development and Automation 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: 
Read only

Multiple pdf attachments from server folder using SO_DOCUMENT_SEND_API1

joaopcabrita
Explorer
0 Likes
1,968

Dears,

I am trying to build a program able to send an email with multiple pdf attachments.

The files are stored in a folder in SAP server.

I am able to generate a pdf file from a spool and sucessfully attach it to the email using:

CALL FUNCTION 'CONVERT_OTF_2_PDF'

IMPORTING

bin_filesize = v_bin_filesize

TABLES

otf = job_output_info-otfdata

doctab_archive = it_docs

lines = it_lines

EXCEPTIONS

err_conv_not_possible = 1

err_otf_mc_noendmarker = 2

OTHERS = 3.

IF sy-subrc = 0.

LOOP AT it_lines.

TRANSLATE it_lines USING ' ~'.

CONCATENATE gd_buffer it_lines INTO gd_buffer.

ENDLOOP.

TRANSLATE gd_buffer USING '~ '.

DO.

it_mess_att_aux = gd_buffer.

APPEND it_mess_att_aux.

SHIFT gd_buffer LEFT BY 255 PLACES.

IF gd_buffer IS INITIAL.

EXIT.

ENDIF.

ENDDO.

ENDIF.

Internal table it_mess_att_aux is the parameter contents_bin of FM SO_DOCUMENT_SEND_API1.

I have now 2 files on a server folder which I would like to include as well.

I am reading the content of the pdf files in following way:

(...)

DATA: gd_buffer TYPE string,

(...)

OPEN DATASET lv_fileserver FOR INPUT IN BINARY MODE.

IF sy-subrc <> 0.

MESSAGE e257(zmsgsd) WITH lv_fileserver.

ELSE.

CLEAR: it_mess_att_aux[], gd_buffer.

DO.

READ DATASET lv_fileserver INTO gd_buffer.

IF sy-subrc <> 0.

EXIT.

ENDIF.

APPEND gd_buffer TO it_mess_att_aux.

ENDDO.

APPEND LINES OF it_mess_att_aux TO it_mess_att.

I do not understand the purpose of the statement:

TRANSLATE it_lines USING ' ~'.

in this context. Could anyone explain it?

All the files are attached to email with the desired name but corrupted and with the wrong size.

Can anyone help me build the logic to attach a pdf file from server in a email?

Thank you in advance,

Kind Regards,

João Cabrita

1 ACCEPTED SOLUTION
Read only

joaopcabrita
Explorer
0 Likes
1,296

Dears,

Thank you for your inputs but I still could not manage to send emails with multiple pdf files.

I am now trying it with CL_BCS Class.:

For instance, BCS_EXAMPLE_5 is quite similar, but instead of sending a MSWORD file I want to send a PDF file.

I have changed the code from example BCS_EXAMPLE_5 like this:

(...)

  • -------- create and set document with attachment ---------------

  • create document from internal table with text

APPEND 'Hello world!' TO text.

document = cl_document_bcs=>create_document(

i_type = 'RAW'

i_text = text

i_length = '12'

i_subject = 'test created by BCS_EXAMPLE_2' ).

  • add attachment to document

  • BCS expects document content here e.g. from document upload

  • binary_content = ...

  • Here I am reading the content of PDF file from server in binary mode into attachment internal table - BEGIN

DATA: lv_file TYPE string.

CONCATENATE '

ECC-DEVSPECS_EMAIL' 'PT_C_ABDN040122.pdf' INTO lv_file.

OPEN DATASET lv_file FOR INPUT IN BINARY MODE.

IF sy-subrc = 0.

DO.

READ DATASET lv_file INTO wa_binary_content .

IF sy-subrc <> 0.

EXIT.

ENDIF.

APPEND wa_binary_content TO binary_content.

ENDDO.

ENDIF.

  • Here I am reading the content of PDF file from server in binary mode into attachment internal table - END

CALL METHOD document->add_attachment

EXPORTING

i_attachment_type = 'PDF'

i_attachment_subject = 'My attachment'

i_att_content_hex = binary_content.

(...)

The email is sent with the pdf file attached, the file size is correct, but I cannot open the file. The file is corrupted. I think I am not generating the itab binary_content correctly.

Can anyone help me on this matter?

Thank you in advance,

Kind Regards,

João Cabrita

5 REPLIES 5
Read only

Former Member
0 Likes
1,296

     data: l_send_request type ref to cl_bcs,         " Send request
      l_body      type bcsy_text,                " Mail body
      l_success   type bcsy_text,                " Atchmnt for success
      l_error     type bcsy_text,                " Atchmnt for error
      wa_text     type soli,                     " Work area for attach
      l_document  type ref to cl_document_bcs,   " Mail body
      l_sender    type ref to if_sender_bcs,     " Sender address
      l_recipient type ref to if_recipient_bcs,  " Recipient
      l_size      type sood-objlen,              " Size of Attachment
      l_lines     type i,                        " Lines count
      l_email     type ad_smtpadr,               " Email ID
      l_extension type soodk-objtp value 'RAW'.  " TXT format
* Prepare mail bidy
append 'Send multiple file as attachment of a Email.'  to l_body.
append space to l_body.
* Preparing contents of attachment with Change Log
* Header line
wa_text-line+0   = 'First column'.
wa_text-line+20  = 'Second column'.
wa_text-line+40  = 'Third column'.
wa_text-line+60  = 'Fourth column'.
wa_text-line+80  = 'Fifth column'.
wa_text-line+100 = 'Sixth column'.
wa_text-line+120 = 'Seventh column'.
wa_text-line+140 = 'Eighth column'.
append wa_text to l_success.
append wa_text to l_error.
clear : wa_text.
* Populate the data part
wa_text-line+0   = '111111111'.
wa_text-line+20  = '222222222'.
wa_text-line+40  = '333333333'.
wa_text-line+60  = '333333333'.
wa_text-line+80  = '444444444'.
wa_text-line+100 = '555555555'.
wa_text-line+120 = '666666666'.
wa_text-line+140 = '777777777'.
append wa_text to l_success.
clear : wa_text.
wa_text-line+0   = 'aaaaaaaaa'.
wa_text-line+20  = 'bbbbbbbbb'.
wa_text-line+40  = 'ccccccccc'.
wa_text-line+60  = 'ddddddddd'.
wa_text-line+80  = 'eeeeeeeee'.
wa_text-line+100 = 'fffffffff'.
wa_text-line+120 = 'ggggggggg'.
wa_text-line+140 = 'hhhhhhhhh'.
append wa_text to l_error.
clear : wa_text.
* Creates persistent send request
l_send_request = cl_bcs=>create_persistent( ).
* Craete document for mail body
l_document = cl_document_bcs=>create_document(
             i_type    = 'RAW'
             i_text    = l_body  " Mail body
             i_subject = 'Mail send as attachment' ).
* Attachment for success records
l_lines = lines( l_success ).
l_size = l_lines * 255.
* Add attchment
call method l_document->add_attachment
  exporting
    i_attachment_type    = l_extension
    i_attachment_subject = 'Success'
    i_attachment_size    = l_size
    i_att_content_text   = l_success. " Attachment for success record
* Attachment for error records
l_lines = lines( l_error ).
l_size = l_lines * 255.
* Add attchment
call method l_document->add_attachment
  exporting
    i_attachment_type    = l_extension
    i_attachment_subject = 'Error'
    i_attachment_size    = l_size
    i_att_content_text   = l_error. " Attachment for error record
* Add the document to send request
call method l_send_request->set_document( l_document ).
* Sender addess
l_sender = cl_sapuser_bcs=>create( sy-uname ).
call method l_send_request->set_sender
  exporting
    i_sender = l_sender.
* Recipient address
l_email = 'emailaddress'.
l_recipient = cl_cam_address_bcs=>create_internet_address( l_email ).

* Add recipient address to send request
call method l_send_request->add_recipient
  exporting
    i_recipient  = l_recipient
    i_express    = 'X'
    i_copy       = ' '
    i_blind_copy = ' '
    i_no_forward = ' '.
l_email = 'receipentemaiil'.
l_recipient = cl_cam_address_bcs=>create_internet_address( l_email ).
* Add recipient address to send request
call method l_send_request->add_recipient
  exporting
    i_recipient  = l_recipient
    i_express    = 'X'
    i_copy       = ' '
    i_blind_copy = ' '
    i_no_forward = ' '.
* E-Mail is placed into queue for sending. In case of very important alerts
* use the following parameter to initiate an 'immediate sending'; but notice that
* this will trigger a dedicated send process for every single message - which is
* very costly for system performance
* l_send_request->set_send_immediately( 'X' ). "uncomment for immediate sending
* Send mail
call method l_send_request->send( ).
commit work. */
Read only

Former Member
0 Likes
1,296

hi , try this ,it is working fine .


  DATA: i_objbin LIKE solisti1 OCCURS 0 WITH HEADER LINE,
          i_objpack LIKE sopcklsti1 OCCURS 0 WITH HEADER LINE,
          i_reclist LIKE somlreci1 OCCURS 0 WITH HEADER LINE,
*          wa_doc_chng LIKE sodocchgi1,
          i_objtxt LIKE solisti1 OCCURS 0 WITH HEADER LINE,
*         i_objpack LIKE sopcklsti1 OCCURS 0 WITH HEADER LINE,
       i_objhead LIKE solisti1 OCCURS 0 WITH HEADER LINE,
      i_tline TYPE TABLE OF tline WITH HEADER LINE,
      i_record LIKE solisti1 OCCURS 0 WITH HEADER LINE,
      wa_doc_chng LIKE sodocchgi1,
      v_lines_txt TYPE i,

       l_lines TYPE I VALUE '20'.

  CALL FUNCTION 'CONVERT_OTF'
    EXPORTING
      format        = 'PDF'
      max_linewidth = 132
    IMPORTING
      bin_filesize  = bin_filesize
    TABLES
      otf           = int_tab_otf_final
      lines         = int_pdf_tab.
  IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.
*****************soc ranjan***************************
*  IF full_path IS INITIAL.
**
**
*    CALL METHOD cl_gui_frontend_services=>file_save_dialog
*      EXPORTING
**    WINDOW_TITLE         =
**           DEFAULT_EXTENSION    = 'PDF'
**    DEFAULT_FILE_NAME    =
*           file_filter          = 'Portable Document Format (.pdf)' " for restricting saving file only as pdf
*      CHANGING
*        filename             = file_name
*        path                 = file_path
*        fullpath             = full_path
*            .
*    IF sy-subrc <> 0.
** MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
**            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
*    ENDIF.
*  ENDIF.

******************eoc ranjan*****************

*break-point.
  CONCATENATE full_path git_select_option-pernr INTO path SEPARATED BY '_'.
  CONCATENATE path 'PDF' INTO path SEPARATED BY '.'.
**  **************************for downloading*********************************

*  CALL FUNCTION 'GUI_DOWNLOAD'
*    EXPORTING
*      bin_filesize = bin_filesize
*      filename     = path
*      filetype     = 'BIN'
*    IMPORTING
*      filelength   = file_size
*    TABLES
*      data_tab     = int_pdf_tab.
*  IF sy-subrc <> 0.
** MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
**         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
*  ENDIF.
*******************************************************************

  CLEAR : lop , el , sl , cl .

*  ******************addeddd later  by ranjan  21.04.2011

DATA : desc TYPE char40 ,
      desc1 TYPE char100 .

  CALL FUNCTION 'QCE1_CONVERT'
TABLES
t_source_tab = int_pdf_tab
t_target_tab = i_record
EXCEPTIONS
convert_not_possible = 1
OTHERS = 2.
IF sy-subrc <> 0.
WRITE : / 'Error in conversion of pdf lines'(015).
ENDIF.

APPEND LINES OF i_record TO i_objbin.
*Creation of the entry for the compressed attachment
DESCRIBE TABLE i_objbin LINES bin_filesize.



i_objtxt = 'Hi'.
APPEND i_objtxt.

i_objtxt = '                                     '.
APPEND i_objtxt.

i_objtxt = 'Pls  find  the attached  salary slip ' .
APPEND i_objtxt.

i_objtxt = '  '.
APPEND i_objtxt.
i_objtxt = '  '.
APPEND i_objtxt.
i_objtxt = '  '.
APPEND i_objtxt.
i_objtxt = '  '.
APPEND i_objtxt.
i_objtxt = '  '.
APPEND i_objtxt.
i_objtxt = '         '.
APPEND i_objtxt.
i_objtxt = '  '.
APPEND i_objtxt.
i_objtxt = '  '.
APPEND i_objtxt.
i_objtxt = '        '.
APPEND i_objtxt.
i_objtxt = '  '.
APPEND i_objtxt.
i_objtxt = '         '.
APPEND i_objtxt.
i_objtxt = '  '.
APPEND i_objtxt.
i_objtxt = '  '.
APPEND i_objtxt.
i_objtxt = '         '.
APPEND i_objtxt.
i_objtxt = '  '.
APPEND i_objtxt.
i_objtxt = 'Regards:  '.
APPEND i_objtxt.

i_objtxt = 'Corporate HR  '.
APPEND i_objtxt.
DESCRIBE TABLE i_objtxt LINES L_LINES.

READ TABLE i_objtxt INDEX L_LINES.
wa_doc_chng-doc_size = ( L_LINES - 1 ) * 255 + STRLEN( i_objtxt ).


*Creating the entry for the compressed document
CLEAR i_objpack-transf_bin.
i_objpack-head_start = 1.
i_objpack-head_num = 0.
i_objpack-body_start = 1.
i_objpack-body_num = L_LINES.
i_objpack-doc_type = 'RAW'.
APPEND i_objpack.



**
***CLEAR:  i_objpack .
***i_objpack-transf_bin = ' '.
**i_objpack-head_start = 1.
**i_objpack-head_num = 0.
**i_objpack-body_start = 1.
**i_objpack-body_num = L_LINES.
**i_objpack-doc_type = 'TXT'.
**i_objpack-obj_descr = desc1 .
**APPEND i_objpack.


*
*CLEAR: i_objhead .
*i_objhead = DESC.
*APPEND i_objhead.

*break-point .
i_objpack-transf_bin = 'X'.
i_objpack-head_start = 1.
i_objpack-head_num = 1.
i_objpack-body_start = 1.
i_objpack-body_num = bin_filesize.
i_objpack-obj_name = 'Employee Payslip'.
i_objpack-obj_descr = 'Employee Payslip'.
i_objpack-doc_size = bin_filesize  * 255 .
i_objpack-doc_type = 'PDF'.
APPEND i_objpack.

CONCATENATE 'Salary Slip of Month' p_month INTO desc SEPARATED BY ' ' .



*DESCRIBE TABLE i_objtxt LINES v_lines_txt.
*READ TABLE i_objtxt INDEX v_lines_txt.

****wa_doc_chng-doc_size = ( v_lines_txt - 1 ) * 375 + STRLEN( i_objtxt ).
wa_doc_chng-obj_name = 'Payslip'.
wa_doc_chng-expiry_dat = sy-datum + 10.
wa_doc_chng-obj_descr = desc.
wa_doc_chng-sensitivty = 'F'.
wa_doc_chng-doc_size = v_lines_txt * 255.





CLEAR i_objpack.

DATA: l_usrid_long TYPE pa0105-usrid_long.
DATA : emessage(70) TYPE c.
CLEAR : l_usrid_long.





SELECT SINGLE usrid_long
FROM pa0105
INTO l_usrid_long
WHERE pernr = git_pa0001-pernr
AND subty = '0010'.
*break-point.
IF sy-subrc EQ 0.
i_reclist-receiver = l_usrid_long.                      


i_reclist-rec_type = 'U'.
i_reclist-com_type = 'INT'.
APPEND i_reclist.
ELSE.
WRITE:/ 'No email id is maintained for this personnel number' , git_pa0001-pernr.
ENDIF.



IF NOT i_reclist[] IS INITIAL.


*******************************************************************************************
***************************************for senders id *************************************
*******************************************************************************************
CALL FUNCTION 'SO_DOCUMENT_SEND_API1'
  EXPORTING
    document_data                    = wa_doc_chng
  put_in_outbox = 'X'

   sender_address                   = 'xys'
   sender_address_type              = 'SMTP'
   commit_work                      = 'X'
* IMPORTING
*   SENT_TO_ALL                      =
*   NEW_OBJECT_ID                    =
*   SENDER_ID                        =
  TABLES
packing_list = i_objpack
object_header = i_objhead
contents_bin = i_objbin
contents_txt = i_objtxt
receivers = i_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 SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

Read only

joaopcabrita
Explorer
0 Likes
1,297

Dears,

Thank you for your inputs but I still could not manage to send emails with multiple pdf files.

I am now trying it with CL_BCS Class.:

For instance, BCS_EXAMPLE_5 is quite similar, but instead of sending a MSWORD file I want to send a PDF file.

I have changed the code from example BCS_EXAMPLE_5 like this:

(...)

  • -------- create and set document with attachment ---------------

  • create document from internal table with text

APPEND 'Hello world!' TO text.

document = cl_document_bcs=>create_document(

i_type = 'RAW'

i_text = text

i_length = '12'

i_subject = 'test created by BCS_EXAMPLE_2' ).

  • add attachment to document

  • BCS expects document content here e.g. from document upload

  • binary_content = ...

  • Here I am reading the content of PDF file from server in binary mode into attachment internal table - BEGIN

DATA: lv_file TYPE string.

CONCATENATE '

ECC-DEVSPECS_EMAIL' 'PT_C_ABDN040122.pdf' INTO lv_file.

OPEN DATASET lv_file FOR INPUT IN BINARY MODE.

IF sy-subrc = 0.

DO.

READ DATASET lv_file INTO wa_binary_content .

IF sy-subrc <> 0.

EXIT.

ENDIF.

APPEND wa_binary_content TO binary_content.

ENDDO.

ENDIF.

  • Here I am reading the content of PDF file from server in binary mode into attachment internal table - END

CALL METHOD document->add_attachment

EXPORTING

i_attachment_type = 'PDF'

i_attachment_subject = 'My attachment'

i_att_content_hex = binary_content.

(...)

The email is sent with the pdf file attached, the file size is correct, but I cannot open the file. The file is corrupted. I think I am not generating the itab binary_content correctly.

Can anyone help me on this matter?

Thank you in advance,

Kind Regards,

João Cabrita

Read only

0 Likes
1,296

Hi Joao,

use

CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
  EXPORTING
    buffer = xstring
  TABLES
    binary_tab = it_pdf .

top convert the pdf hey string to a SOLISTI table as used by add_attachment.

Regards

Clemens

Read only

joaopcabrita
Explorer
0 Likes
1,296

Thank you all for your inputs,

Clemens, your suggestion solved the problem.

Kind Regards,

João Cabrita