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

Send Mail with XML document attached

Former Member
0 Likes
3,969

Hi ,

My requirement is like i need to send a mail attaching XML document. i am able to receive attchment but XML file is not opening.

its below mesage displying

"Invalid at the top level of the document. " pls suggest.

Regards,

Rajkumar

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,997

Raj,

Create the FM with SO_XML with out any code in the FM and add the entry in the table TSOTD.

http://help.sap.com/erp2005_ehp_04/helpdata/EN/6c/69c3c5418d11d1896e0000e8322d00/frameset.htm

http://www.sapdb.info/abap-program-to-send-mail-with-attachment/

I tried it.. it is working fine.

6 REPLIES 6
Read only

athavanraja
Active Contributor
0 Likes
1,997

am able to receive attchment but XML file is not opening.

its below mesage displying

"Invalid at the top level of the document. " pls suggest.

This means that the xml is malformed.

regarding sending mail with XML attachment check out the following code sample (in FM format)

import parameters:



SENDER_ID           TYPE AD_SMTPADR   
SUBJECT             TYPE SO_OBJ_DES   



tables parameter

RECEPIENTS	TYPE	BCSY_SMTPA
RETURN	TYPE	TABLE_OF_STRINGS


top include data declaration


DATA: send_request       TYPE REF TO cl_bcs.
DATA: text               TYPE bcsy_text.
data: xtext type standard table of solix .
DATA: document           TYPE REF TO cl_document_bcs.
DATA: sender             TYPE REF TO if_sender_bcs.
DATA: recipient          TYPE REF TO if_recipient_bcs.
DATA: bcs_exception      TYPE REF TO cx_bcs.
DATA: sent_to_all        TYPE os_boolean.
DATA: conlength TYPE i ,
      conlengths TYPE so_obj_len ,
      result_content TYPE string .

DATA: e_r_page TYPE REF TO cl_rsr_www_page.
data: content_length	TYPE	w3param-cont_len ,
      content_type	TYPE	w3param-cont_type,
      return_code	TYPE	w3param-ret_code .

data: html type standard table of w3html .
data: server type string ,
      port type string .
data: wa_rec type ad_smtpadr .
data: bcs_message type string .


FM Source.


FUNCTION y_email_xml_atta.

  DATA: binary_content     TYPE solix_tab.
  DATA: xl_content TYPE xstring .
  DATA: atta_sub TYPE sood-objdes .

  data:   file_data TYPE string ,
     output TYPE string .
  DATA: flights TYPE flighttab .


  IF NOT recepients[] IS INITIAL .
    SELECT * FROM sflight INTO TABLE flights .

    CALL TRANSFORMATION (`ID`)
              SOURCE flights   = flights[]
              RESULT XML output.


    CLEAR: xl_content .


    CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
      EXPORTING
        text           = output

     IMPORTING
       buffer         = xl_content
     EXCEPTIONS
       failed         = 1
       OTHERS         = 2
              .
    IF sy-subrc <> 0.

    ENDIF.

    REFRESH binary_content .

    CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
      EXPORTING
        buffer     = xl_content
      TABLES
        binary_tab = binary_content.


    REFRESH text .
    CLEAR result_content .

    CONCATENATE
    'Please find attached your XML doc.'
    'Regards'
    ' Team<' INTO result_content .

    conlength = STRLEN( result_content ) .
    conlengths = conlength .
    CALL FUNCTION 'SCMS_STRING_TO_FTEXT'
      EXPORTING
        text      = result_content
      TABLES
        ftext_tab = text.



    TRY.
        CLEAR send_request .

        send_request = cl_bcs=>create_persistent( ).

        CLEAR document .
        document = cl_document_bcs=>create_document(
                        i_type    = 'HTM'
                        i_text    = text
                        i_length  = conlengths
                        i_subject = subject ).

        CALL METHOD document->add_attachment
          EXPORTING
            i_attachment_type    = 'XML'
            i_attachment_subject = atta_sub
            i_att_content_hex    = binary_content.


        CALL METHOD send_request->set_document( document ).

        CLEAR sender .
        sender = cl_cam_address_bcs=>create_internet_address( sender_id ).
        CALL METHOD send_request->set_sender
          EXPORTING
            i_sender = sender.

        CLEAR wa_rec .

        LOOP AT recepients INTO wa_rec  .
          CLEAR recipient .

          recipient = cl_cam_address_bcs=>create_internet_address(
                                            wa_rec ).


          CALL METHOD send_request->add_recipient
            EXPORTING
              i_recipient = recipient
              i_express   = 'X'.
        ENDLOOP .
        CALL METHOD send_request->set_status_attributes
          EXPORTING
            i_requested_status = 'E'
            i_status_mail      = 'E'.

        CALL METHOD send_request->set_send_immediately( 'X' ).

        CALL METHOD send_request->send(
          EXPORTING
            i_with_error_screen = 'X'
          RECEIVING
            result              = sent_to_all ).
        IF sent_to_all = 'X'.
          APPEND 'Mail sent successfully ' TO return .
        ENDIF.

        COMMIT WORK.

      CATCH cx_bcs INTO bcs_exception.
        bcs_message = bcs_exception->get_text( ).
        APPEND bcs_message TO return .
        EXIT.

    ENDTRY.

  ELSE .

    APPEND 'Specify email address for sending' TO return .
  ENDIF .

ENDFUNCTION.

Edited by: Durairaj Athavan Raja on Oct 20, 2009 8:54 AM

Read only

0 Likes
1,997

Hi Raja,

i tried your code by creating new function module , i got attchment but XML file not opening stillfacing same error .

i opened XML file with notepad , contents are shown below .do we need to mention any separator ?

please suggest.

-


#<?xml version="1.0" encoding="utf-16"?>

<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0"><asx:values><FLIGHTS><SFLIGHT><MANDT>300</MANDT><CARRID>AC</CARRID><CONNID>0820</CONNID><FLDATE>2002-12-20</FLDATE><PRICE>1222.0</PRICE><CURRENCY>CAD</CURRENCY><PLANETYPE>A330-300</PLANETYPE><SEATSMAX>320</SEATSMAX><SEATSOCC>12</SEATSOCC><PAYMENTSUM>0.0</PAYMENTSUM><SEATSMAX_B>20</SEATSMAX_B><SEATSOCC_B>1</SEATSOCC_B><SEATSMAX_F>0</SEATSMAX_F><SEATSOCC_F>0</SEATSOCC_F></SFLIGHT><SFLIGHT><MANDT>300</MANDT><CARRID>AF</CARRID><CONNID>0820</CONNID><FLDATE>2002-12-23</FLDATE><PRICE>2222.0</PRICE><CURRENCY>EUR</CURRENCY><PLANETYPE>A330-300</PLANETYPE><SEATSMAX>320</SEATSMAX><SEATSOCC>2</SEATSOCC><PAYMENTSUM>0.0</PAYMENTSUM><SEATSMAX_B>20</SEATSMAX_B><SEATSOCC_B>1</SEATSOCC_B><SEATSMAX_F>0</SEATSMAX_F><SEATSOCC_F>0</SEATSOCC_F></SFLIGHT><SFLIGHT><MANDT>300</MANDT><CARRID>LH</CARRID><CONNID>0400</CONNID><FLDATE>1995-02-28</FLDATE><PRICE>899.0</PRICE><CURRENCY>DEM</CURRENCY><PLANETYPE>A319</PLANETYPE><SEATSMAX>350</SEATSMAX><SEATSOCC>3</SEATSOCC><PAYMENTSUM>2639.0</PAYMENTSUM><SEATSMAX_B>0</SEATSMAX_B><SEATSOCC_B>0</SEATSOCC_B><SEATSMAX_F>0</SEATSMAX_F><SEATSOCC_F>0</SEATSOCC_F></SFLIGHT><SFLIGHT><MANDT>300</MANDT><CARRID>LH</CARRID><CONNID>0454</CONNID><FLDATE>1995-11-17</FLDATE><PRICE>1499.0</PRICE><CURRENCY>DEM</CURRENCY><PLANETYPE>A319</PLANETYPE><SEATSMAX>350</SEATSMAX><SEATSOCC>2</SEATSOCC><PAYMENTSUM>2949.0</PAYMENTSUM><SEATSMAX_B>0</SEATSMAX_B><SEATSOCC_B>0</SEATSOCC_B><SEATSMAX_F>0</SEATSMAX_F><SEATSOCC_F>0</SEATSOCC_F></SFLIGHT><SFLIGHT><MANDT>300</MANDT><CARRID>LH</CARRID><CONNID>0455</CONNID><FLDATE>1995-06-06</FLDATE><PRICE>1090.0</PRICE><CURRENCY>USD</CURRENCY><PLANETYPE>A319</PLANETYPE><SEATSMAX>220</SEATSMAX><SEATSOCC>1</SEATSOCC><PAYMENTSUM>1499.0</PAYMENTSUM><SEATSMAX_B>0</SEATSMAX_B><SEATSOCC_B>0</SEATSOCC_B><SEATSMAX_F>0</SEATSMAX_F><SEATSOCC_F>0</SEATSOCC_F></SFLIGHT><SFLIGHT><MANDT>300</MANDT><CARRID>LH</CARRID><CONNID>3577</CONNID><FLDATE>1995-04-28</FLDATE><PRICE>6000.0</PRICE><CURRENCY>LIT</CURRENCY><PLANETYPE>A319</PLANETYPE><SEATSMAX>220</SEATSMAX><SEATSOCC>1</SEATSOCC><PAYMENTSUM>600.0</PAYMENTSUM><SEATSMAX_B>0</SEATSMAX_B><SEATSOCC_B>0</SEATSOCC_B><SEATSMAX_F>0</SEATSMAX_F><SEATSOCC_F>0</SEATSOCC_F></SFLIGHT><SFLIGHT><MANDT>300</MANDT><CARRID>LH</CARRID><CONNID>9981</CONNID><FLDATE>2002-12-21</FLDATE><PRICE>222.0</PRICE><CURRENCY>EUR</CURRENCY><PLANETYPE>A330-300</PLANETYPE><SEATSMAX>320</SEATSMAX><SEATSOCC>12</SEATSOCC><PAYMENTSUM>0.0</PAYMENTSUM><SEATSMAX_B>20</SEATSMAX_B><SEATSOCC_B>1</SEATSOCC_B><SEATSMAX_F>0</SEATSMAX_F><SEATSOCC_F>0</SEATSOCC_F></SFLIGHT><SFLIGHT><MANDT>300</MANDT><CARRID>SQ</CARRID><CONNID>0026</CONNID><FLDATE>1995-02-28</FLDATE><PRICE>849.0</PRICE><CURRENCY>DEM</CURRENCY><PLANETYPE>DC-10-10</PLANETYPE><SEATSMAX>380</SEATSMAX><SEATSOCC>2</SEATSOCC><PAYMENTSUM>1684.0</PAYMENTSUM><SEATSMAX_B>0</SEATSMAX_B><SEATSOCC_B>0</SEATSOCC_B><SEATSMAX_F>0</SEATSMAX_F><SEATSOCC_F>0</SEATSOCC_F></SFLIGHT></FLIGHTS></asx:values></asx:abap>

Read only

0 Likes
1,997

Hi Raja,

Earlier problem was Invalid at the top level of the document

it is due to XML header #<?xml version="1.0" encoding="utf-16"?>

now i have added OPTIONS addition to the CALL TRANSFORMATION statement to avoid header

Now The syntax is

CALL TRANSFORMATION (`ID`) OPTIONS XML_HEADER = 'no'

SOURCE flights = flights[]

RESULT XML output.

but now the header coming like #<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">

because of # key it is not able to open the file.still message Invalid at the top level of the document is coming

after CALL TRANSFORMATION statement in output parameter There is no # at the begining .

Please suggest how to delete # .

Regards,

Rajkumar

Edited by: Raj Kumar on Oct 21, 2009 12:08 PM

Read only

0 Likes
1,997

the first # is what causing problem, it may be a Carriage return, or a line feed analyze it using CL_ABAP_CHAR_UTILITIES=>CR_LF or newline and remove the same.

Read only

Former Member
0 Likes
1,998

Raj,

Create the FM with SO_XML with out any code in the FM and add the entry in the table TSOTD.

http://help.sap.com/erp2005_ehp_04/helpdata/EN/6c/69c3c5418d11d1896e0000e8322d00/frameset.htm

http://www.sapdb.info/abap-program-to-send-mail-with-attachment/

I tried it.. it is working fine.

Read only

0 Likes
1,997

Thanks K B S , now i am able open the attchment , thanks for very useful links .