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 to attach excel format email

former_member295881
Contributor
0 Likes
1,954

Hello Experts,

I had a requirement where I need to email report to the user. Currently the email part works as shown below but requirement has been changed. Now client wants email in Excel format instead of text. Can anybody please suggest what change I should make in the following code to send email in excel format instead of text.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

constants:
         c_subject(54) type c value 'Incomplete Re-bill order execution summary',

   data: lv_bname   like usr21-bname,
         lv_persnum like usr21-persnumber,
         lv_addrnum like usr21-addrnumber,
         lv_email   like adr6-smtp_addr,
         lv_string  type string,
         lv_row     type string,
         lv_new_amt type char10.

   data: i_packing     like sopcklsti1  occurs 0 with header line,
         i_objbin      like solisti1    occurs 0 with header line,
         i_reclist     like somlreci1   occurs 0 with header line,
         doc_chng      like sodocchgi1,
         l_tabix       like sy-tabix,
         l_lines       type i.

if pemail1 is not initial.
     i_reclist-receiver = pemail1.
     i_reclist-rec_type = 'U'.
     append i_reclist.
   endif.

   if pemail2 is not initial.
     i_reclist-receiver = pemail2.
     i_reclist-rec_type = 'U'.
     append i_reclist.
   endif.

   if pemail3 is not initial.
     i_reclist-receiver = pemail3.
     i_reclist-rec_type = 'U'.
     append i_reclist.
   endif.

   if pemail4 is not initial.
     i_reclist-receiver = pemail4.
     i_reclist-rec_type = 'U'.
     append i_reclist.
   endif.

* Email header info
   doc_chng-obj_name  = c_subject.
   doc_chng-obj_descr = c_subject.

* Create the message packing data
   clear i_packing-transf_bin.
   i_packing-head_start   = 1.
   i_packing-head_num     = 0.
   i_packing-body_start   = 1.
   i_packing-body_num     = 0.
   i_packing-doc_type     = 'RAW'.
   append i_packing.

* Report Summary header
  "concatenate text-001 sy-uname into lv_string separated by space.
   lv_string = text-001.
   append lv_string to i_objbin.
   append text-002 to i_objbin.
   append cl_abap_char_utilities=>cr_lf to i_objbin.

   loop at lt_output into lw_output.
      lv_new_amt = lw_output-new_amt.
     concatenate lw_output-zrerate '|'
                 lw_output-kunrg '|'
                 lw_output-name1 '|'
                 lw_output-auart '|'
                 lw_output-initiator '|'
                 lv_new_amt '|'
                 lw_output-waerk '|'
                 lw_output-erdat '|'
                 lw_output-idocnum '|'
                 lw_output-idocerror
                   into lv_row.
     append lv_row to i_objbin.
     clear: lv_new_amt, lv_row.
   endloop.

   append text-002 to i_objbin.
   append cl_abap_char_utilities=>cr_lf to i_objbin.
   append cl_abap_char_utilities=>cr_lf to i_objbin.

   describe table i_objbin lines l_lines.

* create the message packing data
   i_packing-transf_bin   = 'X '.
   i_packing-head_start   = 1.
   i_packing-head_num     = 1.
   i_packing-body_start   = 1.
   i_packing-body_num     = l_lines.
   i_packing-doc_type     = 'RAW'.
   i_packing-obj_name     = 'EMAIL'.
   i_packing-obj_descr    = 'Rebill order update summary'.
   i_packing-doc_size     = l_lines * 255.
   append i_packing.

* send email
   call function 'SO_NEW_DOCUMENT_ATT_SEND_API1'
     exporting
       document_data              = doc_chng
       put_in_outbox              = ''
       commit_work                = 'X'
     tables
       packing_list               = i_packing
       contents_bin               = i_objbin
       receivers                  = i_reclist
     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 e188(zcpc) with 'Error sending email'.
   endif.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

Many thanks in advance.

Hello Experts,

I had a requirement where I need to email report to the user. Currently the email part works as shown below but requirement has been changed. Now client wants email in Excel format instead of text. Can anybody please suggest what change I should make in the following code to send email in excel format instead of text.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

constants:
         c_subject(54) type c value 'Incomplete Re-bill order execution summary',

   data: lv_bname   like usr21-bname,
         lv_persnum like usr21-persnumber,
         lv_addrnum like usr21-addrnumber,
         lv_email   like adr6-smtp_addr,
         lv_string  type string,
         lv_row     type string,
         lv_new_amt type char10.

   data: i_packing     like sopcklsti1  occurs 0 with header line,
         i_objbin      like solisti1    occurs 0 with header line,
         i_reclist     like somlreci1   occurs 0 with header line,
         doc_chng      like sodocchgi1,
         l_tabix       like sy-tabix,
         l_lines       type i.

if pemail1 is not initial.
     i_reclist-receiver = pemail1.
     i_reclist-rec_type = 'U'.
     append i_reclist.
   endif.

   if pemail2 is not initial.
     i_reclist-receiver = pemail2.
     i_reclist-rec_type = 'U'.
     append i_reclist.
   endif.

   if pemail3 is not initial.
     i_reclist-receiver = pemail3.
     i_reclist-rec_type = 'U'.
     append i_reclist.
   endif.

   if pemail4 is not initial.
     i_reclist-receiver = pemail4.
     i_reclist-rec_type = 'U'.
     append i_reclist.
   endif.

* Email header info
   doc_chng-obj_name  = c_subject.
   doc_chng-obj_descr = c_subject.

* Create the message packing data
   clear i_packing-transf_bin.
   i_packing-head_start   = 1.
   i_packing-head_num     = 0.
   i_packing-body_start   = 1.
   i_packing-body_num     = 0.
   i_packing-doc_type     = 'RAW'.
   append i_packing.

* Report Summary header
  "concatenate text-001 sy-uname into lv_string separated by space.
   lv_string = text-001.
   append lv_string to i_objbin.
   append text-002 to i_objbin.
   append cl_abap_char_utilities=>cr_lf to i_objbin.

   loop at lt_output into lw_output.
      lv_new_amt = lw_output-new_amt.
     concatenate lw_output-zrerate '|'
                 lw_output-kunrg '|'
                 lw_output-name1 '|'
                 lw_output-auart '|'
                 lw_output-initiator '|'
                 lv_new_amt '|'
                 lw_output-waerk '|'
                 lw_output-erdat '|'
                 lw_output-idocnum '|'
                 lw_output-idocerror
                   into lv_row.
     append lv_row to i_objbin.
     clear: lv_new_amt, lv_row.
   endloop.

   append text-002 to i_objbin.
   append cl_abap_char_utilities=>cr_lf to i_objbin.
   append cl_abap_char_utilities=>cr_lf to i_objbin.

   describe table i_objbin lines l_lines.

* create the message packing data
   i_packing-transf_bin   = 'X '.
   i_packing-head_start   = 1.
   i_packing-head_num     = 1.
   i_packing-body_start   = 1.
   i_packing-body_num     = l_lines.
   i_packing-doc_type     = 'RAW'.
   i_packing-obj_name     = 'EMAIL'.
   i_packing-obj_descr    = 'Rebill order update summary'.
   i_packing-doc_size     = l_lines * 255.
   append i_packing.

* send email
   call function 'SO_NEW_DOCUMENT_ATT_SEND_API1'
     exporting
       document_data              = doc_chng
       put_in_outbox              = ''
       commit_work                = 'X'
     tables
       packing_list               = i_packing
       contents_bin               = i_objbin
       receivers                  = i_reclist
     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 e188(zcpc) with 'Error sending email'.
   endif.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

Many thanks in advance.

5 REPLIES 5
Read only

aferngas
Active Participant
0 Likes
1,532

Hi,

You can use the ABAP2XLSX project to create the attachment. Take a look at the project and note that you must generate a header to include the file extension XLSX (4 characters instead of the normal 3). It's a solved issue in note 1459896.

Regards,

Alex

Read only

Former Member
0 Likes
1,532

hi,

*&---------------------------------------------------------------------*

*& REPORT  ZATTACH_EMAIL                                               *

*&---------------------------------------------------------------------*

*& Description: Program to upload attachments and send it to the       *

*&              required Email ID                                      *

*&---------------------------------------------------------------------*

*& Modification Log:                                                   *

*&                                                                     *

*& Date       Name                User ID             Transport #      *

*&            Description of Modifications                             *

*&---------------------------------------------------------------------*

*&            Amit Bisht                                               *

*&           -Initial version                                          *

*&---------------------------------------------------------------------*

REPORT  zattach_email  MESSAGE-ID zz

                       LINE-SIZE 132

                       NO STANDARD PAGE HEADING.

*----------------------------------------------------------------------*

*                     TABLES                                           *

*----------------------------------------------------------------------*

TABLES: somlreci1.

*----------------------------------------------------------------------*

*                     GLOBAL DATA DECLARATION                          *

*----------------------------------------------------------------------*

DATA: t_mailhex   TYPE STANDARD TABLE OF solix,

      t_contents  TYPE STANDARD TABLE OF solisti1,

      wa_contents TYPE solisti1,

      w_file      TYPE dsvasdocid,

      w_extn(5)   TYPE c,

      w_mail_subj TYPE string,

      w_document  TYPE REF TO cl_document_bcs.

*----------------------------------------------------------------------*

*                     CONSTANTS DECLARATION                            *

*----------------------------------------------------------------------*

CONSTANTS:

*-- Constants used in the body of the Email (HTML)

c_htm         TYPE char3   VALUE 'HTM',

c_style_start TYPE char255 VALUE '<FONT face=Arial size=2>',

c_new_line    TYPE char255 VALUE '<br>',

c_link_start  TYPE char128 VALUE '<A href="www.w3schools.com">',

c_link_text   TYPE char32  VALUE 'Link for learning HTML',

c_link_end    TYPE char4   VALUE '</A>',

c_space(6)    TYPE c       VALUE ' ',

*-- Used as an Example for displaying space between texts in Email body

c_emp1(6)     TYPE c       VALUE 101001,

c_emp2(6)     TYPE c       VALUE 101002,

c_emp3(6)     TYPE c       VALUE 101003.

*----------------------------------------------------------------------*

*                     SELECTION SCREEN                                 *

*----------------------------------------------------------------------*

*-- Input Details - Block

SELECTION-SCREEN BEGIN OF BLOCK file WITH FRAME TITLE text-t01.

PARAMETERS: p_attach   TYPE  rlgrap-filename.

SELECTION-SCREEN END OF BLOCK file.

*-- Email ID of the Recipient

SELECTION-SCREEN BEGIN OF BLOCK mail WITH FRAME TITLE text-t02.

SELECT-OPTIONS: s_mailid  FOR somlreci1-receiver NO INTERVALS.

SELECTION-SCREEN END OF BLOCK mail.

*----------------------------------------------------------------------*

*                          AT SELECTION SCREEN                         *

*----------------------------------------------------------------------*

*-- Providing F4 Help for the input file

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_attach.

  PERFORM file_path USING 'P_ATTACH'.

*&---------------------------------------------------------------------*

*                       START-OF-SELECTION                             *

*&---------------------------------------------------------------------*

START-OF-SELECTION.

  IF p_attach IS INITIAL.

    MESSAGE i999(zz) WITH 'Please Enter at-least one file name'(001).

    EXIT.

  ENDIF.

*-- Upload data from Presentation Server to SAP

  PERFORM upload_data.

*----------------------------------------------------------------------*

*                        END OF SELECTION                              *

*----------------------------------------------------------------------*

END-OF-SELECTION.

*-- Frame the Body of the Email

  PERFORM frame_mail_body.

*-- Send Mail

  PERFORM send_mail.

*&--------------------------------------------------------------------*

*& Form Name      : File_path

*&--------------------------------------------------------------------*

*& Description    : F4 help for File Name / Path

*&--------------------------------------------------------------------*

*& Parameters     : FP_FILE  --> File Name

*&--------------------------------------------------------------------*

FORM file_path USING fp_file TYPE any.

*-- Selects the directory list

  CALL FUNCTION 'F4_DXFILENAME_4_DYNP'

    EXPORTING

      dynpfield_filename = fp_file

      dyname             = sy-cprog

      dynumb             = sy-dynnr

      filetype           = 'P'

      location           = 'P'

      server             = space.

ENDFORM.                    " file_path

*&---------------------------------------------------------------------*

*& Form Name     : Upload_Data                                         *

*&---------------------------------------------------------------------*

*& Description   : Upload data into SAP from Presentation Server       *

*&---------------------------------------------------------------------*

*& Parameters    : None                                                *

*&---------------------------------------------------------------------*

FORM upload_data.

*-- Local data declaration

  DATA: l_file  TYPE string,

        l_index TYPE sy-tabix,

*-- For holding the split file name

        tl_splitfile TYPE STANDARD TABLE OF rlgrap-filename,

        wl_splitfile TYPE rlgrap-filename.

  l_file = p_attach.

**-- Function module to split the Filename and Extension from the Path

  CALL FUNCTION 'CH_SPLIT_FILENAME'

    EXPORTING

      complete_filename = l_file

    IMPORTING

      extension         = w_extn

      name              = w_file.

*-- Split the filename at '.'

  SPLIT l_file AT '.' INTO TABLE tl_splitfile.

  DESCRIBE TABLE tl_splitfile LINES l_index.

*-- In case the filename contains more than one dot

  IF l_index GT 2.

    CLEAR: wl_splitfile, w_extn.

*-- Get the Extension of the file

    READ TABLE tl_splitfile INTO wl_splitfile INDEX l_index.

    w_extn = wl_splitfile.

    DELETE tl_splitfile INDEX l_index.

    DELETE tl_splitfile INDEX 1.

    CLEAR wl_splitfile.

*-- Get the Actual filename

    LOOP AT tl_splitfile INTO wl_splitfile.

      CONCATENATE '.' wl_splitfile INTO wl_splitfile.

    ENDLOOP.

    CONCATENATE w_file wl_splitfile INTO w_file.

  ENDIF.

  CONDENSE w_extn.

*-- Upload File

  CALL FUNCTION 'GUI_UPLOAD'

    EXPORTING

      filename                = l_file

      filetype                = 'BIN'

    TABLES

      data_tab                = t_mailhex

    EXCEPTIONS

      file_open_error         = 1

      file_read_error         = 2

      no_batch                = 3

      gui_refuse_filetransfer = 4

      invalid_type            = 5

      no_authority            = 6

      unknown_error           = 7

      bad_data_format         = 8

      header_not_allowed      = 9

      separator_not_allowed   = 10

      header_too_long         = 11

      unknown_dp_error        = 12

      access_denied           = 13

      dp_out_of_memory        = 14

      disk_full               = 15

      dp_timeout              = 16

      OTHERS                  = 17.

  IF sy-subrc IS NOT INITIAL.

    MESSAGE i999(zz) WITH 'Error in reading file for upload'(002)

    w_file.

  ENDIF.

ENDFORM.                    " upload_data

*&---------------------------------------------------------------------*

*& Form Name     : frame_mail_body                                     *

*&---------------------------------------------------------------------*

*& Description   : To frame the body part of the Email to be sent      *

*&---------------------------------------------------------------------*

*& Parameters    : None                                                *

*&---------------------------------------------------------------------*

FORM frame_mail_body.

*-- Local data declaration to hold the textpool

  DATA: tl_textpool TYPE STANDARD TABLE OF textpool,

        wl_textpool TYPE textpool.

*-- Read the Entire Textpool into an Internal table

  READ TEXTPOOL sy-repid INTO tl_textpool LANGUAGE sy-langu.

  IF sy-subrc IS INITIAL.

    SORT tl_textpool BY id key.

  ENDIF.

*-- Font start

  CLEAR wa_contents.

  wa_contents-line = c_style_start.

  APPEND wa_contents TO t_contents.

*-- New line

  CLEAR wa_contents.

  wa_contents-line = c_new_line.

  APPEND wa_contents TO t_contents.

*-- Program name : Email Attachment

  CLEAR: wl_textpool, wa_contents.

  READ TABLE tl_textpool INTO wl_textpool

                         WITH KEY id = 'I' key = 'T03'

                         BINARY SEARCH.

  IF sy-subrc EQ 0.

    wa_contents-line = wl_textpool-entry.

*-- "#" Present in the Text Element will be replaced by the below value

    REPLACE: '#' WITH 'Email Attachment' INTO wa_contents-line.

    APPEND wa_contents TO t_contents.

  ENDIF.

*-- New line

  CLEAR wa_contents.

  wa_contents-line = c_new_line.

  APPEND wa_contents TO t_contents.

*-- New line

  CLEAR wa_contents.

  wa_contents-line = c_new_line.

  APPEND wa_contents TO t_contents.

*-- You can also change or add the text here...

  CLEAR: wl_textpool, wa_contents.

  READ TABLE tl_textpool INTO wl_textpool

                         WITH KEY id = 'I' key = 'T04'

                         BINARY SEARCH.

  IF sy-subrc EQ 0.

    wa_contents-line = wl_textpool-entry.

    APPEND wa_contents TO t_contents.

  ENDIF.

*-- New line

  CLEAR wa_contents.

  wa_contents-line = c_new_line.

  APPEND wa_contents TO t_contents.

*-- New line

  CLEAR wa_contents.

  wa_contents-line = c_new_line.

  APPEND wa_contents TO t_contents.

*-- For giving spaces between texts, you can use...

  CLEAR: wl_textpool, wa_contents.

  READ TABLE tl_textpool INTO wl_textpool

                         WITH KEY id = 'I' key = 'T05'

                         BINARY SEARCH.

  IF sy-subrc EQ 0.

    wa_contents-line = wl_textpool-entry.

    APPEND wa_contents TO t_contents.

  ENDIF.

*-- New line

  CLEAR wa_contents.

  wa_contents-line = c_new_line.

  APPEND wa_contents TO t_contents.

*-- For Ex; Employee Numbers :

  CLEAR: wl_textpool, wa_contents.

  READ TABLE tl_textpool INTO wl_textpool

                         WITH KEY id = 'I' key = 'T06'

                         BINARY SEARCH.

  IF sy-subrc EQ 0.

    wa_contents-line = wl_textpool-entry.

*-- How to give Spaces in between texts

    CONCATENATE wa_contents-line c_space c_emp1 c_space c_emp2

                                 c_space c_emp3 INTO wa_contents-line.

    APPEND wa_contents TO t_contents.

  ENDIF.

*-- New line

  CLEAR wa_contents.

  wa_contents-line = c_new_line.

  APPEND wa_contents TO t_contents.

*-- New line

  CLEAR wa_contents.

  wa_contents-line = c_new_line.

  APPEND wa_contents TO t_contents.

*-- For more Information on HTML..

  CLEAR: wl_textpool, wa_contents.

  READ TABLE tl_textpool INTO wl_textpool

                         WITH KEY id = 'I' key = 'T07'

                         BINARY SEARCH.

  IF sy-subrc EQ 0.

    wa_contents-line = wl_textpool-entry.

    APPEND wa_contents TO t_contents.

  ENDIF.

*-- New line

  CLEAR wa_contents.

  wa_contents-line = c_new_line.

  APPEND wa_contents TO t_contents.

*-- New line

  CLEAR wa_contents.

  wa_contents-line = c_new_line.

  APPEND wa_contents TO t_contents.

*-- Hyperlink

  CLEAR wa_contents.

  CONCATENATE c_link_start c_link_text c_link_end INTO wa_contents-line.

  APPEND wa_contents TO t_contents.

*-- Subject of the Mail

  CONCATENATE text-t08 w_mail_subj INTO w_mail_subj.

ENDFORM.                    " frame_mail_body

*&---------------------------------------------------------------------*

*& Form Name     : Send_mail                                           *

*&---------------------------------------------------------------------*

*& Description   : To set the recipients and send the mail             *

*&---------------------------------------------------------------------*

*& Parameters    : None                                                *

*&---------------------------------------------------------------------*

FORM send_mail.

*-- Local data declaration for sending mail

  DATA: l_send_request  TYPE REF TO cl_bcs,

        l_document      TYPE REF TO cl_document_bcs,

        l_sender        TYPE REF TO cl_sapuser_bcs,

        l_sub           TYPE char50,

        l_recipient     TYPE REF TO if_recipient_bcs,

        tl_contents     TYPE STANDARD TABLE OF soli,

        l_doc_len       TYPE so_obj_len,

        l_cnt           TYPE sy-tabix,

        l_rcv_email     TYPE adr6-smtp_addr,

        l_result        TYPE sy-binpt,

        l_bcs_exception TYPE REF TO cx_bcs,

        l_subj          TYPE string,

        wl_mailid       LIKE LINE OF s_mailid.

  TRY.

*-- Create persistent send request

      l_send_request = cl_bcs=>create_persistent( ).

      tl_contents[] = t_contents[].

*-- Get the length of the Document

      DESCRIBE TABLE tl_contents LINES l_cnt.

      READ TABLE tl_contents INTO wa_contents INDEX l_cnt.

      l_doc_len = ( l_cnt - 1 ) * 255 + STRLEN( wa_contents ).

*-- Subject of the mail

      l_sub = w_mail_subj.

*-- Create Document

      l_document = cl_document_bcs=>create_document(

                   i_type       = c_htm

                   i_text       = tl_contents

                   i_length     = l_doc_len

                   i_subject    = l_sub

                   i_language   = sy-langu

                   i_importance = '1' ).

*-- Subject of the mail

      MOVE w_mail_subj TO l_subj.

      w_document = l_document.

      TRY.

*-- Set the Message Subject

          CALL METHOD l_send_request->set_message_subject

            EXPORTING

              ip_subject = l_subj.

        CATCH cx_sy_dyn_call_illegal_method.

      ENDTRY.

*-- Add document to send request

      CALL METHOD l_send_request->set_document( l_document ).

*-- Do send delivery info for successful mails

      CALL METHOD l_send_request->set_status_attributes

        EXPORTING

          i_requested_status = 'E'

          i_status_mail      = 'A'.

*-- Set sender

      l_sender = cl_sapuser_bcs=>create( sy-uname ).

      CALL METHOD l_send_request->set_sender

        EXPORTING

          i_sender = l_sender.

*-- To frame the attachments for the mail

      PERFORM frame_attachments.

*-- Add the recipients to the Send mail

      LOOP AT s_mailid INTO wl_mailid.

        l_rcv_email = wl_mailid-low.

        CHECK NOT l_rcv_email IS INITIAL.

        l_recipient = cl_cam_address_bcs=>create_internet_address(

                                                      l_rcv_email ).

        CALL METHOD l_send_request->add_recipient

          EXPORTING

            i_recipient = l_recipient

            i_express   = 'X'.

      ENDLOOP.

*-- Send Email

      CALL METHOD l_send_request->send(

          EXPORTING

            i_with_error_screen = 'X'

          RECEIVING

            result              = l_result ).

      IF l_result = 'X'.

        MESSAGE s999(zz) WITH

        'Approval Mail Sent Successfully'(003).

      ENDIF.

    CATCH cx_bcs INTO l_bcs_exception.

      IF l_result NE 'X'.

        MESSAGE s999(zz) WITH

        'Approval Mail Not Successful'(004).

      ENDIF.

  ENDTRY.

  COMMIT WORK.                                             "Commit Work

ENDFORM.                    " send_mail

*&---------------------------------------------------------------------*

*& Form Name     : frame_attachments                                   *

*&---------------------------------------------------------------------*

*& Description   : To frame the attachments for the mail to be sent    *

*&---------------------------------------------------------------------*

*& Parameters    : None                                                *

*&---------------------------------------------------------------------*

FORM frame_attachments.

*-- Local Data declaration

  DATA: l_subject   TYPE so_obj_des,

        l_att_type  TYPE soodk-objtp.

*-- Subject of the Attachment

  l_subject  = w_file.

*-- Format of the Attachment

  l_att_type = w_extn.

  IF t_mailhex[] IS NOT INITIAL.

    TRY.

*-- Add Attachment to the Document

        CALL METHOD w_document->add_attachment

          EXPORTING

            i_attachment_type    = l_att_type

            i_attachment_subject = l_subject

            i_att_content_hex    = t_mailhex.

      CATCH cx_document_bcs.

    ENDTRY.

  ENDIF.

ENDFORM.                    " frame_attachments

Read only

Former Member
0 Likes
1,532

Hello 

check this one.  http://scn.sap.com/thread/3301780

Regards

Sudesh

Read only

Former Member
0 Likes
1,532

Hi,

Store all the values from an internal table into a String.

  cr_lf TYPE abap_cr_lf VALUE
           cl_abap_char_utilities=>cr_lf.

    LOOP AT tab[] ASSIGNING <dyn_wa>.
      DO.
        ASSIGN COMPONENT  sy-index
           OF STRUCTURE <dyn_wa> TO <dyn_field>.
        IF sy-subrc <> 0.
          EXIT.
        ENDIF.

        IF <dyn_field> IS ASSIGNED.
          CLEAR tmp_str
          tmp_str =  <dyn_field>.
        ENDIF.

        CONCATENATE str1 temp_str tab(Horizontal Tab)
               INTO str1.
      ENDDO.

      CONCATENATE str1 cr_lf INTO str1.
    ENDLOOP.


Convert String to Binary data using below method.


  TRY.
      cl_bcs_convert=>string_to_solix(
        EXPORTING
          iv_string   =
          iv_codepage = 4103
          iv_add_bom  =
        IMPORTING
          et_solix  =
          ev_size   =
    CATCH cx_bcs.
      MESSAGE ).
  ENDTRY.

create persistent data

        lo_send_request = cl_bcs=>create_persistent( ).

        lo_document = cl_document_bcs=>create_document(
        i_type    = 'RAW'
        i_text    =  EMAIL BODy
        i_subject = Email Subject ).

        lo_document->add_attachment(
                i_attachment_type     = Attachment type
                i_attachment_subject  = Attachment Subject
                i_att_content_hex     = Binary content ).


        lo_send_request->set_document( lo_document).

        lo_send_request->set_send_immediately( abap_true ).

Read only

former_member295881
Contributor
0 Likes
1,532

Thank you experts for  your input. I've been able to almost resolve my issue by using the following sample code but the only issue left is that when click on excel attachment then I see error that "This file cannot be previewed because it is corrupted" and here is the screen shot.

Can please somebody take a look at the following code and tell me how to rectify this issue.

-------------------------------------------------------------------------------------------------------------------------------------------

CLASS: cl_abap_char_utilities DEFINITION LOAD.

DATA: docdata LIKE sodocchgi1,
       objpack LIKE sopcklsti1 OCCURS 1 WITH HEADER LINE,
       objtxt LIKE solisti1 OCCURS 10 WITH HEADER LINE,
       objbin1 LIKE solisti1 OCCURS 10 WITH HEADER LINE,
       objbin2 LIKE solisti1 OCCURS 10 WITH HEADER LINE,
       objbin_final LIKE solisti1 OCCURS 10 WITH HEADER LINE,
       reclist LIKE somlreci1 OCCURS 1 WITH HEADER LINE,
       tab_lines TYPE sy-tabix.

DATA: gd_sender_type LIKE soextreci1-adr_typ.

DATA: c_tab TYPE c VALUE cl_abap_char_utilities=>horizontal_tab,
       c_ret TYPE c VALUE cl_abap_char_utilities=>cr_lf.

DATA: c_dev TYPE sy-sysid.

DATA: BEGIN OF i_data OCCURS 0,
       a(20),
       b(20),
       END OF i_data.

DATA: BEGIN OF st,
       f1(2) TYPE c,
       f2(2) TYPE n,
       END OF st.

DATA: itab1 LIKE TABLE OF st WITH HEADER LINE,
       itab2 LIKE TABLE OF st WITH HEADER LINE.


DATA: n TYPE i.

PARAMETER: p_email1 LIKE somlreci1-receiver
                     DEFAULT 'email address',
            p_sender LIKE somlreci1-receiver.

START-OF-SELECTION.

   itab1-f1 = 'AA'. itab1-f2 = '01'. APPEND itab1.
   itab1-f1 = 'BB'. itab1-f2 = '02'. APPEND itab1.
   itab1-f1 = 'CC'. itab1-f2 = '03'. APPEND itab1.

   itab2-f1 = 'ZZ'. itab2-f2 = '26'. APPEND itab2.
   itab2-f1 = 'YY'. itab2-f2 = '25'. APPEND itab2.

   LOOP AT itab1.
     CONCATENATE itab1-f1 itab1-f2 INTO objbin1 separated BY c_tab.
     CONCATENATE c_ret objbin1 INTO objbin1.
     APPEND objbin1.
   ENDLOOP.

   LOOP AT itab2.
     CONCATENATE itab2-f1 itab2-f2 INTO objbin2 separated BY c_tab.
     CONCATENATE c_ret objbin2 INTO objbin2.
     APPEND objbin2.
   ENDLOOP.

   LOOP AT objbin1.
     MOVE objbin1-line TO objbin_final-line.
     APPEND objbin_final.
   ENDLOOP.

   LOOP AT objbin2.
     MOVE objbin2-line TO objbin_final-line.
     APPEND objbin_final.
   ENDLOOP.

   PERFORM process_email.
   c_dev = sy-sysid.

   IF sy-sysid = c_dev.
     wait up to 5 seconds.
     SUBMIT rsconn01 WITH mode = 'INT'
     WITH output = 'X'
     AND RETURN.
   ENDIF.

   IF sy-subrc = 0.
     WRITE: / 'Email succesfilly delivered'.
   ELSE.
     WRITE: / 'failure'.
   ENDIF.

FORM process_email.

   IF p_sender EQ space.
     gd_sender_type = space.
   ELSE.
     gd_sender_type = 'INT'.
   ENDIF.

*Body
   docdata-obj_name = 'Mail_Excel_File'.
   docdata-obj_descr = 'Excel file attachment'.
   objtxt = 'Attached is the sample Excel file'.
   APPEND objtxt.
   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.

*Attachment
   n = 1.

   DESCRIBE TABLE objbin1 LINES tab_lines.
   objpack-doc_size = tab_lines * 255.
   objpack-transf_bin = 'X'.
   objpack-head_start = 1.
   objpack-head_num = 1.
   objpack-body_start = n.
   objpack-body_num = tab_lines.
   objpack-doc_type = 'XLS'.
   docdata-obj_name = 'Excel_File_Attachment1'.
   objpack-obj_descr = 'Excel File Attachment1'.
   APPEND objpack.

   n = n + tab_lines.

   DESCRIBE TABLE objbin2 LINES tab_lines.
   objpack-doc_size = tab_lines * 255.
   objpack-transf_bin = 'X'.
   objpack-head_start = 1.
   objpack-head_num = 1.
   objpack-body_start = n.
   objpack-body_num = tab_lines.
   objpack-doc_type = 'XLS'.
   docdata-obj_name = 'Excel_File_Attachment2'.
   objpack-obj_descr = 'Excel File Attachment2'.
   APPEND objpack.


*Create the list of recipients
   reclist-receiver = p_email1.
   reclist-rec_type = 'U'.
   reclist-express = 'X'.
   APPEND reclist.


*Send the e-mail
   CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
     EXPORTING
       document_data              = docdata
       put_in_outbox              = 'X'
       commit_work                = 'X'
     TABLES
       packing_list               = objpack
       contents_bin               = objbin_final
       contents_txt               = objtxt
       receivers                  = reclist
     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.

   COMMIT WORK.
ENDFORM.                    "process_email

-------------------------------------------------------------------------------------------------------------------------------------------

Many thanks in advance.