cancel
Showing results for 
Search instead for 
Did you mean: 

howto email documents attached to product

Former Member
0 Kudos

i'm trying to email a PDF document that is attached to a product (commpr01). From the GUI this is easy but how to do it from code?

I'm using

cl_crm_documents=>get_info

and

cl_crm_documents=>get_document

To access the binary data of the document attached to the product.

I'm using

SO_NEW_DOCUMENT_ATT_SEND_API1

to send an email with the attachment

Before sending i'm using function

TABLE_COMPRESS

to mold the binary data from

cl_crm_documents=>get_document

into binary data for

SO_NEW_DOCUMENT_ATT_SEND_API1

.

Somewhere along in the whole process something get garbled because, although i do receive an email WITH attachment, the document is unreadable.

I realy could use some help right about now..

regards, Marcel

my test program:

REPORT  ztest_documents_display                                     .

DATA:
  ls_business_object TYPE  sibflporb,
  lt_loios TYPE skwf_ios,
  lt_phios TYPE skwf_ios.


* product id invullen
ls_business_object-instid = 'D0CB9644CAEE2030E100000089780315'.
ls_business_object-typeid = 'BUS1178'.
ls_business_object-catid = 'BO'.

break makkerman.

CALL METHOD cl_crm_documents=>get_info
  EXPORTING
    business_object       = ls_business_object
*    PROPERTIES_REQUEST    =
*    NEWEST_ONLY           =
*    PROPERTIES_QUERY      =
*    PROPERTIES_QUERY_RA   =
  IMPORTING
*    PHIOLOIOS             =
*    IOS_PROPERTIES_RESULT =
    loios                 = lt_loios
    phios                 = lt_phios
    .


data:
  ls_io type skwf_io,
  lt_properties type sdokproptys,
  lt_file_access_info type sdokfilacis,
  ls_file_access_info type sdokfilaci,
  lt_content_ascii type sdokcntascs,
  lt_content_bin type sdokcntbins,
  lt_business_objects type sibflporbt,
  ls_loio 	type skwf_io,
  lv_io_does_not_exist 	type xflag.



DATA: docdata    LIKE sodocchgi1,
      objpack    LIKE sopcklsti1 OCCURS  1 WITH HEADER LINE,
      objhead    LIKE solisti1   OCCURS  1 WITH HEADER LINE,
      objtxt     LIKE solisti1   OCCURS 10 WITH HEADER LINE,
      objbin     LIKE solisti1   OCCURS 10 WITH HEADER LINE,
      objhex     LIKE solix      OCCURS 10 WITH HEADER LINE,
      reclist    LIKE somlreci1  OCCURS  1 WITH HEADER LINE.

DATA: tab_lines  TYPE i,
      doc_size   TYPE i,
      att_type   LIKE soodk-objtp.

objbin = '  |  '.
APPEND objbin.

break makkerman.

LOOP AT lt_loios INTO ls_io.

  CALL METHOD cl_crm_documents=>get_document
    EXPORTING
      io                = ls_io
    IMPORTING
      properties        = lt_properties
      phios             = lt_phios
      file_access_info  = lt_file_access_info
      content_ascii     = lt_content_ascii
      content_bin       = lt_content_bin
      business_objects  = lt_business_objects
      loio              = ls_loio
      io_does_not_exist = lv_io_does_not_exist.

  IF lv_io_does_not_exist IS INITIAL.
    LOOP AT lt_file_access_info INTO ls_file_access_info.
      NEW-LINE.
      NEW-LINE.
      WRITE: / 'Filename: ', ls_file_access_info-file_name, 'Mime-Type: ', ls_file_access_info-mimetype .
    ENDLOOP.

  ENDIF.


ENDLOOP.

*   Because listobject is of size RAW(1000)
*       and objbin     is of size CHAR(255) we make this table copy
CALL function 'TABLE_COMPRESS'
*     IMPORTING
*       COMPRESSED_SIZE       =
tables
IN                    = lt_content_bin
out                   = objbin
EXCEPTIONS
others                = 1
    .
IF sy-subrc <> 0.
  MESSAGE ID '61' TYPE 'E' NUMBER '731'
  WITH 'TABLE_COMPRESS'.
ENDIF.


* NOTE: Creation of attachment is finished yet.
*   For your report, the attachment should be placed into table
*     objtxt for plain text or
*     objbin for binary content.
*   Now create the message and send the document. 'of recipients'.
* Create Message Body
*   Title and Description
docdata-obj_name  = 'Your Order'.
docdata-obj_descr = 'Marcel stuurt PDFje ?'.
*   Main Text
objtxt = 'Test Document.'.
APPEND objtxt.
objtxt = 'Pompie dom hier is een pdf...'.
APPEND objtxt.
objtxt = 'Have a nice day.'.
APPEND objtxt.
*   Write Packing List (Main)
DESCRIBE TABLE objtxt LINES tab_lines.
READ     TABLE objtxt INDEX tab_lines.
docdata-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.
* Create Message Attachment
*   Write Packing List (Attachment)
DESCRIBE TABLE objbin LINES tab_lines.
READ     TABLE objbin INDEX tab_lines.
objpack-doc_size = ( tab_lines - 1 ) * 255 + STRLEN( objbin ).
objpack-transf_bin = 'X'.
objpack-head_start = 1.
objpack-head_num   = 0.
objpack-body_start = 1.
objpack-body_num   = tab_lines.
objpack-doc_type   = 'BIN'.
objpack-obj_name   = 'ATTACHMENT'.
objpack-obj_descr  = ls_file_access_info-file_name.
APPEND objpack.

* Create receiver list
reclist-receiver = '_____@gmail.com'.  "<-- change address
reclist-rec_type = 'U'.
APPEND reclist.
* Send Message
CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
  EXPORTING
    document_data                     = docdata
     put_in_outbox                     = ' '
     commit_work                       = 'X'     "used from rel.6.10
*   IMPORTING
*     SENT_TO_ALL                        =
*     NEW_OBJECT_ID                     =
  TABLES
    packing_list                      = objpack
    object_header                     = objhead
*    contents_bin                      = objbin
    contents_txt                      = objtxt
*    CONTENTS_HEX                      = objhex
*     OBJECT_PARA                        =
*     OBJECT_PARB                        =
    receivers                         = reclist

        .
IF sy-subrc <> 0.
  MESSAGE ID 'SO' TYPE 'S' NUMBER '023'
          WITH docdata-obj_name.
ENDIF.
WRITE: / 'End of Program'.

Accepted Solutions (1)

Accepted Solutions (1)

gregorw
Active Contributor
0 Kudos

Hi,

perhaps this Code snipplet helps you. I've used it to send the attachments of an order and the attachments of the order items in an action. To simplify building the Mail I've used Thomas Jung's <a href="/people/thomas.jung3/blog/2004/09/08/sending-e-mail-from-abap--version-610-and-higher--bcs-interface">Simple Mail function</a> which I've extended to <a href="/people/gregor.wolf3/blog/2006/05/25/extend-thomas-jung146s-simple-mail-function-with-attachment-filename">maintain an Attachment Filename</a>.

*
* Get Documents attached to Ordered Products
*
        IF it_item IS NOT INITIAL.
          LOOP AT it_item INTO wa_item.
            CREATE OBJECT cref.
*     bo-instid is the guid of the Product
            bo-instid = wa_item-product.
            bo-typeid = 'BUS1178'.
            bo-catid = 'BO'.
            CALL METHOD cref->get_info
              EXPORTING
                business_object = bo
              IMPORTING
                phios           = phios.
*     Append all Attachments of the Order to the Mail
            LOOP AT phios INTO pio.
              FREE: content, lt_content_bin, content_hex, attachment_header.
              io = pio.
              CALL METHOD cl_crm_documents=>get_with_table
               EXPORTING
*     LOIO =
                phio = io
*     AS_IS_MODE =
*     TEXT_AS_STREAM =
                raw_mode = 'X'
               IMPORTING
                file_access_info = file_access_info
*     FILE_CONTENT_ASCII =
                file_content_binary = lt_content_bin
                error = ls_error.
*     BAD_IOS =
              .

* convert table into string
              LOOP AT lt_content_bin REFERENCE INTO lr_content_bin.
                CONCATENATE content lr_content_bin->line INTO content IN BYTE MODE.
              ENDLOOP.

**** Fill Table for Attachment
              WHILE NOT content IS INITIAL.
                v_line = content.
                wa_content_hex-line = v_line.
                APPEND wa_content_hex TO content_hex.
                SHIFT content LEFT BY 255 PLACES IN BYTE MODE.
              ENDWHILE.
* Detect File Type
              READ TABLE file_access_info INTO wa_file_access_info INDEX 1.
              filename = wa_file_access_info-file_name.
              CALL METHOD cl_abap_string_utilities=>del_trailing_blanks
                CHANGING
                  str = filename.
              l = STRLEN( filename ) - 3.
              wa_document-type = filename+l(3).
* wa_document-subject = wa_file_access_info-file_name.
              wa_document-content_hex = content_hex.
* Fill Attachment Header
              wa_attachment_header-line = filename.
              APPEND wa_attachment_header TO attachment_header.
              wa_document-attachment_header = attachment_header.
              APPEND wa_document TO documents.

            ENDLOOP.
          ENDLOOP.
        ENDIF.

Regards

Gregor

Former Member
0 Kudos

It turns out that the part

WHILE NOT content IS INITIAL.

v_line = content.

wa_content_hex-line = v_line.

APPEND wa_content_hex TO content_hex.

SHIFT content LEFT BY 255 PLACES IN BYTE MODE.

ENDWHILE.

takes a considerable amount of time when processing several larger PDF (>2MB)

Answers (0)