Archived discussions are read-only. Learn more about SAP Q&A

Urgent: Sending an Email with Excel sheet attachment

Hi All,

I am preparing a report and has to send mail with excel sheet attachement. In report i am getting everything fine. But in the mail i am not getting the data, data is passing to the FM succesfully. But in the mail, excel is not having full data.

the below mentioned is my code.

i_texts-line = c_stern.

i_texts-line+254(1) = c_newline.

APPEND i_texts.

LOOP AT i_texc.

i_att_line-line = i_texc.

APPEND i_att_line.

ENDLOOP.

DATA: wa_doc_chng LIKE sodocchgi1.

DATA: w_tab_lines LIKE sy-tabix.

STATICS: c_body_start TYPE i VALUE '1'.

CONSTANTS: lc_filename(10) TYPE c VALUE 'Attachment',

lc_atthead(30) TYPE c VALUE 'Manually released invoices'.

DATA: i_objpack LIKE sopcklsti1 OCCURS 2 WITH HEADER LINE.

DATA: w_doc_name(30) TYPE c.

DATA: l_text1(15),

l_text2(12),

l_text3(15),

l_text4(15),

l_text5(14),

l_text6(17),

l_text7(10),

l_text8(20),

l_text9(19),

l_text10(19),

l_text11(19),

l_text12(16),

l_text13(20),

l_text14(19),

l_text15(19),

l_text16(30).

DATA: DATE(10),

DT(2),

MON(3),

YR(4),

lc_final(50).

CLEAR: i_texts, i_reci,i_att_line. "i_bname,

REFRESH: i_texts, i_reci,i_att_line. "i_bname,

WRITE sy-datum TO w_datex.

WRITE sy-uzeit TO w_timex.

l_text1 = text-003.

l_text2 = text-004.

l_text3 = text-005.

l_text4 = text-006.

l_text5 = text-007.

l_text6 = text-008.

l_text7 = text-009.

l_text8 = text-010.

l_text9 = text-011.

l_text10 = text-012.

l_text11 = text-013.

l_text12 = text-014.

l_text13 = text-015.

l_text14 = text-016.

l_text15 = text-017.

l_text16 = text-026.

CONCATENATE

l_text1

l_text2

l_text3

l_text4

l_text5

l_text6

l_text7

l_text8

l_text9

l_text10

l_text11

l_text12

l_text13

l_text14

l_text15

l_text16

INTO i_texts SEPARATED BY

c_horz_tab.

i_texts-line+254(1) = c_newline.

APPEND i_texts.

CLEAR i_texts.

LOOP AT i_texc.

i_att_line-line = i_texc.

APPEND i_att_line.

ENDLOOP.

LOOP AT so_rec.

i_reci-receiver = so_rec-low.

i_reci-express = 'X'.

i_reci-rec_type = 'U'.

APPEND i_reci.

ENDLOOP.

DESCRIBE TABLE i_texts LINES w_tab_lines.

READ TABLE i_texts INDEX w_tab_lines.

wa_doc_chng-doc_size = ( w_tab_lines - 1 ) * 255 + STRLEN( i_texts ).

wa_obj_name = 'ZFI_XX_REL_BLOCKED_INVOICE_01'.

DT = SY-DATUM+6(2).

MON = SY-DATUM+4(2).

YR = SY-DATUM+0(4).

CONCATENATE DT MON YR INTO DATE SEPARATED BY '-'.

CONCATENATE lc_atthead date into lc_final separated by ' '.

MOVE lc_final

TO wa_obj_descr .

wa_doc_chng-obj_name = wa_obj_name.

wa_doc_chng-obj_descr = wa_obj_descr.

CLEAR i_objpack-transf_bin.

i_objpack-head_start = 1.

i_objpack-head_num = 0.

i_objpack-body_start = 0.

i_objpack-body_num = w_tab_lines.

i_objpack-doc_type = 'RAW'.

APPEND i_objpack.

APPEND LINES OF i_att_line TO i_texts.

LOOP AT i_arseg INTO wa_arseg.

i_xmplt_d-belnr = wa_arseg-belnr.

i_xmplt_d-budat = wa_arseg-budat.

i_xmplt_d-bukrs = wa_arseg-bukrs.

i_xmplt_d-lifnr = wa_arseg-lifnr.

i_xmplt_d-name1_lifnr = wa_arseg-name1_lifnr.

i_xmplt_d-wrbtr = wa_arseg-wrbtr.

i_xmplt_d-waers = wa_arseg-waers.

i_xmplt_d-ebeln = wa_arseg-ebeln.

i_xmplt_d-ebelp = wa_arseg-ebelp.

i_xmplt_d-reason = wa_arseg-reason.

i_xmplt_d-dmeng = wa_arseg-dmeng.

i_xmplt_d-dwert = wa_arseg-dwert.

i_xmplt_d-bname = wa_arseg-bname.

i_xmplt_d-name_text = wa_arseg-name_text.

i_xmplt_d-udate = wa_arseg-udate.

i_xmplt_d-comment = wa_arseg-comment.

APPEND i_xmplt_d.

ENDLOOP.

LOOP AT i_xmplt_d.

CONCATENATE i_xmplt_d-belnr

i_xmplt_d-budat

i_xmplt_d-bukrs

i_xmplt_d-lifnr

i_xmplt_d-name1_lifnr

i_xmplt_d-wrbtr

i_xmplt_d-waers

i_xmplt_d-ebeln

i_xmplt_d-ebelp

i_xmplt_d-reason

i_xmplt_d-dmeng

i_xmplt_d-dwert

i_xmplt_d-bname

i_xmplt_d-name_text

i_xmplt_d-udate

i_xmplt_d-comment

INTO i_texts SEPARATED

BY c_horz_tab.

i_texts-line+254(1) = c_newline.

APPEND i_texts.

CLEAR i_texts.

ENDLOOP.

DESCRIBE TABLE i_texts LINES w_tab_lines.

READ TABLE i_texts INDEX w_tab_lines.

wa_doc_chng-doc_size = ( w_tab_lines - 1 ) * 255 + STRLEN( i_texts ).

DESCRIBE TABLE i_texts LINES w_tab_lines.

i_objpack-transf_bin = ' '.

i_objpack-head_start = 0.

i_objpack-head_num = 0.

i_objpack-body_start = 1.

i_objpack-body_num = w_tab_lines.

i_objpack-doc_type = 'XLS'.

i_objpack-obj_name = 'Attachment'.

  • i_objpack-obj_descr = w_doc_name.

i_objpack-obj_descr = lc_filename.

i_objpack-doc_size = w_tab_lines * 255.

APPEND i_objpack.

CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'

EXPORTING

document_data = wa_doc_chng

TABLES

packing_list = i_objpack

contents_txt = i_texts

receivers = i_reci

EXCEPTIONS

too_many_receivers = 1

document_not_sent = 2

operation_no_authorization = 4

OTHERS = 99.

IF sy-subrc = 0.

COMMIT WORK.

ELSE.

WRITE: / 'Error in send E-Mail' .

ENDIF.

please revert me asap. It is very urgent to me. thanks in advance.

Bye

Tags:
replied

hI

The code below demonstrates how to send an email to an external email address(test@sapdev.co.uk),

where the data is stored within a .xls attachment.

*&---------------------------------------------------------------------*
*& Report  ZEMAIL_ATTACH                                               *
*&                                                                     *
*&---------------------------------------------------------------------*
*& Example of sending external email via SAPCONNECT                    *
*&                                                                     *
*&---------------------------------------------------------------------*
REPORT  ZEMAIL_ATTACH                   .
TABLES: ekko.

PARAMETERS: p_email   TYPE somlreci1-receiver
                                  DEFAULT 'test@sapdev.co.uk'.

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 .xls documnet attachment'
                                      'XLS'
                                      '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.
  CONSTANTS: con_cret TYPE x VALUE '0D',  "OK for non Unicode
             con_tab TYPE x 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.
    CONCATENATE wa_charekpo-ebeln wa_charekpo-ebelp
                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

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question