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: 

Sending mail from abap code but data not as attachment but as table in mail body

anuj_srivastava
Active Participant
0 Kudos

Hi,

I have a requirement wherein i have to shoot a mail from my abap code but the data should not be sent as attachment but in the body of the mail itself.

If anyone has ever encountered this type of requirement please share here.

Regards,

Anuj

1 ACCEPTED SOLUTION

Clemenss
Active Contributor
0 Kudos

Hi Anuj,

1. Please do not use FM SO_NEW_DOCUMENT_SEND_API .

It's not easy to get any support for this troublesome obsolete function.

2. Go for CL_BCS

3. If you want a table in the body, you have some options. The precondition is to have HTML formatted text in the body. Today's mailers all can interpret this and display well-formatted.

If you are able to create an ABAP list for printer or screen output, you can make use of SAP standard functions. Please adapt this code excerpt to your needs.

* Assume LIST output has been created

FORM output_mail .

  DATA:

    lo_bcs                           TYPE REF TO cl_bcs,

    lo_document                      TYPE REF TO cl_document_bcs,

    lo_recipient                     TYPE REF TO cl_cam_address_bcs,

    lv_subject                       TYPE string,

    lt_soli                          TYPE soli_tab,

    lv_spono                         type sy-spono.

  IF sy-batch IS NOT INITIAL.

* In background, getting complete list must be accomplished via spool  

* Get the List from the Spool

    NEW-PAGE PRINT OFF.

    lv_spono = sy-spono.

    SUBMIT rspolist EXPORTING LIST TO MEMORY AND RETURN

                    WITH rqident = lv_spono

                    WITH pages = 'X'.

* List from Memory

    CALL FUNCTION 'LIST_FROM_MEMORY'

      TABLES

        listobject = lt_soli

      EXCEPTIONS

        OTHERS     = 0.

  ELSE.

* Online: Simply get the on screen list 

* Current list index 1

    CALL FUNCTION 'SAVE_LIST'

      TABLES

        listobject = lt_soli

      EXCEPTIONS

        OTHERS     = 0.

  ENDIF.

*  convert list to html format

  CALL FUNCTION 'WWW_HTML_FROM_LISTOBJECT'

    TABLES

      html        = lt_html

      listobject  = lt_soli.

* create mail object      

  TRY.

      lo_bcs = cl_bcs=>create_persistent( ).

      lo_document = cl_document_bcs=>create_document(

        i_type    = 'HTM'

        i_text    = lt_soli

        i_subject = '' ).

* lv_subject may carry meaningful subject line       

      lo_bcs->set_message_subject( lv_subject ).

* add document to send request

      lo_bcs->set_document( lo_document ).

* create recipient

      lo_recipient = cl_cam_address_bcs=>create_internet_address( <name.name><at><domain> ).

* add recipient to send request

        CALL METHOD lo_bcs->add_recipient

          EXPORTING

            i_recipient = lo_recipient

            i_express   = 'X'.

* No need for any scheduled job - mail is sent immediately           

      lo_bcs->set_send_immediately( abap_true ).

* send document

      IF lo_bcs->send( ) IS INITIAL.

* Output error message

      ELSE.

        COMMIT WORK.

      ENDIF.

    CATCH

      cx_address_bcs

      cx_document_bcs

      cx_send_req_bcs

      cx_bcs

      INTO lo_bcs_exception .

      lv_string = lo_bcs_exception->get_text( ).

* Output error message

  ENDTRY.

ENDFORM.                    " OUTPUT_MAIL

You can send whatever ABAP list you like, even colored ALV lists look really good in the mail body.

Regards,

Clemens


3 REPLIES 3

former_member213851
Active Contributor
0 Kudos

Hi Anuj,

Use FM SO_NEW_DOCUMENT_SEND_API .


Please refer below link:

http://wiki.sdn.sap.com/wiki/display/Snippets/Send+mail+via+SAP+ABAP+Code

Former Member
0 Kudos

Use FM SO_DOCUMENT_SEND_API1. While calling this FM, fill up your multiple entries in table contents_txt using loop. And send the mail.

Clemenss
Active Contributor
0 Kudos

Hi Anuj,

1. Please do not use FM SO_NEW_DOCUMENT_SEND_API .

It's not easy to get any support for this troublesome obsolete function.

2. Go for CL_BCS

3. If you want a table in the body, you have some options. The precondition is to have HTML formatted text in the body. Today's mailers all can interpret this and display well-formatted.

If you are able to create an ABAP list for printer or screen output, you can make use of SAP standard functions. Please adapt this code excerpt to your needs.

* Assume LIST output has been created

FORM output_mail .

  DATA:

    lo_bcs                           TYPE REF TO cl_bcs,

    lo_document                      TYPE REF TO cl_document_bcs,

    lo_recipient                     TYPE REF TO cl_cam_address_bcs,

    lv_subject                       TYPE string,

    lt_soli                          TYPE soli_tab,

    lv_spono                         type sy-spono.

  IF sy-batch IS NOT INITIAL.

* In background, getting complete list must be accomplished via spool  

* Get the List from the Spool

    NEW-PAGE PRINT OFF.

    lv_spono = sy-spono.

    SUBMIT rspolist EXPORTING LIST TO MEMORY AND RETURN

                    WITH rqident = lv_spono

                    WITH pages = 'X'.

* List from Memory

    CALL FUNCTION 'LIST_FROM_MEMORY'

      TABLES

        listobject = lt_soli

      EXCEPTIONS

        OTHERS     = 0.

  ELSE.

* Online: Simply get the on screen list 

* Current list index 1

    CALL FUNCTION 'SAVE_LIST'

      TABLES

        listobject = lt_soli

      EXCEPTIONS

        OTHERS     = 0.

  ENDIF.

*  convert list to html format

  CALL FUNCTION 'WWW_HTML_FROM_LISTOBJECT'

    TABLES

      html        = lt_html

      listobject  = lt_soli.

* create mail object      

  TRY.

      lo_bcs = cl_bcs=>create_persistent( ).

      lo_document = cl_document_bcs=>create_document(

        i_type    = 'HTM'

        i_text    = lt_soli

        i_subject = '' ).

* lv_subject may carry meaningful subject line       

      lo_bcs->set_message_subject( lv_subject ).

* add document to send request

      lo_bcs->set_document( lo_document ).

* create recipient

      lo_recipient = cl_cam_address_bcs=>create_internet_address( <name.name><at><domain> ).

* add recipient to send request

        CALL METHOD lo_bcs->add_recipient

          EXPORTING

            i_recipient = lo_recipient

            i_express   = 'X'.

* No need for any scheduled job - mail is sent immediately           

      lo_bcs->set_send_immediately( abap_true ).

* send document

      IF lo_bcs->send( ) IS INITIAL.

* Output error message

      ELSE.

        COMMIT WORK.

      ENDIF.

    CATCH

      cx_address_bcs

      cx_document_bcs

      cx_send_req_bcs

      cx_bcs

      INTO lo_bcs_exception .

      lv_string = lo_bcs_exception->get_text( ).

* Output error message

  ENDTRY.

ENDFORM.                    " OUTPUT_MAIL

You can send whatever ABAP list you like, even colored ALV lists look really good in the mail body.

Regards,

Clemens