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: 

Attach text file on email

Former Member
0 Kudos

Hi,

I must attach one table like file ".txt" at an email.

I use the call function "SO_NEW_DOCUMENT_ATT_SEND_API1" and I can send email with attachment the text file.

When I open this file, it is not properly aligned like I see it into table during program process.

If I change the file extension in ".doc", it's attached and is properly aligned.

Do you konw tell me if I must change some function parameter's settings?

Thanks, Davide.

4 REPLIES 4

venkat_o
Active Contributor
0 Kudos

Davide Leso, Check the sample program used to send .txt file as attachment with tab delimited data. its working. Please try to change receivers id in the program

report  zvenkat_txt_attach.
*--------------------------------------------------------*
"  Data retrieval related declarations
*--------------------------------------------------------*
types:
     begin of t_emp_dat,
       pernr type pa0001-pernr,
       persg type pa0001-persg,
       persk type pa0001-persk,
       plans type pa0001-plans,
       stell type pa0001-stell,
     end of t_emp_dat.
data:
     w_emp_data type t_emp_dat.
data:
     i_emp_data type standard table of t_emp_dat.
*--------------------------------------------------------*
"  declarations
*--------------------------------------------------------*
"Variables
data :
     g_sent_to_all   type sonv-flag,
     g_tab_lines     type i.
"Types
types:
     t_document_data  type  sodocchgi1,
     t_packing_list   type  sopcklsti1,
     t_attachment     type  solisti1,
     t_body_msg       type  solisti1,
     t_receivers      type  somlreci1.
"Workareas
data :
     w_document_data  type  t_document_data,
     w_packing_list   type  t_packing_list,
     w_attachment     type  t_attachment,
     w_body_msg       type  t_body_msg,
     w_receivers      type  t_receivers.
"Internal Tables
data :
     i_document_data  type standard table of t_document_data,
     i_packing_list   type standard table of t_packing_list,
     i_attachment     type standard table of t_attachment,
     i_body_msg       type standard table of t_body_msg,
     i_receivers      type standard table of t_receivers.

*--------------------------------------------------------*
"Start-of-selection.
*--------------------------------------------------------*
start-of-selection.
  perform get_data.
  perform build_xls_data_table.

*--------------------------------------------------------*
  "End-of-selection.
*--------------------------------------------------------*
end-of-selection.
  perform send_txt.

*&--------------------------------------------------------*
  "Form  get_data from PA0001
*&--------------------------------------------------------*
form get_data.

  select pernr
         persg
         persk
         plans
         stell
   from pa0001
   into corresponding fields of table i_emp_data
   up to 4 rows.

endform.                    " get_data
*&---------------------------------------------------------*
"Form  build_xls_data_table
*&---------------------------------------------------------*
form build_xls_data_table.
  "If you have Unicode check active in program attributes then
  "you will need to declare constants as follows.
  class cl_abap_char_utilities definition load.
  constants:
      con_tab  type c value cl_abap_char_utilities=>horizontal_tab,
      con_cret type c value cl_abap_char_utilities=>cr_lf.

  concatenate 'PERNR' 'PERSG' 'PERSK' 'PLANS' 'STELL'
         into  w_attachment
 separated by  con_tab.

  concatenate con_cret
              w_attachment
         into w_attachment.

  append w_attachment to i_attachment.
  clear  w_attachment.

  loop at i_emp_data into w_emp_data.

    concatenate w_emp_data-pernr
                w_emp_data-persg
                w_emp_data-persk
                w_emp_data-plans
                w_emp_data-stell
           into w_attachment
   separated by con_tab.

    concatenate con_cret w_attachment
           into w_attachment.

    append w_attachment to i_attachment.
    clear  w_attachment.
  endloop.

endform.                    "build_xls_data_table
*&----------------------------------------------------------*
"Form  
"---------------
"PACKING LIST
"This table requires information about how the data in the
"tables OBJECT_HEADER, CONTENTS_BIN and CONTENTS_TXT are to
"be distributed to the documents and its attachments.The first
"row is for the document, the following rows are each for one
"attachment.
*&-----------------------------------------------------------*
form send_txt .

  "Subject of the 
  w_document_data-obj_name  = 'MAI_TO_HEAD'.
  w_document_data-obj_descr = 'Regarding TXT attachment by SAP ABAP'.

  "Body 
  perform build_body_of_mai
    using:space,
          'Hi,',
          'I am fine. How are you? How are you doing ? ',
          'This program has been created to send simple mai',
          'with Subject,Body with Address of the sender. ',
          'Regards,',
          'Venkat.O,',
          'SAP HR Technical Consultant.'.

  "Write Packing List for Body
  describe table i_body_msg lines g_tab_lines.
  w_packing_list-head_start = 1.
  w_packing_list-head_num   = 0.
  w_packing_list-body_start = 1.
  w_packing_list-body_num   = g_tab_lines.
  w_packing_list-doc_type   = 'RAW'.
  append w_packing_list to i_packing_list.
  clear  w_packing_list.

  "Write Packing List for Attachment
  w_packing_list-transf_bin = 'X'.
  w_packing_list-head_start = 1.
  w_packing_list-head_num   = 1.
  w_packing_list-body_start = 1.
  describe table i_attachment lines w_packing_list-body_num.
  w_packing_list-doc_type   = 'TXT'.
  w_packing_list-obj_descr  = 'Text Attachment'.
  w_packing_list-obj_name   = 'TXT_ATTACHMENT'.
  w_packing_list-doc_size   = w_packing_list-body_num * 255.
  append w_packing_list to i_packing_list.
  clear  w_packing_list.

  "Fill the document data and get size of attachment
  w_document_data-obj_langu  = sy-langu.
  read table i_attachment into w_attachment index g_tab_lines.
  w_document_data-doc_size = ( g_tab_lines - 1 ) * 255 + strlen( w_attachment ).

  "Receivers List.
  w_receivers-rec_type   = 'U'.  "Internet address
  w_receivers-receiver   = ' '." Please mention receiver here
  w_receivers-com_type   = 'INT'.
  w_receivers-notif_del  = 'X'.
  w_receivers-notif_ndel = 'X'.
  append w_receivers to i_receivers .
  clear:w_receivers.

  "Function module to send mai to Recipients
  call function 'SO_NEW_DOCUMENT_ATT_SEND_API1'
    exporting
      document_data              = w_document_data
      put_in_outbox              = 'X'
      commit_work                = 'X'
    importing
      sent_to_all                = g_sent_to_all
    tables
      packing_list               = i_packing_list
      contents_bin               = i_attachment
      contents_txt               = i_body_msg
      receivers                  = i_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 .
    message i303(me) with 'Mai has been Successfully Sent.'.
  else.
    wait up to 2 seconds.
    "This program starts the SAPconnect send process.
    submit rsconn01 with mode = 'INT'
                    with output = 'X'
                    and return.
  endif.

endform.                    " send_mai
*&-----------------------------------------------------------*
"      Form  build_body_of_mai
*&-----------------------------------------------------------*
form build_body_of_mai  using l_message.

  w_body_msg = l_message.
  append w_body_msg to i_body_msg.
  clear  w_body_msg.

endform.                    " build_body_of_
I hope that it helps u. Regards, Venkat.O

venkat_o
Active Contributor
0 Kudos

as part of the community rule we can not use mail word. so i tried to delete .please check there may be small syntax errors ..

Regards,

Venkat.O

Former Member
0 Kudos

Dear Venkat,

also if I use the character "#" like record's separator the file remain not aligned.

The body's file after the modify that you have suggested me is this:

#2008#04#001#900000#00000048#AGK#00000000#10#100.00#00#01#Q#1 #2008#04#001##00000141#ACD#00000000#00# 0.0#00##Q#1 #2008#04#001##00000169#A1Y#00000000#00# 0.0#00##I#1 #2008#04#001##00000241##00000000#00# 0.0#00##I#1 #2008#04#001##00000243#AGF#00000000#10#100.00#00#01#I#1 #2008#04#001##00000467##00000000#00# 0.0#00##I#1 #2008#04#001#210408#00000700#A1Y#00000000#10# 0.0#00#01#I#1

I must have the body's file like is below:

200804 001 900000 00000048AGK 0000000010 100.00 00 01 Q 1

200804 001 00000141ACD 0000000000 0.0 00 Q 1

200804 001 00000169A1Y 0000000000 0.0 00 I 1

200804 001 00000241 0000000000 0.0 00 I 1

200804 001 00000243AGF 0000000010 100.00 00 01 I 1

200804 001 00000467 0000000000 0.0 00 I 1

200804 001 210408 00000700A1Y 0000000010 0.0 00 01 I 1

Regards, Davide

Former Member
0 Kudos

hi try this ..

TABLES: ekko.

PARAMETERS: p_email TYPE somlreci1-receiver .

TYPES: BEGIN OF t_ekpo,

ebeln TYPE ekpo-ebeln,

ebelp TYPE ekpo-ebelp,

aedat TYPE ekpo-aedat,

matnr TYPE ekpo-matnr,

END OF t_ekpo.

DATA: it_ekpo TYPE STANDARD TABLE OF t_ekpo INITIAL SIZE 0,

wa_ekpo TYPE t_ekpo.

TYPES: BEGIN OF t_charekpo,

ebeln(10) TYPE c,

ebelp(5) TYPE c,

aedat(8) TYPE c,

matnr(18) TYPE c,

END OF t_charekpo.

DATA: wa_charekpo TYPE t_charekpo.

DATA: it_message TYPE STANDARD TABLE OF solisti1 INITIAL SIZE 0

WITH HEADER LINE.

DATA: it_attach TYPE STANDARD TABLE OF solisti1 INITIAL SIZE 0

WITH HEADER LINE.

DATA: t_packing_list LIKE sopcklsti1 OCCURS 0 WITH HEADER LINE,

t_contents LIKE solisti1 OCCURS 0 WITH HEADER LINE,

t_receivers LIKE somlreci1 OCCURS 0 WITH HEADER LINE,

t_attachment LIKE solisti1 OCCURS 0 WITH HEADER LINE,

t_object_header LIKE solisti1 OCCURS 0 WITH HEADER LINE,

w_cnt TYPE i,

w_sent_all(1) TYPE c,

w_doc_data LIKE sodocchgi1,

gd_error TYPE sy-subrc,

gd_reciever TYPE sy-subrc.

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

*START_OF_SELECTION

START-OF-SELECTION.

  • Retrieve sample data from table ekpo

PERFORM data_retrieval.

  • Populate table with detaisl to be entered into .xls file

PERFORM build_xls_data_table.

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

*END-OF-SELECTION

END-OF-SELECTION.

  • Populate message body text

perform populate_email_message_body.

  • Send file by email as .xls speadsheet

PERFORM send_file_as_email_attachment

tables it_message

it_attach

using p_email

'Example .txt documnet attachment'

'txt'

'filename'

' '

' '

' '

changing gd_error

gd_reciever.

  • Instructs mail send program for SAPCONNECT to send email(rsconn01)

PERFORM initiate_mail_execute_program.

&----


*& Form DATA_RETRIEVAL

&----


  • Retrieve data form EKPO table and populate itab it_ekko

----


FORM data_retrieval.

SELECT ebeln ebelp aedat matnr

UP TO 10 ROWS

FROM ekpo

INTO TABLE it_ekpo.

ENDFORM. " DATA_RETRIEVAL

&----


*& Form BUILD_XLS_DATA_TABLE

&----


  • Build data table for .xls document

----


FORM build_xls_data_table.

data: ld_store(50) type c. "Leading zeros

CONSTANTS: con_cret(5) TYPE c VALUE '0D', "OK for non Unicode

con_tab(5) TYPE c VALUE '09'. "OK for non Unicode

*If you have Unicode check active in program attributes thnen you will

*need to declare constants as follows

*class cl_abap_char_utilities definition load.

*constants:

  • con_tab type c value cl_abap_char_utilities=>HORIZONTAL_TAB,

  • con_cret type c value cl_abap_char_utilities=>CR_LF.

CONCATENATE 'EBELN' 'EBELP' 'AEDAT' 'MATNR' INTO it_attach SEPARATED BY con_tab.

CONCATENATE con_cret it_attach INTO it_attach.

APPEND it_attach.

LOOP AT it_ekpo INTO wa_charekpo.

*Modification to retain leading zeros

  • inserts code for excell REPLACE command into ld_store

  • =REPLACE("00100",1,5,"00100")

concatenate '=REPLACE("' wa_charekpo-ebelp '",1,5,"'

wa_charekpo-ebelp '")' into ld_store .

  • concatenate ld_store into .xls file instead of actual value(ebelp)

CONCATENATE wa_charekpo-ebeln ld_store wa_charekpo-aedat wa_charekpo-matnr INTO it_attach SEPARATED BY con_tab.

CONCATENATE con_cret it_attach INTO it_attach.

APPEND it_attach.

ENDLOOP.

ENDFORM. " BUILD_XLS_DATA_TABLE

&----


*& Form SEND_FILE_AS_EMAIL_ATTACHMENT

&----


  • Send email

----


FORM send_file_as_email_attachment tables pit_message

pit_attach

using p_email

p_mtitle

p_format

p_filename

p_attdescription

p_sender_address

p_sender_addres_type

changing p_error

p_reciever.

DATA: ld_error TYPE sy-subrc,

ld_reciever TYPE sy-subrc,

ld_mtitle LIKE sodocchgi1-obj_descr,

ld_email LIKE somlreci1-receiver,

ld_format TYPE so_obj_tp ,

ld_attdescription TYPE so_obj_nam ,

ld_attfilename TYPE so_obj_des ,

ld_sender_address LIKE soextreci1-receiver,

ld_sender_address_type LIKE soextreci1-adr_typ,

ld_receiver LIKE sy-subrc.

ld_email = p_email.

ld_mtitle = p_mtitle.

ld_format = p_format.

ld_attdescription = p_attdescription.

ld_attfilename = p_filename.

ld_sender_address = p_sender_address.

ld_sender_address_type = p_sender_addres_type.

  • Fill the document data.

w_doc_data-doc_size = 1.

  • Populate the subject/generic message attributes

w_doc_data-obj_langu = sy-langu.

w_doc_data-obj_name = 'SAPRPT'.

w_doc_data-obj_descr = ld_mtitle .

w_doc_data-sensitivty = 'F'.

  • Fill the document data and get size of attachment

CLEAR w_doc_data.

READ TABLE it_attach INDEX w_cnt.

w_doc_data-doc_size =

( w_cnt - 1 ) * 255 + STRLEN( it_attach ).

w_doc_data-obj_langu = sy-langu.

w_doc_data-obj_name = 'SAPRPT'.

w_doc_data-obj_descr = ld_mtitle.

w_doc_data-sensitivty = 'F'.

CLEAR t_attachment.

REFRESH t_attachment.

t_attachment[] = pit_attach[].

  • Describe the body of the message

CLEAR t_packing_list.

REFRESH t_packing_list.

t_packing_list-transf_bin = space.

t_packing_list-head_start = 1.

t_packing_list-head_num = 0.

t_packing_list-body_start = 1.

DESCRIBE TABLE it_message LINES t_packing_list-body_num.

t_packing_list-doc_type = 'RAW'.

APPEND t_packing_list.

  • Create attachment notification

t_packing_list-transf_bin = 'X'.

t_packing_list-head_start = 1.

t_packing_list-head_num = 1.

t_packing_list-body_start = 1.

DESCRIBE TABLE t_attachment LINES t_packing_list-body_num.

t_packing_list-doc_type = ld_format.

t_packing_list-obj_descr = ld_attdescription.

t_packing_list-obj_name = ld_attfilename.

t_packing_list-doc_size = t_packing_list-body_num * 255.

APPEND t_packing_list.

  • Add the recipients email address

CLEAR t_receivers.

REFRESH t_receivers.

t_receivers-receiver = ld_email.

t_receivers-rec_type = 'U'.

t_receivers-com_type = 'INT'.

t_receivers-notif_del = 'X'.

t_receivers-notif_ndel = 'X'.

APPEND t_receivers.

CALL FUNCTION 'SO_DOCUMENT_SEND_API1'

EXPORTING

document_data = w_doc_data

put_in_outbox = 'X'

sender_address = ld_sender_address

sender_address_type = ld_sender_address_type

commit_work = 'X'

IMPORTING

sent_to_all = w_sent_all

TABLES

packing_list = t_packing_list

contents_bin = t_attachment

contents_txt = it_message

receivers = t_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.

  • Populate zerror return code

ld_error = sy-subrc.

  • Populate zreceiver return code

LOOP AT t_receivers.

ld_receiver = t_receivers-retrn_code.

ENDLOOP.

ENDFORM.

&----


*& Form INITIATE_MAIL_EXECUTE_PROGRAM

&----


  • Instructs mail send program for SAPCONNECT to send email.

----


FORM initiate_mail_execute_program.

WAIT UP TO 2 SECONDS.

SUBMIT rsconn01 WITH mode = 'INT'

WITH output = 'X'

AND RETURN.

ENDFORM. " INITIATE_MAIL_EXECUTE_PROGRAM

&----


*& Form POPULATE_EMAIL_MESSAGE_BODY

&----


  • Populate message body text

----


form populate_email_message_body.

REFRESH it_message.

it_message = 'Please find attached a list test ekpo records'.

APPEND it_message.

endform. " POPULATE_EMAIL_MESSAGE_BODY

regards,

venkat.