Monthly Archives: November 2007

Send mail from ABAP to an email address or a shared/private distribution list

*&———————————————————————*
*& Report  ZSENDEMAIL
*&
*&———————————————————————*
*& Example of sending external email via SAPCONNECT
*& Prueba de envio de mail
*&———————————————————————*
REPORT zsendemail.

perform SEND_MAIL.
perform initiate_mail_execute_program.

*———————————————————————–
*———————————————————————–
*———————————————————————–

*&———————————————————————*
*&      Form SEND_MAIL
*&———————————————————————*
*&
*&———————————————————————*
FORM SEND_MAIL.

DATA: send_request       TYPE REF TO cl_bcs.
DATA: text               TYPE bcsy_text.
DATA: document           TYPE REF TO cl_document_bcs.
DATA: sender             TYPE REF TO cl_sapuser_bcs.
DATA: recipient          TYPE REF TO if_recipient_bcs.
DATA: bcs_exception      type ref to cx_bcs.
DATA: sent_to_all        type os_boolean.

try.
   " ——– create persistent send request ————————
   send_request = cl_bcs=>create_persistent( ).

   "——– create and set document ——————————-
   " create document from internal table with text
   APPEND ‘Hello world!’ TO text.
   document = cl_document_bcs=>create_document(
                 i_type    = ‘RAW’
                 i_text    = text
                 i_length  = ’12’
                 i_subject = ‘test created by BCS_EXAMPLE_4’ ).

   " add document to send request
   CALL METHOD send_request->set_document( document ).

   "——— set sender ——————————————-
   " note: this is necessary only if you want to set the sender
   "       different from actual user (SY-UNAME). Otherwise sender is
   "       set automatically with actual user.

   sender = cl_sapuser_bcs=>create( sy-uname ).
   CALL METHOD send_request->set_sender
      EXPORTING i_sender = sender.

   " enviar a una direccion email
   " create recipient – please replace e-mail address !!!
   recipient = cl_cam_address_bcs=>create_internet_address(
                  ‘poncio_piloto@gmail.com’ ).


   " add recipient with its respective attributes to send request
   CALL METHOD send_request->add_recipient
       EXPORTING
          i_recipient  = recipient
          i_express    = ‘X’.

   " enviar a un shared distribution list
   " https://www.sdn.sap.com/irj/sdn/thread?messageID=4220548
   recipient = cl_distributionlist_bcs=>getu_persistent(
                  i_dliname = ‘KRONOS’
                  i_private = ‘ ‘).
   " add recipient with its respective attributes to send request
   CALL METHOD send_request->add_recipient
       EXPORTING
          i_recipient  = recipient
          i_express    = ‘X’.

   " esto permite que se a~adan o cambien las direcciones de mail y se envie en correo
   "———- send document ———————————————————
   "CALL METHOD send_request->edit(
   "   i_hide_note     = ‘X’
   "   i_starting_at_x = 12
   "   i_starting_at_y = 5
   "   i_ending_at_x   = 97
   "   i_ending_at_y   = 22 ).

   " y esto envia directamente
   call method send_request->send.

   COMMIT WORK.

   "———————————————————–
   "                     exception handling
   "———————————————————–
   " replace this very rudimentary exception handling
   "    with your own one !!!
   "———————————————————–
   catch cx_bcs into bcs_exception.
      write: ‘Fehler aufgetreten.'(001).
      write: ‘Fehlertyp:'(002), bcs_exception->error_type.
      exit.

   endtry.

endform.


form initiate_mail_execute_program.
*&———————————————————————*
*&      Form  INITIATE_MAIL_EXECUTE_PROGRAM
*&———————————————————————*
*       Instructs mail send program for SAPCONNECT to send email.
*———————————————————————-*
* http://www.sapdevelopment.co.uk/reporting/email/email_mbody.htm
   data: gd_error type sy-subrc.
   wait up to 3 seconds.
   if gd_error eq 0.
      submit rsconn01 with mode = ‘INT’ with output = ‘X’ and return.
   endif.
endform.
* INITIATE_MAIL_EXECUTE_PROGRAM

How to execute an OS command from ABAP?

To execute an operating system command from ABAP, Mr. Arturo Salcido sent this code to me:

Para ejecutar un comando de sistema operativo desde ABAP, el Sr. Arturo Salcido me envio este codigo:


*&———————————————————————*
*& Report  ZWINCMD                                                     *
*&                                                                     *
*&———————————————————————*
*&                                                                     *
*&                                                                     *
*&———————————————————————*

REPORT ZWINCMD NO STANDARD PAGE HEADING.
include <icon>.

* Ejecuta un comando en el servidor
* data declaration
DATA: BEGIN OF OUTP OCCURS 0,
  LINE(255)     TYPE C,
END OF OUTP.
*Selection screen
PARAMETERS: comando(132) TYPE C LOWER CASE.

START-OF-SELECTION.
  PERFORM EXEC_OS.

*&———————————————————————*
*&      Form  EXEC_OS
*&———————————————————————*
*       text
*———————————————————————-*
*  –>  p1        text
*  <–  p2        text
*———————————————————————-*
FORM EXEC_OS.
  REFRESH OUTP.

* System call which executes a Unix command *
  CALL ‘SYSTEM’ ID ‘COMMAND’ FIELD COMANDO
              ID ‘TAB’     FIELD OUTP-*SYS*.

* Display *
  FORMAT COLOR COL_POSITIVE HOTSPOT.
  WRITE:  ICON_EXECUTE_OBJECT AS ICON,
          ‘Execute’ .
  FORMAT RESET.
  WRITE:
          COMANDO INPUT.
  WRITE: / SY-SUBRC.
  LOOP AT OUTP.
    WRITE: /1 OUTP-LINE.
  ENDLOOP.
  REFRESH OUTP.
ENDFORM.                               " EXEC_UNIX

AT LINE-SELECTION.
  DATA: FLDNAME(20), LINENO TYPE I.
  GET CURSOR FIELD FLDNAME LINE LINENO.
  IF LINENO = 1.                       " if on first line
    READ LINE 2 FIELD VALUE COMANDO INTO COMANDO.
    SY-LSIND = 1.
    PERFORM EXEC_OS.
  ENDIF.

AT PF8.
  READ LINE 2 FIELD VALUE COMANDO INTO COMANDO.
  SY-LSIND = 0.
  PERFORM EXEC_OS.