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: 

Sending DMS documents as Email attachments

Former Member
0 Kudos

Hello,

I have a requirement to read documents(For eg. XLS, JPG ) stored in SAP Document Management System(DMS) and send it in email.

The records read from DMS are in binary form (In internal table, I can see contents in Hexadecimal). If I send the document as an attachment using Function Module SO_NEW_DOCUMENT_SEND_API1, it doesnt come out nicely (If I open the attachment in Outlook/Lotus Notes, it is just junk characters).

Instead of doing this, If I download the document to desktop, upload it and send it as email attachment, it is working. Looks like contents of document get converted from Binary to ASCII in this process.

I guess the missing piece while trying to send it directly from SAP is format of the document content. If document is converted from Binary to ASCII format, I hope it will work.

Does anybody knows a conversion function module which does a conversion from Binary to ASCII format?

Thanks

Anil

10 REPLIES 10

Former Member

Hello Anil,

I have the same problem as you and I read the files (.jpg /.doc /.xls / ... ) from DMS, download it and upload it for conversion the format. But its not running in background, witch should be possible.

Have you got a solution ?

Many thanks for you answer.

Josef

0 Kudos

Hi Josef,

Yes. I have found a solution myself. Wrote a function module to get the DMS document attachments in 2 internal tables.

Code given below:

FUNCTION z_pm_doc_email.

*"----


""Local Interface:

*" IMPORTING

*" REFERENCE(DOKAR) TYPE DOKAR

*" REFERENCE(DOKNR) TYPE DOKNR

*" REFERENCE(DOKVR) TYPE DOKVR

*" TABLES

*" DATATAB1 STRUCTURE SOLISTI1

*" DATATAB2 STRUCTURE SOLISTI1

*"----


************************************************************************

  • Constants

************************************************************************

CONSTANTS: c_print TYPE apptp VALUE '3', "Print option

c_pc TYPE typdt VALUE 'PC', "Type

c_def TYPE ntadr VALUE 'DEFAULT', "Hostname

c_sys TYPE char4 VALUE 'WN32', "System

c_x TYPE char1 VALUE 'X'. "Value: X

************************************************************************

  • Variables

************************************************************************

DATA: l_appname TYPE tdwx-appfd,

l_apptype TYPE tdwd-typdt,

l_app TYPE tdwd-typdt,

l_tabix TYPE syindex,

lf_cont_provide LIKE mcdok-content_provide,

l_dttrg TYPE dttrg, "Name of data carrier

l_dappl TYPE dappl. "Application

DATA: prc_curr_dest_offset TYPE i VALUE 0,

prc_src_record_length TYPE i VALUE 0,

prc_src_data_rest TYPE i VALUE 0,

prc_dest_data_rest TYPE i VALUE 0,

prc_dest_record_length TYPE i VALUE 0,

prc_src_file_length TYPE i VALUE 0,

prc_error TYPE i VALUE 0,

prc_get_header TYPE c,

par_filesize TYPE i,

par_header_length TYPE i,

par_header TYPE xstring.

************************************************************************

  • Structures

************************************************************************

DATA: wa_draw TYPE draw,

wa_docfile TYPE dms_doc_file,

wa_frontend TYPE dms_frontend_data,

wa_cout TYPE dms_checkout_def.

DATA: BEGIN OF hex_record,

myhex(2550) TYPE x,

END OF hex_record.

DATA: wa_solisti1 LIKE solisti1.

************************************************************************

  • Internal Tables

************************************************************************

DATA: t_draz TYPE STANDARD TABLE OF draz.

DATA: t_drao LIKE drao OCCURS 0 WITH HEADER LINE,

t_draoz LIKE draoz OCCURS 0 WITH HEADER LINE.

DATA: prc_hex_tab LIKE hex_record OCCURS 1 WITH HEADER LINE.

DATA: BEGIN OF par_data_tab OCCURS 0,

mytex(255),

END OF par_data_tab.

************************************************************************

  • Field Symbols

************************************************************************

FIELD-SYMBOLS: <f_src> TYPE ANY,

<f_dest> TYPE ANY.

************************************************************************

  • Processing logic

************************************************************************

  • Get document data

SELECT SINGLE *

FROM draw

INTO wa_draw

WHERE dokar = dokar

AND doknr = doknr

AND dokvr = dokvr.

CHECK sy-subrc = 0.

l_app = c_pc.

CALL FUNCTION 'CV120_GET_APPL_TYPE'

EXPORTING

pf_dappl = wa_draw-dappl

pf_apptp = c_print

pf_typdt = l_app

IMPORTING

pfx_appl_name = l_appname

pfx_appl_type = l_apptype

EXCEPTIONS

error = 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.

wa_frontend-frontend_type = c_pc. "'PC'.

wa_frontend-hostname = c_def. "'DEFAULT'.

wa_frontend-winsys = c_sys. "'WN32'.

wa_frontend-platform = 0.

  • Print the original documents (upto 2) attached to the Document

DO 2 TIMES.

l_tabix = sy-index.

CLEAR wa_docfile.

CASE l_tabix.

WHEN 1.

l_dappl = wa_draw-dappl.

l_dttrg = wa_draw-dttrg.

WHEN 2.

l_dappl = wa_draw-dappl1.

l_dttrg = wa_draw-dttrg1.

ENDCASE.

wa_docfile-fileno = l_tabix.

wa_docfile-dappl = l_dappl.

wa_docfile-dttrg = l_dttrg.

wa_cout-comp_get = c_x. "'X'.

REFRESH: t_draz, t_drao, t_draoz.

clear: prc_curr_dest_offset,

prc_src_record_length,

prc_src_data_rest,

prc_dest_data_rest,

prc_dest_record_length,

prc_src_file_length,

prc_error,

prc_get_header,

par_filesize,

par_header_length,

par_header.

  • Read the document from DMS, document content will be available in

  • int. table t_drao

CALL FUNCTION 'CV120_DOC_CHECKOUT'

EXPORTING: ps_cout_def = wa_cout

ps_draw = wa_draw

ps_doc_file = wa_docfile

IMPORTING: pfx_cont_provide = lf_cont_provide

TABLES: pt_draz = t_draz

ptx_drao = t_drao

ptx_draoz = t_draoz

EXCEPTIONS: error = 1

OTHERS = 2.

CHECK t_drao[] IS NOT INITIAL.

REFRESH: prc_hex_tab.

CLEAR: prc_hex_tab.

  • Move it an int. table for conversion

LOOP AT t_drao.

prc_hex_tab-myhex = t_drao-orblk.

APPEND prc_hex_tab.

CLEAR prc_hex_tab.

IF sy-tabix = 1.

par_filesize = t_drao-orln.

ENDIF.

ENDLOOP.

DESCRIBE FIELD par_data_tab LENGTH prc_dest_record_length

IN BYTE MODE.

DESCRIBE FIELD prc_hex_tab LENGTH prc_src_record_length

IN BYTE MODE.

prc_dest_data_rest = prc_dest_record_length.

ASSIGN par_data_tab TO <f_dest> TYPE 'X'.

prc_src_file_length = par_filesize.

IF par_header_length > 0.

prc_get_header = 'X'.

ELSE.

CLEAR prc_get_header.

ENDIF.

LOOP AT prc_hex_tab.

ASSIGN prc_hex_tab TO <f_src> TYPE 'X'.

IF prc_src_file_length < prc_src_record_length.

prc_src_data_rest = prc_src_file_length.

ELSE.

prc_src_data_rest = prc_src_record_length.

ENDIF.

prc_src_file_length = prc_src_file_length - prc_src_record_length.

  • Check if we need the header

IF prc_get_header IS NOT INITIAL.

CLEAR prc_get_header.

IF par_header_length >= prc_dest_record_length.

  • MESSAGE ID 'FES' TYPE 'E' NUMBER '010' RAISING HEADER_TOO_LONG

*.

ENDIF.

MOVE <f_src>(par_header_length) TO par_header.

prc_src_data_rest = prc_src_data_rest - par_header_length.

<f_src> = <f_src>+par_header_length(prc_src_data_rest).

par_filesize = par_filesize - par_header_length.

ENDIF.

DO.

IF prc_src_data_rest < prc_dest_data_rest.

MOVE <f_src>(prc_src_data_rest)

TO <f_dest>+prc_curr_dest_offset.

prc_curr_dest_offset =

prc_curr_dest_offset + prc_src_data_rest.

prc_dest_data_rest = prc_dest_data_rest - prc_src_data_rest.

prc_src_data_rest = 0.

EXIT.

ELSE.

MOVE <f_src>(prc_dest_data_rest)

TO <f_dest>+prc_curr_dest_offset.

APPEND par_data_tab.

ASSIGN par_data_tab TO <f_dest> TYPE 'X'.

prc_curr_dest_offset = 0.

prc_src_data_rest = prc_src_data_rest - prc_dest_data_rest.

IF prc_src_data_rest > 0.

ASSIGN <f_src>+prc_dest_data_rest(prc_src_data_rest)

TO <f_src> TYPE 'X'.

prc_dest_data_rest = prc_dest_record_length.

ELSE.

prc_dest_data_rest = prc_dest_record_length.

EXIT.

ENDIF.

ENDIF.

ENDDO.

ENDLOOP.

IF prc_curr_dest_offset > 0.

APPEND par_data_tab.

ENDIF.

  • Move the contents of converted internal table

IF par_data_tab[] IS NOT INITIAL.

CASE l_tabix.

WHEN 1.

datatab1[] = par_data_tab[].

WHEN 2.

datatab2[] = par_data_tab[].

ENDCASE.

REFRESH par_data_tab.

ENDIF.

IF <f_src> IS ASSIGNED.

UNASSIGN <f_src>.

ENDIF.

IF <f_dest> IS ASSIGNED.

UNASSIGN <f_dest>.

ENDIF.

ENDDO.

ENDFUNCTION.

0 Kudos

Hi José

many thanks for the code - I'm testing it and think it works.

Best wishes

Josef

Former Member
0 Kudos

Hi Anil Jose,

Can you send me the code after coverting to the HEXA

I have same scenario but i m not to send that as a mail havinf a format problem .

You said you have solved the problem.

Former Member
0 Kudos

Hi Anil,

I have a similar requirement where I need to send all the DMS attachments on the PO line items as e-mail attachments along with the Purchase order form to the vendor.

Currently I have configured external send in the output type of the PO and it takes care of sending the PO form as a pdf attachment to the vendor. Now the challenge is to attach the DMS attachments of the PO along with the original PO on the same e-mail.

Please suggest...

Thanks,

K.

0 Kudos

Hi Ravi and all Gurus,

Did any find any option to send or Print the DMS documents attached at the line items of the PO.

We are presently facing issue and not able to figure how to go about .

Please help

Thanks and regards

Former Member
0 Kudos

Hello All,

Can someone please let us know how did you achieve this functionality? Please share your inputs...

Many people are looking for the solution... I am the one in that list. Please provide us the details.

Thank YOu.

Shyam

0 Kudos

Hello,

There are integrations available for SAP that allow you to collect order plus document attachments from PO module.

Feel free to email me directly for info.

Thanks,

David

0 Kudos

Just want to share, I was able to do send all kinds of documents.

*"----------------------------------------------------------------------

*"*"Lokale Schnittstelle:

*"  IMPORTING

*"     VALUE(PHIOS) TYPE SKWF_IOS

*"     VALUE(COMMIT) TYPE SKWF_FLAG DEFAULT ' '

*"     VALUE(USERS) TYPE SWDTUSER OPTIONAL

*"     VALUE(EMAIL_ADDRESSES) TYPE  BCSY_SMTPA OPTIONAL

*"     VALUE(FAX_ADDRESSES) TYPE  SDOK_FAX_RECIPIENTS OPTIONAL

*"     VALUE(SUBJECT) TYPE SO_OBJ_DES OPTIONAL

*"     VALUE(TEXT) TYPE SOLI_TAB OPTIONAL

*"     VALUE(SENDER_ID) TYPE SYUNAME OPTIONAL

*"  EXPORTING

*"     VALUE(ERROR) TYPE SKWF_ERROR

*"----------------------------------------------------------------------

  data: send_request       type ref to cl_bcs.

  data: document           type ref to cl_document_bcs.

  data: bcs_exception type ref to cx_bcs.

  data: lv_recipient       type ad_smtpadr.

  data: lt_phio            type skwf_ios.

  data: ls_phio            type skwf_io.

  data: ls_error           type skwf_error.

  data: ls_file_access_info    type sdokfilaci,

lt_file_access_info    type sdokfilacis,

lt_file_content_ascii  type table of sdokcntasc,

lt_file_content_binary type table of sdokcntbin.

  data: lt_text         type soli_tab.

  data: lt_mail_text    type soli_tab.

  data: lv_text         type soli.

  data: lv_size         type so_obj_len.

  data: lv_size_i type i.

  data: lv_buffer type xstring.

  data: lv_buffer_txt   type string.

  data: lt_content      type solix_tab.

  data: lt_content_txt  type soli_tab.

  data: lv_type         type soodk-objtp.

  data: lv_subject      type sood-objdes.

  data: lv_length type i.

  data: lt_attachments type         bcsy_ifdoc.

  data: lv_attachment   type ref to if_document_bcs.

  data: sent_to_all     type os_boolean.

  data: l_my_error_type type bcs_cxerr.

  data: lr_recipient    type ref to if_recipient_bcs.

  data: lt_recipients   type bcsy_re3.

  data: ls_recipient    type bcss_re3.

  data: lv_email        type ad_smtpadr.

  data: lv_user         type syuname.

  data: ls_fax          type sdok_fax_recipient.

  data: sender          TYPE REF TO cl_sapuser_bcs.

  lt_phio[]     = phios[].

  try.

      lv_text = 'Anlagen:'(001).

      append lv_text to lt_text.

      append initial line to lt_text.

* -> add content management documents as attachments

      loop at lt_phio into ls_phio.

        refresh: lt_file_content_ascii,

lt_file_content_binary,

lt_file_access_info.

data: ls_OBJECT_ID type  SDOKOBJECT.

ls_object_id-class = 'DMS_PCD1'.

ls_object_id-objid = '51DA751424DF22A5E10000000A01B002'.

* --> get content of cm document

        call function 'SDOK_PHIO_LOAD_CONTENT'

exporting

         OBJECT_ID = ls_object_id

importing

error = ls_error

tables

file_access_info    = lt_file_access_info

file_content_ascii  = lt_file_content_ascii

file_content_binary = lt_file_content_binary.

        if not ls_error is initial.

          error = ls_error.

        endif.

        check not lt_file_access_info[] is initial.

        read table lt_file_access_info

          into ls_file_access_info index 1.

* --> append info text about current document to mail text

lv_size    = ls_file_access_info-file_size.

        condense lv_size.

        concatenate '(' lv_size 'Byte'(002) ')' into lv_text.

        concatenate ls_file_access_info-file_name

lv_text

into lv_text

separated by space.

        append lv_text to lt_text.

* --> transform cm table (1022 bytes) into bcs table (255 bytes)

        lv_size_i = ls_file_access_info-file_size.

        refresh: lt_content, lt_content_txt.

        if not ls_file_access_info-binary_flg is initial.

          call function 'SCMS_BINARY_TO_XSTRING'

exporting

input_length = lv_size_i

importing

buffer       = lv_buffer

tables

binary_tab   = lt_file_content_binary.

          call function 'SCMS_XSTRING_TO_BINARY'

exporting

buffer     = lv_buffer

tables

binary_tab = lt_content.

        else.

          call function 'SCMS_FTEXT_TO_STRING'

exporting

length    = lv_size_i

importing

ftext     = lv_buffer_txt

tables

ftext_tab = lt_file_content_ascii.

          call function 'SCMS_STRING_TO_FTEXT'

exporting

text      = lv_buffer_txt

tables

ftext_tab = lt_content_txt.

        endif.

* Get extension of file

        data: l_file_parts type table of skwf_filnm,

l_file_ext type skwf_filnm,

l_i TYPE i,

l_ext TYPE i,

l_fn  TYPE i.

        split ls_file_access_info-file_name

at '.' into table l_file_parts. "#EC NOTEXT

        describe table l_file_parts lines l_i.

        clear l_file_ext.

        if l_i > 1.

          read table l_file_parts into l_file_ext index l_i.

*         the extension will be added later,no need to provide it with filename

          l_ext = STRLEN( l_file_ext ).

          l_fn = STRLEN( ls_file_access_info-file_name ).

          l_fn = l_fn - l_ext - 1.

          if l_fn GT 0.

ls_file_access_info-file_name = ls_file_access_info-file_name+0(l_fn).

          else.

clear ls_file_access_info-file_name.

endif.

        endif.

        translate l_file_ext to upper case. "#EC SYNTCHAR

        lv_type = l_file_ext.

        lv_subject = ls_file_access_info-file_name.

lv_size    = ls_file_access_info-file_size.

        call method cl_document_bcs=>create_document

exporting

i_type    = lv_type

i_subject = lv_subject

i_length  = lv_size

i_hex     = lt_content

i_text    = lt_content_txt

receiving

result    = document.

        append document to lt_attachments.

      endloop.

      if text[] is initial.

* -> append info text about documents to mail text

        append lines of lt_text to lt_mail_text.

      else.

        lt_mail_text[] = text[].

      endif.

* -> create recipients table

      loop at email_addresses into lv_email.

        lr_recipient = cl_cam_address_bcs=>create_internet_address( lv_email ).

        clear ls_recipient.

ls_recipient-recipient = lr_recipient.

        append ls_recipient to lt_recipients.

      endloop.

      loop at fax_addresses into ls_fax.

        lr_recipient = cl_cam_address_bcs=>create_fax_address( i_country = ls_fax-country

i_number  = ls_fax-number ).

        clear ls_recipient.

ls_recipient-recipient = lr_recipient.

        append ls_recipient to lt_recipients.

      endloop.

      loop at users into lv_user.

        lr_recipient ?= cl_sapuser_bcs=>create( lv_user ).

        clear ls_recipient.

ls_recipient-recipient = lr_recipient.

        append ls_recipient to lt_recipients.

      endloop.

      if sy-batch NE 'X' .

* -> send screen

      call method cl_bcs=>short_message

        exporting

i_subject       = subject

i_text          = lt_mail_text

i_attachments   = lt_attachments

i_recipients    = lt_recipients

i_starting_at_x = 10

i_starting_at_y = 2

i_ending_at_y   = 30

i_ending_at_x   = 100

        receiving

result          = send_request.

      if not commit is initial.

        commit work.

      endif.

      else.

*     the batch send

        send_request = cl_bcs=>create_persistent( ).

        document = cl_document_bcs=>create_document(

i_type    = 'RAW'

i_text    = lt_mail_text

i_subject = subject ).

        CALL METHOD send_request->set_document( document ).

        clear lv_attachment.

        loop at lt_attachments into lv_attachment.

          CALL METHOD DOCUMENT->ADD_DOCUMENT_AS_ATTACHMENT

EXPORTING

IM_DOCUMENT     = lv_attachment

          .

        endloop.

        clear lr_recipient.

        loop at lt_recipients into ls_recipient .

CALL METHOD send_request->add_recipient

EXPORTING

i_recipient  = ls_recipient-recipient

i_express    = ls_recipient-sndex.

        endloop.

        if not sender_id is initial.

sender = cl_sapuser_bcs=>create( sender_id ).

CALL METHOD send_request->set_sender

EXPORTING i_sender = sender.

        endif.

        CALL METHOD send_request->send(

exporting

i_with_error_screen = ' '

receiving

result = sent_to_all ).

        if sent_to_all NE 'X'.

MESSAGE I045(SKWG_ERRS).

*         Das Dokument wurde nicht an alle Empfänger gesendet

        endif.

        if not commit is initial.

commit work.

        endif.

      endif.

* -> exception handling

    CATCH CX_DOCUMENT_BCS cx_bcs INTO bcs_exception.

      l_my_error_type = bcs_exception->error_type.

      perform error_handling using l_my_error_type error.

      exit.

  endtry.

Thanks

Former Member
0 Kudos

Hi All

Even i have a similar requirement.

We need to print the documents but at the time of PGI of outbound delivery.

Help will be really appreciated.

Thanks-

Can