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: 

Attaching a tab delimited file to mail

Former Member
0 Kudos
1,442

Hi,

I have to attach a tab delimited file to a mail and sent it using Function Module 'SO_DOCUMENT_SEND_API1'.

I have filled in the following details but i am not sure about what should be given for 'lt_packing_list-doc_type'. Should it be a 'TXT 'or any other file format. Some one has suggested me a 'CSV' but i guess for that the data should be separated by a comma and i need a tab delimited file.

Please suggest me what needs to be done.

Thank you,

Taher


  DATA:
        con_tab  TYPE c VALUE cl_abap_char_utilities=>horizontal_tab,
        con_cret TYPE c VALUE cl_abap_char_utilities=>cr_lf.

  LOOP AT gt_output INTO ls_output.

    CONCATENATE ls_output-email
                ls_output-pos
                ls_output-txt
                ls_output-func
                ls_output-mail
             INTO gt_attach SEPARATED BY con_tab.

    CONCATENATE con_cret gt_attach  INTO gt_attach.
    CONDENSE gt_attach.
    APPEND  gt_attach.

* Fill the document data.
  lv_xdocdata-doc_size = 1.

* Populate the subject/generic message attributes
  lv_xdocdata-obj_langu = sy-langu.
  lv_xdocdata-obj_name  = 'SAPRPT'.
  lv_xdocdata-obj_descr = 'Report' .

* Fill the document data and get size of attachment
  CLEAR lv_xdocdata.
  READ TABLE gt_attach INDEX lv_xcnt.
  lv_xdocdata-doc_size =
     ( lv_xcnt - 1 ) * 255 + STRLEN( gt_attach ).
  lv_xdocdata-obj_langu  = sy-langu.
  lv_xdocdata-obj_name   = 'SAPRPT'.
  lv_xdocdata-obj_descr  = ' Report'.
  CLEAR lt_attachment.  REFRESH lt_attachment.
  lt_attachment[] = gt_attach[].

* Describe the body of the message
  CLEAR lt_packing_list.  REFRESH lt_packing_list.
  lt_packing_list-transf_bin = space.
  lt_packing_list-head_start = 1.
  lt_packing_list-head_num = 0.  lt_packing_list-body_start = 1.
  DESCRIBE TABLE lt_message LINES lt_packing_list-body_num.
  lt_packing_list-doc_type = 'RAW'.
  APPEND lt_packing_list.

* Create attachment notification
  lt_packing_list-transf_bin = 'X'.
  lt_packing_list-head_start = 1.
  lt_packing_list-head_num   = 1.
  lt_packing_list-body_start = 1.

  DESCRIBE TABLE lt_attachment LINES lt_packing_list-body_num.
  lt_packing_list-doc_type   =  ?????. "TXT or any other format
  lt_packing_list-obj_descr  =  'Report'.
  lt_packing_list-obj_name   =  'Report'.
  lt_packing_list-doc_size   =  lt_packing_list-body_num * 255.
  APPEND lt_packing_list.

* Add the recipients email address
  LOOP AT gt_receivers INTO ls_receivers.
    CLEAR lt_receivers.
    lt_receivers-receiver = ls_receivers.
    lt_receivers-rec_type = 'U'.
    lt_receivers-com_type = 'INT'.
    APPEND lt_receivers.
  ENDLOOP.

1 ACCEPTED SOLUTION

MarcinPciak
Active Contributor
0 Kudos
372

Hi Taher,

Which table you populate your data to (CONTENTS_BIN,CONTENTS_TXT,CONTENTS_HEX) ?.

As both the message and attachment reside one table (contents_txt) in it_packing_list body of message should start with 1 line, but of an attachment when body of message ends. This is:


* Describe the body of the message
  CLEAR lt_packing_list.  REFRESH lt_packing_list.
  lt_packing_list-transf_bin = space.
  lt_packing_list-head_start = 1.
  lt_packing_list-head_num = 0.  lt_packing_list-body_start = 1.
  DESCRIBE TABLE lt_message LINES lt_packing_list-body_num.
  lt_packing_list-doc_type = 'RAW'.
  APPEND lt_packing_list.
 
* Create attachment notification
  lt_packing_list-transf_bin = 'X'.
  lt_packing_list-head_start = 1.
  lt_packing_list-head_num   = 1.
  lt_packing_list-body_start =  "start of attachemnt with next line after body of message 

Next thing is what I appointed before. Both should be passed as ASCII so in both lt_packing_list-transf_bin = space.

Please see my code.


CONSTANTS: con_cret(2) type c VALUE cl_abap_char_utilities=>cr_lf,
           con_tab(2)  TYPE c VALUE cl_abap_char_utilities=>horizontal_tab.

DATA: doc_attr TYPE sodocchgi1,
      it_packing_list TYPE TABLE OF sopcklsti1 WITH HEADER LINE,
      it_message TYPE TABLE OF solisti1 WITH HEADER LINE,
      it_attachment TYPE TABLE OF solisti1 WITH HEADER LINE,
      it_txt TYPE TABLE OF solisti1 WITH HEADER LINE,
      it_receivers TYPE TABLE OF somlreci1 WITH HEADER LINE.

"Document attributes
doc_attr-obj_name = 'EXT_MAIL'.
doc_attr-obj_descr = 'External test mail'.   "title
doc_attr-obj_langu = sy-langu.
doc_attr-sensitivty = 'F'.      "functional message

"Message in ASCII (txt) format
APPEND 'This is body message' TO it_message.   "message is from 1 to 2
APPEND 'and second line for message.' TO it_message.
APPEND LINES OF it_message TO it_txt.

"Determine how data are distrtibuted to document and attachment
"First line in it_packing describes message body
it_packing_list-transf_bin = space.  "ASCII format
it_packing_list-body_start = 1.      "message body starts from 1st line
DESCRIBE TABLE it_message LINES it_packing_list-body_num.  "lines in it_txt table for message body
it_packing_list-doc_type = 'RAW'.
APPEND it_packing_list.

"Attachment in ASCII (txt) format
CONCATENATE '1first' 'second' 'third' 'fourth' into it_attachment SEPARATED BY con_tab.   "first line
CONCATENATE con_cret it_attachment.
APPEND it_attachment.

CONCATENATE '2first' 'second' 'third' 'fourth' into it_attachment SEPARATED BY con_tab. "second line
CONCATENATE it_attachment con_cret into it_attachment.
APPEND it_attachment.

APPEND LINES OF it_attachment TO it_txt.

"Further lines in it_packing describe attachment
CLEAR it_packing_list.
it_packing_list-transf_bin = space.  "ASCII format
it_packing_list-body_start = 3.
DESCRIBE TABLE it_attachment LINES it_packing_list-body_num.
it_packing_list-doc_type = 'txt'.
it_packing_list-obj_name = 'Test attachment'.
it_packing_list-obj_descr = 'Title of an attachment'.
it_packing_list-obj_langu = sy-langu.
"size od attachment = length of last line + all remaining lines * 255
READ TABLE it_attachment INDEX it_packing_list-body_num.
it_packing_list-doc_size = STRLEN( it_attachment ) + 255 * ( it_packing_list-body_num - 1 ).
APPEND it_packing_list.

"Receivers
it_receivers-receiver = "some mail address.
it_receivers-rec_type = 'U'.  "internet address
it_receivers-com_type = 'INT'. "send via internet
APPEND it_receivers.

CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
  EXPORTING
    document_data                    = doc_attr
    put_in_outbox                    = 'X'
    commit_work                      = 'X'
* IMPORTING
*   SENT_TO_ALL                      =
*   NEW_OBJECT_ID                    =
  TABLES
    packing_list                     = it_packing_list
*   OBJECT_HEADER                    =
*   CONTENTS_BIN                     =
   contents_txt                     = it_txt
*   CONTENTS_HEX                     =
*   OBJECT_PARA                      =
*   OBJECT_PARB                      =
    receivers                        = it_receivers.
* 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.
  WAIT UP TO 2 SECONDS.
  SUBMIT rsconn01 WITH mode = 'INT'
*                    WITH ouput = 'X'
                  AND RETURN.
ENDIF.

I am using SO01 t-code to see the document and attachemnt.

I am sure when you get rid of the differences with the codes you will get desired result.

Regards

Marcin

7 REPLIES 7

MarcinPciak
Active Contributor
0 Kudos
372

Hi,

For tab delimition the most suitable format is XLS but with TXT will also work.

Regards

Marcin

0 Kudos
372

Yes i tried that but when I open my file from transaction SOST I can see the data as below. Also there a extra characters being added after every alphabet.

E m a i l A d d r e s s P o s i t i o n T i t l e C o u n t r y T e x t F u n c t i o n T e x t L i n e m a n a g e r E m a i l A d d r e s s

B u s i n e s s D e v e l o p m e n t

J A M E S . B O N D . T E S T I N G . M A I L _ E M P L O Y E E _ T F 9 9 _ A L L _ T E S T @ T E S T H R H e a d D u m m y A r e a

0 Kudos
372

When you are sending flat file you must use ASCII format instead of bin


lt_packing_list-transf_bin = space. "set space for ASCII

Regards

Marcin

0 Kudos
372

Hi Marcin,

This is not working either.

Any thing else that i need to consider.

I just need to specify something. When instead of TXT i use XLS as the extension then these spaces are not added and the display is normal.

Kindly suggest.

MarcinPciak
Active Contributor
0 Kudos
373

Hi Taher,

Which table you populate your data to (CONTENTS_BIN,CONTENTS_TXT,CONTENTS_HEX) ?.

As both the message and attachment reside one table (contents_txt) in it_packing_list body of message should start with 1 line, but of an attachment when body of message ends. This is:


* Describe the body of the message
  CLEAR lt_packing_list.  REFRESH lt_packing_list.
  lt_packing_list-transf_bin = space.
  lt_packing_list-head_start = 1.
  lt_packing_list-head_num = 0.  lt_packing_list-body_start = 1.
  DESCRIBE TABLE lt_message LINES lt_packing_list-body_num.
  lt_packing_list-doc_type = 'RAW'.
  APPEND lt_packing_list.
 
* Create attachment notification
  lt_packing_list-transf_bin = 'X'.
  lt_packing_list-head_start = 1.
  lt_packing_list-head_num   = 1.
  lt_packing_list-body_start =  "start of attachemnt with next line after body of message 

Next thing is what I appointed before. Both should be passed as ASCII so in both lt_packing_list-transf_bin = space.

Please see my code.


CONSTANTS: con_cret(2) type c VALUE cl_abap_char_utilities=>cr_lf,
           con_tab(2)  TYPE c VALUE cl_abap_char_utilities=>horizontal_tab.

DATA: doc_attr TYPE sodocchgi1,
      it_packing_list TYPE TABLE OF sopcklsti1 WITH HEADER LINE,
      it_message TYPE TABLE OF solisti1 WITH HEADER LINE,
      it_attachment TYPE TABLE OF solisti1 WITH HEADER LINE,
      it_txt TYPE TABLE OF solisti1 WITH HEADER LINE,
      it_receivers TYPE TABLE OF somlreci1 WITH HEADER LINE.

"Document attributes
doc_attr-obj_name = 'EXT_MAIL'.
doc_attr-obj_descr = 'External test mail'.   "title
doc_attr-obj_langu = sy-langu.
doc_attr-sensitivty = 'F'.      "functional message

"Message in ASCII (txt) format
APPEND 'This is body message' TO it_message.   "message is from 1 to 2
APPEND 'and second line for message.' TO it_message.
APPEND LINES OF it_message TO it_txt.

"Determine how data are distrtibuted to document and attachment
"First line in it_packing describes message body
it_packing_list-transf_bin = space.  "ASCII format
it_packing_list-body_start = 1.      "message body starts from 1st line
DESCRIBE TABLE it_message LINES it_packing_list-body_num.  "lines in it_txt table for message body
it_packing_list-doc_type = 'RAW'.
APPEND it_packing_list.

"Attachment in ASCII (txt) format
CONCATENATE '1first' 'second' 'third' 'fourth' into it_attachment SEPARATED BY con_tab.   "first line
CONCATENATE con_cret it_attachment.
APPEND it_attachment.

CONCATENATE '2first' 'second' 'third' 'fourth' into it_attachment SEPARATED BY con_tab. "second line
CONCATENATE it_attachment con_cret into it_attachment.
APPEND it_attachment.

APPEND LINES OF it_attachment TO it_txt.

"Further lines in it_packing describe attachment
CLEAR it_packing_list.
it_packing_list-transf_bin = space.  "ASCII format
it_packing_list-body_start = 3.
DESCRIBE TABLE it_attachment LINES it_packing_list-body_num.
it_packing_list-doc_type = 'txt'.
it_packing_list-obj_name = 'Test attachment'.
it_packing_list-obj_descr = 'Title of an attachment'.
it_packing_list-obj_langu = sy-langu.
"size od attachment = length of last line + all remaining lines * 255
READ TABLE it_attachment INDEX it_packing_list-body_num.
it_packing_list-doc_size = STRLEN( it_attachment ) + 255 * ( it_packing_list-body_num - 1 ).
APPEND it_packing_list.

"Receivers
it_receivers-receiver = "some mail address.
it_receivers-rec_type = 'U'.  "internet address
it_receivers-com_type = 'INT'. "send via internet
APPEND it_receivers.

CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
  EXPORTING
    document_data                    = doc_attr
    put_in_outbox                    = 'X'
    commit_work                      = 'X'
* IMPORTING
*   SENT_TO_ALL                      =
*   NEW_OBJECT_ID                    =
  TABLES
    packing_list                     = it_packing_list
*   OBJECT_HEADER                    =
*   CONTENTS_BIN                     =
   contents_txt                     = it_txt
*   CONTENTS_HEX                     =
*   OBJECT_PARA                      =
*   OBJECT_PARB                      =
    receivers                        = it_receivers.
* 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.
  WAIT UP TO 2 SECONDS.
  SUBMIT rsconn01 WITH mode = 'INT'
*                    WITH ouput = 'X'
                  AND RETURN.
ENDIF.

I am using SO01 t-code to see the document and attachemnt.

I am sure when you get rid of the differences with the codes you will get desired result.

Regards

Marcin

0 Kudos
372

Hi Marcin,

Thank you very much for your help.

My issue is resolved and the mail attachment is coming fine.

I suggest the following format for putting the tab and new line so that the data is formated properly.


"This is the first line of the file attached
CONCATENATE '1first' con_tab 'second' con_tab 'third' con_tab 'fourth' con_tab 
INTO it_attachment."SEPARATED BY con_tab.  
APPEND it_attachment.
 
"This is the 2nd line of the file attached
clear it_attachment.
CONCATENATE con_cret '2first' con_tab 'second' con_tab 'third' con_tab 'fourth' con_tab 
INTO it_attachment." SEPARATED BY con_tab. APPEND it_attachment.
 
"This is the 3rd line of the file attached
CONCATENATE con_cret '3first' con_tab 'second' con_tab 'third' con_tab 'fourth' con_tab 
INTO it_attachment." SEPARATED BY con_tab. APPEND it_attachment.

Thank you very much again.

Regards,

Taher

Former Member
0 Kudos
372

Some other problem related to the same issue.

Hi,

I have developed a report to attach a tab delimited ".TXT" file to a mail and sent it across.

But there are some issues.

I have used the following to insert a tab and insert a newline respectively.


con_tab  TYPE c VALUE cl_abap_char_utilities=>horizontal_tab,
con_cret TYPE c VALUE cl_abap_char_utilities=>cr_lf.

Now the problem is that in the attachment file shoukd contain data in the following format

test test1 test2 test3

test test1 test2 test3

But it is comming like this

test test1 test2 test3 #test test1 test2

test3 #test test1 test2 test3

As this is a tab delimited file to be read by a legacy system this format will nto work correctly.

Can any please suggest as what can be done so that the mail attahment contains data in the correct format.

Thank you,

Taher