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

How Write a Program to send TWO attachments in the same mail.

Former Member
0 Likes
2,952

Hello,

How to write a ABAP program so that when u run this program it shd send two 0r more files as an attachment.

Presently i know how to send a single attachment but not the multiple attachment.

i.e. by using "send_api" function module .

please reply ASAP i need this urgently.

7 REPLIES 7
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
1,167

I did this one time before, I think its as easy as adding another entry in the Packing List.

Regards,

Rich Heilman

Read only

0 Likes
1,167

Hi Rich

Can u explain it briefly about this....

i am little confused by ur reply.

Regards

Kali

Read only

0 Likes
1,167

Here is a sample program. It creates two txt files as attachments.



report zrich_0003.


data: itcpo like itcpo,
      tab_lines like sy-tabix.


* Variables for EMAIL functionality
data: maildata   like sodocchgi1.
data: mailpack   like sopcklsti1 occurs 2 with header line.
data: mailhead   like solisti1 occurs 1 with header line.
data: mailbin    like solisti1 occurs 10 with header line.
data: mailtxt    like solisti1 occurs 10 with header line.
data: mailrec    like somlrec90 occurs 0  with header line.
data: solisti1   like solisti1 occurs 0 with header line.


perform send_form_via_email.


************************************************************************
*       FORM  SEND_FORM_VIA_EMAIL                                      *
************************************************************************
form  send_form_via_email.

  clear:    maildata, mailtxt, mailbin, mailpack, mailhead, mailrec.
  refresh:  mailtxt, mailbin, mailpack, mailhead, mailrec.

* Creation of the document to be sent File Name
  maildata-obj_name = 'TEST'.
* Mail Subject
  maildata-obj_descr = 'Subject'.

* Mail Contents
  mailtxt-line = 'Here is your file'.
  append mailtxt.

* Prepare Packing List
  perform prepare_packing_list.

* Set recipient - email address here!!!
  mailrec-receiver = you@yourcompany.com'.
  mailrec-rec_type  = 'U'.
  append mailrec.

* Sending the document
  call function 'SO_NEW_DOCUMENT_ATT_SEND_API1'
       exporting
            document_data              = maildata
            put_in_outbox              = ' '
       tables
            packing_list               = mailpack
            object_header              = mailhead
            contents_bin               = mailbin
            contents_txt               = mailtxt
            receivers                  = mailrec
       exceptions
            too_many_receivers         = 1
            document_not_sent          = 2
            operation_no_authorization = 4
            others                     = 99.

endform.

************************************************************************
*      Form  PREPARE_PACKING_LIST
************************************************************************
form prepare_packing_list.

  clear:    mailpack, mailbin, mailhead.
  refresh:  mailpack, mailbin, mailhead.

  describe table mailtxt lines tab_lines.
  read table mailtxt index tab_lines.
  maildata-doc_size = ( tab_lines - 1 ) * 255 + strlen( mailtxt ).

* Creation of the entry for the compressed document
  clear mailpack-transf_bin.
  mailpack-head_start = 1.
  mailpack-head_num = 0.
  mailpack-body_start = 1.
  mailpack-body_num = tab_lines.
  mailpack-doc_type = 'RAW'.
  append mailpack.

  mailhead = 'TEST.TXT'.
  append mailhead.



* File 1
  mailbin = 'This is file 1'.
  append mailbin.

  describe table mailbin lines tab_lines.

  mailpack-transf_bin = 'X'.
  mailpack-head_start = 1.
  mailpack-head_num = 1.
  mailpack-body_start = 1.
  mailpack-body_num = tab_lines.
  mailpack-doc_type = 'TXT'.
  mailpack-obj_name = 'TEST1'.
  mailpack-obj_descr = 'Subject'.
  mailpack-doc_size = tab_lines * 255.
  append mailpack.



*File 2
  mailbin = 'This is file 2'.
  append mailbin.

  data: start type i.
  data: end type i.

  start = tab_lines + 1.

  describe table mailbin lines end.

  mailpack-transf_bin = 'X'.
  mailpack-head_start = 1.
  mailpack-head_num = 1.
  mailpack-body_start = start.
  mailpack-body_num = end.
  mailpack-doc_type = 'TXT'.
  mailpack-obj_name = 'TEST2'.
  mailpack-obj_descr = 'Subject'.
  mailpack-doc_size = tab_lines * 255.
  append mailpack.


endform.

Regards,

Rich Heilman

Read only

Former Member
0 Likes
1,167

Hi kali,

I assume u must be knowing

how to send 1 attachment.

1. OBJPACK-OBJ_NAME = 'ATTACHMENT'.

OBJPACK-OBJ_DESCR = 'Payslip'.

append OBJPACK.

2. objpack is the table which contains

the attachment.

3. Hence, just ADD ONE MORE Entry to it

for 2 attachments !

regards,

amit m.

Read only

former_member186741
Active Contributor
0 Likes
1,167

I think the currently preferred way is to use the approach in the SAP example abap BCS_EXAMPLE_5. Multiple attachemnts can be done by calling METHOD document->add_attachment several times.

Read only

Former Member
0 Likes
1,167

this is simpple FM which can can send any type of Attachment.

FUNCTION zsend_mail_attachment.
*"----------------------------------------------------------------------
*"*"Local interface:
*"  IMPORTING
*"     REFERENCE(V_FILE_PATH) TYPE  STRING OPTIONAL
*"     REFERENCE(V_SUBJECT) TYPE  SO_OBJ_DES
*"  TABLES
*"      IT_RECEIVERS STRUCTURE  SOMLRECI1
*"      IT_MESSAGE STRUCTURE  SOLISTI1
*"----------------------------------------------------------------------

  DATA: gd_cnt TYPE i,
        gd_sent_all(1) TYPE c,
        gd_doc_data LIKE sodocchgi1,
        gd_error TYPE sy-subrc.

  DATA objbin LIKE solisti1 OCCURS 0 WITH HEADER LINE.

  DATA : it_packing_list LIKE sopcklsti1 OCCURS 0 WITH HEADER LINE.

* Binary store for File
  DATA : BEGIN OF it_file OCCURS 0,
           row(255),
         END OF it_file.

* splitting of the filepath
  DATA : BEGIN OF i_split OCCURS 0,
           row(50),
         END OF i_split.

  DATA tab_lines LIKE sy-tabix.

  REFRESH : it_file, objbin, it_packing_list, i_split.
  CLEAR   : it_file, objbin, it_packing_list, i_split.

  DESCRIBE TABLE it_message LINES tab_lines.
  READ     TABLE it_message INDEX tab_lines.

  gd_doc_data-doc_size = ( tab_lines - 1 ) * 255 + STRLEN( it_message ).
  gd_doc_data-obj_langu  = sy-langu.
  gd_doc_data-obj_name   = 'SENDFILE'.
  gd_doc_data-obj_descr  = v_subject.
  gd_doc_data-sensitivty = 'O'.

  CLEAR it_packing_list.

  it_packing_list-head_start = 1.
  it_packing_list-head_num   = 0.
  it_packing_list-body_start = 1.
  it_packing_list-doc_type   = 'RAW'.
  it_packing_list-body_num   = tab_lines.
  APPEND it_packing_list.

  IF v_file_path IS NOT INITIAL.
    DATA : v_len      TYPE i,
           v_index    LIKE sy-index,
           v_doc_type TYPE  so_obj_tp,
           v_filename TYPE  so_obj_des .

    v_len = STRLEN( v_file_path ) - 3.
    v_doc_type = v_file_path+v_len(3) .
    TRANSLATE v_doc_type TO UPPER CASE .

    SPLIT v_file_path AT '' INTO TABLE i_split .
    DESCRIBE TABLE i_split LINES v_index .
    READ TABLE i_split INDEX v_index .
    v_filename = i_split-row .
    v_len = STRLEN( v_filename ) - 4.
    v_filename = v_filename(v_len) .

    CALL FUNCTION 'GUI_UPLOAD'
      EXPORTING
        filename = v_file_path
        filetype = 'BIN'
      TABLES
        data_tab = it_file.

    LOOP AT it_file.
      MOVE it_file-row TO objbin-line.
      APPEND objbin.
    ENDLOOP.

    CLEAR it_packing_list.

    DESCRIBE TABLE objbin LINES tab_lines.

    it_packing_list-transf_bin = 'X'.
    it_packing_list-head_start = 1.
    it_packing_list-head_num   = 1.
    it_packing_list-body_start = 1.
    it_packing_list-doc_type   = v_doc_type.
    it_packing_list-body_num   = tab_lines.
    it_packing_list-doc_size   = tab_lines * 255.
    it_packing_list-obj_descr  = v_filename.
    APPEND it_packing_list.
  ENDIF.

*  it_receivers-receiver = 'abhiaries@yahoo.com'.
*  it_receivers-rec_type = 'U'.
*  it_receivers-com_type = 'INT'.
*  APPEND it_receivers .

* Call the FM to post the message to SAPMAIL
  CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
    EXPORTING
      document_data = gd_doc_data
      put_in_outbox = 'X'
      commit_work   = 'X'
*   IMPORTING
*     sent_to_all = gd_sent_all
    TABLES
      packing_list = it_packing_list
      contents_txt = it_message
      contents_bin = objbin
      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.

ENDFUNCTION.

i have made it for only one attchmenet, for multiple attachmenet u can make more parameters <b>v_file_path_1, v_file_path_2, v_file_path_3</b> like wise do the coding as i have done for <b>v_file_path</b>

hope this will solves ur query.....

abhishek

Read only

Former Member
0 Likes
1,167

Hi..

I want some tipes and sample program

how to write a BDC program it will update MY report

i have already develop customer Interest and balance Enquiry Screen

report it will update automatically Tr code fb02.

give some tipes and idea how to develop the BDC Program .

By using this the Tr code automatically update