User Tools

Site Tools

You are not allowed to perform this action

programming:abap:massive_send_mail

Massive Send Mail

This report is useful to notify to all or a group of users for something important: I use the report mostly to remind programmed downtime.
The mail body is uploaded from PC - I prefer to edit the message in HTML format with a PC HTML editor so I can preview the result.

The report prepares the mail then uses the General purpose Send Mail function to send the mail.

You can also use the report as a bulk emailer without any relation to SAP users if you upload the email addresses via the text file.

Below the parameters:
Parameters for Massive Send Mail

And here the source code:

*&---------------------------------------------------------------------*
*&  Report  Z_MASSIVE_SEND_MAIL
*&---------------------------------------------------------------------*
*&  Send Mail to potentially all users. Be careful !
*&---------------------------------------------------------------------*
*&  IperCube 07.2013
*&---------------------------------------------------------------------*
 
REPORT  z_massive_send_mail.
 
TABLES: usr02.
 
DATA: tab_emllst    TYPE STANDARD TABLE OF txt255.
DATA: tab_body      TYPE STANDARD TABLE OF txt255.
DATA: tab_txt_ref   TYPE STANDARD TABLE OF txt255.
 
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE title_b1.
SELECTION-SCREEN SKIP.
PARAMETERS: p_test AS CHECKBOX DEFAULT 'X'.
 
SELECTION-SCREEN SKIP.
PARAMETERS: p_rdsap     RADIOBUTTON GROUP rad1 DEFAULT 'X'.
SELECT-OPTIONS p_user FOR usr02-bname.
PARAMETERS: p_date TYPE sy-datum.
 
SELECTION-SCREEN SKIP.
PARAMETERS: p_rdfile     RADIOBUTTON GROUP rad1.
PARAMETERS: p_emllst(100) LOWER CASE.
SELECTION-SCREEN COMMENT /33(79) comm1 FOR FIELD p_emllst.
SELECTION-SCREEN END OF BLOCK b1.
 
SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE title_b2.
PARAMETERS: p_sendr TYPE usr02-bname.
PARAMETERS: p_subj(50) LOWER CASE.
PARAMETERS: p_bodyf(100) LOWER CASE.
SELECTION-SCREEN COMMENT /33(79) comm2 FOR FIELD p_bodyf.
SELECTION-SCREEN END OF BLOCK b2.
 
* Init Constants
INITIALIZATION.
  title_b1 = 'Source List for Massive Send Mail'.
  title_b2 = 'Additional Parameters'.
  comm1 = 'One email address per record'.
  comm2 = 'Mail body can be HTML or plain text'.
 
***********************************************************************
AT SELECTION-SCREEN.
***********************************************************************
 
* Check Mail body text file if not Test Mode
  IF p_test IS INITIAL.
    IF p_bodyf IS INITIAL.
      MESSAGE w888(sabapdocu) WITH 'Mail body File Mandatory !'.
    ENDIF.
  ENDIF.
 
* Check email list text file
  IF p_rdfile IS NOT INITIAL.
    IF p_emllst IS INITIAL.
      MESSAGE w888(sabapdocu) WITH 'Email list File Mandatory !'.
    ENDIF.
  ENDIF.
 
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_emllst .
  PERFORM help_file CHANGING p_emllst.
 
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_bodyf .
  PERFORM help_file CHANGING p_bodyf.
 
***********************************************************************
START-OF-SELECTION.
***********************************************************************
 
  "Load Body File if not Test Mode
  IF p_test IS INITIAL.
    PERFORM load_file TABLES tab_body USING p_bodyf.
  ENDIF.
 
  "Load Email list
  IF p_rdfile IS NOT INITIAL.
    PERFORM load_file TABLES tab_emllst USING p_emllst.
  ENDIF.
 
  "Send Mail
  PERFORM massive_send_mail USING p_rdsap.
 
*&---------------------------------------------------------------------*
*&      Form  massive_send_mail
*&---------------------------------------------------------------------*
FORM massive_send_mail USING p_user_from_sap.
 
  DATA: char TYPE soli,
        wa_documents_body  TYPE zes_keg_email_documents  WITH HEADER LINE,
        wa_recipients      TYPE zes_keg_email_recipients WITH HEADER LINE,
        documents          TYPE zes_keg_email_documents,
        recipients         TYPE zes_keg_email_recipients.
 
  DATA: w_area TYPE txt255.
  DATA: tab_usr02 TYPE TABLE OF usr02 WITH HEADER LINE.
 
  WRITE: / 'Test Mode (X, blank): ', p_test.
  ULINE.
 
  "Subject
  MOVE p_subj TO wa_documents_body-subject.
 
  "Get Mail Body if not Test Mode
  IF p_test IS INITIAL.
    "Message Type = File Extension
    DATA: v_file TYPE string, v_ext TYPE string.
 
    SPLIT p_bodyf AT '.' INTO v_file v_ext.
    TRANSLATE v_ext TO UPPER CASE.
    MOVE v_ext TO wa_documents_body-type.
 
    "Build Message body
    LOOP AT tab_body INTO w_area.
      MOVE w_area TO char.
      APPEND char TO wa_documents_body-content_text.
      CLEAR char.
    ENDLOOP.
 
    APPEND wa_documents_body TO documents.
  ENDIF.
 
  "Get Recipients list from SAP - Fill the same table
  IF p_user_from_sap IS NOT INITIAL.
    SELECT * INTO TABLE tab_usr02 FROM usr02
      WHERE bname IN p_user
      AND   trdat => p_date.
 
    LOOP AT tab_usr02.
      w_area = tab_usr02-bname.
      APPEND w_area TO tab_emllst.
    ENDLOOP.
 
  ENDIF.
 
  DATA: i_lines TYPE i,
        p_perc  TYPE p LENGTH 8 DECIMALS 2,
        szmessage(100).
 
  DESCRIBE TABLE tab_emllst LINES i_lines.
 
  "Loop on list
  LOOP AT tab_emllst INTO w_area.
 
    "Choose source: UNAME (List from SAP) or Email (List from file)
    IF p_user_from_sap IS NOT INITIAL.
      wa_recipients-uname = w_area.
    ELSE.
      wa_recipients-c_address = w_area.
    ENDIF.
 
    APPEND wa_recipients TO recipients.
 
    "Send Mail if not Test Mode
    IF p_test IS INITIAL.
 
      "Notify progress
      p_perc = 100 / i_lines * sy-tabix.
      CONCATENATE 'Sending mail to' w_area INTO szmessage SEPARATED BY space.
 
      CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
        EXPORTING
          percentage = p_perc
          text       = szmessage.
 
      "Send Mail
      TRY.
          CALL FUNCTION 'Z_SEND_MAIL'
            EXPORTING
              requested_status = 'E'
              documents        = documents
              recipients       = recipients
              mail_sender      = p_sendr.
        CATCH cx_bcs.
      ENDTRY.
    ENDIF.
 
    WRITE: / w_area.
 
    REFRESH: recipients, wa_recipients.
    CLEAR: recipients, wa_recipients.
 
  ENDLOOP.
 
  "Total
  IF p_test IS INITIAL.
    szmessage = i_lines.
    CONCATENATE 'Processed' szmessage 'Users' INTO szmessage SEPARATED BY space.
    ULINE.
    WRITE: / szmessage.
  ENDIF.
 
ENDFORM.                    "massive_send_mail
*&---------------------------------------------------------------------*
*&      Form  load_file
*&---------------------------------------------------------------------*
FORM load_file TABLES tab_txt_ref USING p_file_name.
 
  DATA: v_file TYPE string.
 
  v_file = p_file_name.
 
  CALL METHOD cl_gui_frontend_services=>gui_upload
    EXPORTING
      filename                = v_file
      filetype                = 'ASC'
    CHANGING
      data_tab                = tab_txt_ref
    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
      not_supported_by_gui    = 17
      error_no_gui            = 18
      OTHERS                  = 19.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
               WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
 
ENDFORM.                    " load_file
 
*&---------------------------------------------------------------------*
*&      Form  help_file
*&---------------------------------------------------------------------*
FORM help_file CHANGING p_file.
 
  CALL FUNCTION 'WS_FILENAME_GET'
    EXPORTING
*     DEF_FILENAME     = ' '
      def_path         = 'C:\'
      mask             = ',HTML files,*.htm;*.html*,Text files,*.txt.'
      mode             = 'O'
      title            = 'Select file (htm, txt)'
    IMPORTING
      filename         = p_file
*     RC               =
    EXCEPTIONS
      inv_winsys       = 1
      no_batch         = 2
      selection_cancel = 3
      selection_error  = 4
      OTHERS           = 5.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE 'S' NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
 
ENDFORM.                    " help_file
programming/abap/massive_send_mail.txt · Last modified: 2013/08/10 13:05 by IperCube