Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

Sending Mails - Home Page

Author: Suresh Maryala
Submitted: 07/10/2008
Related Links:

  • Selection Screen Details

As part of the Selection screen user inputs Training Name, Location, Begin date, End Date, Pernr or User type and Pernr number or user id. . 

Program Logic

Based on information provided to selection screen, program reads infotype 105 to find the user id or pernr and also the e-mail address. User id is stored in sub type 0001 and e-mail address is stored in sub type 0010.  Process

  • *# Read infotype 0105 to find the PERNR, User ID and E-mail address
    1. Build internal table for Microsoft calendar entry
    2. Build training details to be part of text
    3. Call so_raw_to_rtf to convert the calendar entry details to have line breaks
    4. Call method send_request to send the e-mail 
    5. Add entry in table TSOPE with VCS and ASCII check box selected
  •  

ABAP Code

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.
DATA: it_content         TYPE soli_tab.
DATA: it_attach          TYPE soli_tab.
DATA: send_request       TYPE REF TO cl_bcs.
DATA: l_subject          LIKE sodocchgi1-obj_descr,
      l_sender_type      LIKE soextreci1-adr_typ,
      l_recsize          TYPE i,
      l_receiver         TYPE sy-subrc.
DATA: lt_soli            TYPE soli_tab    .
DATA: ls_soli            LIKE LINE OF lt_soli.
DATA: lt_soli_new        TYPE soli_tab.
DATA: ls_email           TYPE pa0105-usrid_long.
DATA: ls_uname           TYPE pa0105-usrid.
DATA: ls_pernr           TYPE pernr-pernr.
DATA: ls_begin(10)       TYPE c.
DATA: ls_end(10)         TYPE c.
DATA: result        TYPE REF TO cl_document_bcs.
DATA: BEGIN OF ls_data,
  sender TYPE lsoppvar-sender,  "sender
  kbgda  TYPE ppvar-kbgda,      "begin date
  kndda  TYPE ppvar-endda,      "end date
  kbtim  TYPE ppvar-kbtim,      "begin time first day
  ketim  TYPE ppvar-ketim,      "end time last day
  kstxt  TYPE ppvar-kstxt,      "training text
  b_ort  TYPE ppvar-b_ort,      "training location
  rstxt  TYPE ppvar-rstxt,      "room text
  amail  TYPE lsoppvar-amail,   "recipient eMail
  astxt  TYPE ppvar-astxt,      "recipient
END OF ls_data.
FIELD-SYMBOLS: <fs_soli> LIKE ls_soli.
************************************************************************
* SELECTION SCREEN DEFINITION
************************************************************************
SELECTION-SCREEN BEGIN OF BLOCK group WITH FRAME TITLE text-001.
PARAMETERS:  p_trnam   LIKE p1000-stext DEFAULT 'TRAINING NAME',
             p_locnm   LIKE p1000-stext DEFAULT 'LOCATION',
             p_begda   LIKE p1000-begda DEFAULT '20081101',
             p_endda   LIKE p1000-endda DEFAULT '20081104',
             p_ltype   LIKE lso_borkey_participation-learnertype DEFAULT 'P',
             p_lid     LIKE lso_borkey_participation-learnerid DEFAULT '10000137'.
SELECTION-SCREEN END   OF BLOCK group.
************************************************************************
* INITIALIZATION
************************************************************************
INITIALIZATION.

START-OF-SELECTION.
  CASE p_ltype.
    WHEN 'P'.
* Move PERNR number to variable
      ls_pernr = p_lid.
    WHEN 'US'.
      ls_uname = p_lid.
      CONDENSE ls_uname.
      SELECT SINGLE pernr FROM pa0105 INTO ls_pernr WHERE
                                usrid = ls_uname AND
                                begda LE sy-datum AND
                                endda GE sy-datum AND
                                subty = '0001'.
  ENDCASE.
* Read infotype 0105 and subtypes 0001 and 0010
* Sub type 0001 has username
* sub type 0010 has e-mail
  SELECT SINGLE usrid_long FROM pa0105 INTO ls_email WHERE
                         pernr = ls_pernr AND
                         begda LE sy-datum AND
                         endda GE sy-datum AND
                         subty = '0010'.
  SELECT SINGLE usrid FROM pa0105 INTO ls_uname WHERE
                         pernr = ls_pernr AND
                         begda LE sy-datum AND
                         endda GE sy-datum AND
                         subty = '0001'.
  PERFORM f_data.
*&---------------------------------------------------------------------*
*&      Form  f_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM f_data .
* Calendar logic begin
  ls_soli-line = 'BEGIN:VCALENDAR'.
  APPEND ls_soli TO lt_soli.
  ls_soli-line = 'PRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//EN'.
  APPEND ls_soli TO lt_soli.
  ls_soli-line = 'VERSION:1.0'.
  APPEND ls_soli TO lt_soli.
  ls_soli-line = 'METHOD:REQUEST'.
  APPEND ls_soli TO lt_soli.
  ls_soli-line = 'BEGIN:VEVENT'.
  APPEND ls_soli TO lt_soli.
  CONCATENATE 'ATTENDEE;CN=' ls_uname ';ROLE=REQ-PARTICIPANT;RSVP=TRUE:MAILTO:' ls_email INTO ls_soli-line.
  APPEND ls_soli TO lt_soli.
  CONCATENATE 'DTSTART:' p_begda 'T140000Z' INTO ls_soli-line.
  APPEND ls_soli TO lt_soli.
  CONCATENATE 'DTEND:' p_endda 'T220000Z' INTO ls_soli-line.
  APPEND ls_soli TO lt_soli.
  CONCATENATE 'LOCATION:' p_locnm INTO ls_soli-line.
  APPEND ls_soli TO lt_soli.
  ls_soli-line = 'TRANSP:OPAQUE'.
  APPEND ls_soli TO lt_soli.
  ls_soli-line = 'SEQUENCE:1'.
  APPEND ls_soli TO lt_soli.
  ls_soli-line = 'DESCRIPTION: Course Participation'.
  APPEND ls_soli TO lt_soli.
  CONCATENATE 'SUMMARY: Course Participation for ' ls_uname INTO ls_soli-line SEPARATED BY space.
  APPEND ls_soli TO lt_soli.
  ls_soli-line = 'PRIORITY:5'.
  APPEND ls_soli TO lt_soli.
  ls_soli-line = 'X-MICROSOFT-CDO-IMPORTANCE:1'.
  APPEND ls_soli TO lt_soli.
  ls_soli-line = 'CLASS:PUBLIC'.
  APPEND ls_soli TO lt_soli.
  ls_soli-line = 'END:VEVENT'.
  APPEND ls_soli TO lt_soli.
  ls_soli-line = 'END:VCALENDAR'.
  APPEND ls_soli TO lt_soli.
* End of Calendar
* Call this function module to convert data created for calendar entry
*
  CALL FUNCTION 'SO_RAW_TO_RTF'
    TABLES
      objcont_old = lt_soli[]
      objcont_new = lt_soli_new[].
  l_subject = 'Course Participation'.
  ls_soli-line = 'This is to notify following course has been approved'.
  APPEND ls_soli TO it_content.
  CLEAR ls_soli.
  APPEND ls_soli TO it_content.
  CONCATENATE 'Training Name: ' p_trnam INTO ls_soli-line.
  APPEND ls_soli TO it_content.
  CLEAR ls_soli.
  APPEND ls_soli TO it_content.
  WRITE p_begda TO ls_begin.
  WRITE p_endda TO ls_end.
  CONCATENATE 'Begin Date: ' ls_begin INTO ls_soli-line.
  APPEND ls_soli TO it_content.
  CLEAR ls_soli.
  APPEND ls_soli TO it_content.
  CONCATENATE 'End Date: ' ls_end INTO ls_soli-line.
  APPEND ls_soli TO it_content.
  CLEAR ls_soli.
  APPEND ls_soli TO it_content.
  CONCATENATE 'Location Name: ' p_locnm INTO ls_soli-line.
  APPEND ls_soli TO it_content.
  CLEAR ls_soli.
  APPEND ls_soli TO it_content.
  TRY.
* Create persistent send request
      send_request = cl_bcs=>create_persistent( ).
      document = cl_document_bcs=>create_document(
                                    i_type    = 'RAW'
                                    i_text = it_content[]
                                    i_subject = l_subject ).
      DESCRIBE TABLE lt_soli.
      CALL METHOD document->add_attachment
        EXPORTING
          i_attachment_type    = 'VCS'
          i_attachment_subject = 'Course'
          i_att_content_text   = lt_soli_new[].
* Add document to send request
      CALL METHOD send_request->set_document( document ).
* Get sender object
      sender = cl_sapuser_bcs=>create( sy-uname ).
* Add sender
      CALL METHOD send_request->set_sender
        EXPORTING
          i_sender = sender.
*     ------ add recipient (e-mail address) ----------------------
*     create recipient

*  Enter a valid e-mail address
      recipient = cl_cam_address_bcs=>create_internet_address(
                                        ls_email ).
*     add recipient with its respective attributes to send request
      CALL METHOD send_request->add_recipient
        EXPORTING
          i_recipient  = recipient
          i_express    = 'U'
          i_copy       = ' '
          i_blind_copy = ' '
          i_no_forward = ' '.
********Trigger e-mails immediately****************************
      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'.
        WRITE 'Document Sent Successfully'.
      ENDIF.
      COMMIT WORK.
    CATCH cx_document_bcs INTO bcs_exception.
      WRITE: 'Error Occurred'.
      WRITE: 'Error Type', bcs_exception->error_type.
      EXIT.
  ENDTRY.
ENDFORM.                    " f_data