SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

Function Module to send email (By user name)



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Programming Techniques | Приемы программирования -> Mail
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1639

PostPosted: Sun Mar 09, 2008 10:57 am    Post subject: Function Module to send email (By user name) Reply with quote

Function Module to send email (By user name)
AUTHOR: Kevin Wilson
Original: http: //www.erpgenie.com/abap/code/abap25.htm

You can call this function module and pass in either login ids (separated by commas) or email addresses (separated by commas) or both. Login IDs go in UNAME and email addresses go in RECEXTNAM.

Code:
FUNCTION z_send_email_itab_uname.
*"----------------------------------------------------------------------
*"*"Local interface:
*"  IMPORTING
*"     VALUE(APPLICATION) LIKE  SOOD1-OBJNAM
*"     VALUE(EMAILTITLE) LIKE  SOOD1-OBJDES
*"     VALUE(RECEXTNAM) LIKE  SOOS1-RECEXTNAM OPTIONAL
*"     VALUE(SENDER) LIKE  SOUD-USRNAM DEFAULT SY-UNAME
*"     VALUE(UNAME) LIKE  SOOS1-RECEXTNAM OPTIONAL
*"  EXPORTING
*"     VALUE(RETURN_CODE) LIKE  SY-SUBRC
*"  TABLES
*"      TEXTTAB STRUCTURE  SOLI
*"----------------------------------------------------------------------

* tables
  TABLES: pa0105,   "Employee detail for username and email address
          usr21,    "Login addition address data
          adr6.     "Address table for email addresses

*- local data declaration
  DATA: ohd    LIKE sood1,
        oid    LIKE soodk,
        to_all LIKE sonv-flag,
        t_uname LIKE sy-uname,
        okey   LIKE swotobjid-objkey.
  DATA: BEGIN OF receivers OCCURS 0.
          INCLUDE STRUCTURE soos1.
  DATA: END OF receivers.

*- fill odh
  CLEAR ohd.
  ohd-objla    = sy-langu.
  ohd-objnam   = application.
  ohd-objdes   = emailtitle.
  ohd-objpri   = 3.
  ohd-objsns   = 'F'.
  ohd-ownnam   = sy-uname.

  IF NOT uname IS INITIAL.

* Find personnel number of username
    CONDENSE uname NO-GAPS.

    WHILE uname CS ','.
      t_uname = uname+0(sy-fdpos).
      ADD 1 TO sy-fdpos.
      SHIFT uname LEFT BY sy-fdpos PLACES.

      PERFORM find_email_address USING t_uname
                                 CHANGING recextnam.
    ENDWHILE.
*Do the last record
    IF uname <> space.
      t_uname = uname.
      PERFORM find_email_address USING t_uname
                                 CHANGING recextnam.

    ENDIF.

  ENDIF.

*- send Email
  IF NOT recextnam IS INITIAL.

    CONDENSE recextnam NO-GAPS.
    CHECK recextnam CS '@'.

  ENDIF.

*- for every individual recipient send an Email
* (see OSS message 0120050409/0000362105/1999)

  WHILE recextnam CS ','.
    PERFORM init_rec TABLES receivers.
    READ TABLE receivers INDEX 1.
    receivers-recextnam = recextnam+0(sy-fdpos).
    ADD 1 TO sy-fdpos.
    SHIFT recextnam LEFT BY sy-fdpos PLACES.
    MODIFY receivers INDEX 1.
    PERFORM so_object_send_rec
     TABLES texttab receivers
      USING ohd
            sender.
  ENDWHILE.

*- check last recipient in recipient list
  IF recextnam <> space.
    PERFORM init_rec TABLES receivers.
    READ TABLE receivers INDEX 1.
    receivers-recextnam = recextnam.
    MODIFY receivers INDEX 1.
    PERFORM so_object_send_rec
     TABLES texttab receivers
      USING ohd
            sender.
  ENDIF.


ENDFUNCTION.



*---------------------------------------------------------------------*
*       FORM SO_OBJECT_SEND_REC                                       *
*---------------------------------------------------------------------*

FORM  so_object_send_rec

TABLES  objcont      STRUCTURE soli
        receivers    STRUCTURE soos1
USING   object_hd    STRUCTURE sood1
        sender LIKE soud-usrnam.

  DATA:   oid     LIKE soodk,
          to_all  LIKE sonv-flag,
          okey    LIKE swotobjid-objkey.

  CALL FUNCTION 'SO_OBJECT_SEND'
       EXPORTING
            extern_address             = 'X'
            object_hd_change           = object_hd
            object_type                = 'RAW'
            outbox_flag                = 'X'
            sender                     = sender
       IMPORTING
            object_id_new              = oid
            sent_to_all                = to_all
            office_object_key          = okey
       TABLES
            objcont                    = objcont
            receivers                  = receivers
       EXCEPTIONS
            active_user_not_exist      = 1
            communication_failure      = 2
            component_not_available    = 3
            folder_not_exist           = 4
            folder_no_authorization    = 5
            forwarder_not_exist        = 6
            note_not_exist             = 7
            object_not_exist           = 8
            object_not_sent            = 9
            object_no_authorization    = 10
            object_type_not_exist      = 11
            operation_no_authorization = 12
            owner_not_exist            = 13
            parameter_error            = 14
            substitute_not_active      = 15
            substitute_not_defined     = 16
            system_failure             = 17
            too_much_receivers         = 18
            user_not_exist             = 19
            x_error                    = 20
            OTHERS                     = 21.

  IF sy-subrc <> 0.
    CASE sy-subrc.
      WHEN 1.
        MESSAGE i999(b1) WITH 'Activer user does not exist.'
                          'Send Mail failed!'.
      WHEN 2.
        MESSAGE i999(b1) WITH 'Communication Failure.'
                          'Send Mail failed!'.
      WHEN 3.
        MESSAGE i999(b1) WITH 'Component not available.'
                          'Send Mail failed!'.
      WHEN 4.
        MESSAGE i999(b1) WITH 'Folder does not exist..'
                          'Send Mail failed!'.
      WHEN 5.
        MESSAGE i999(b1) WITH 'No authorization for folder.'
                          'Send Mail failed!'.
      WHEN 6.
        MESSAGE i999(b1) WITH 'Forwarder does not exist.'
                          'Send Mail failed!'.
      WHEN 7.
        MESSAGE i999(b1) WITH 'Note does not exist.'
                          'Send Mail failed!'.
      WHEN 8.
        MESSAGE i999(b1) WITH 'Object does not exist.'
                          'Send Mail failed!'.
      WHEN 9.
        MESSAGE i999(b1) WITH 'Object not sent.'
                          'Send Mail failed!'.
      WHEN 10.
        MESSAGE i999(b1) WITH 'No authorization for object.'
                          'Send Mail failed!'.
      WHEN 11.
        MESSAGE i999(b1) WITH 'Object type does not exist.'
                          'Send Mail failed!'.
      WHEN 12.
        MESSAGE i999(b1) WITH 'No authorization for operation.'
                          'Send Mail failed!'.
      WHEN 13.
        MESSAGE i999(b1) WITH 'Owner does not exist.'
                          'Send Mail failed!'.
      WHEN 14.
        MESSAGE i999(b1) WITH 'Parameter Error.'
                          'Send Mail failed!'.
      WHEN 15.
        MESSAGE i999(b1) WITH 'Substitute not active.'
                          'Send Mail failed!'.
      WHEN 16.
        MESSAGE i999(b1) WITH 'Substitute not defined.'
                          'Send Mail failed!'.
      WHEN 17.
        MESSAGE i999(b1) WITH 'System failure.'
                          'Send Mail failed!'.
      WHEN 18.
        MESSAGE i999(b1) WITH 'Too many receivers.'
                          'Send Mail failed!'.
      WHEN 19.
        MESSAGE i999(b1) WITH 'User does not exist.'
                          'Send Mail failed!'.
      WHEN 20.
        MESSAGE i999(b1) WITH 'Unknown error ocurred.'
                          'Send Mail failed!'.
      WHEN 21.
        MESSAGE i999(b1) WITH 'Unknown error ocurred.'
                          'Send Mail failed!'.
    ENDCASE.
  ENDIF.

ENDFORM.
*
*
*
**---------------------------------------------------------------------*
**       FORM INIT_REC                                                 *
**---------------------------------------------------------------------*

FORM init_rec TABLES receivers STRUCTURE soos1.
  CLEAR receivers.
  REFRESH receivers.
  MOVE sy-datum  TO receivers-rcdat .
  MOVE sy-uzeit  TO receivers-rctim.
  MOVE '1'       TO receivers-sndpri.
  MOVE 'X'       TO receivers-sndex.
  MOVE 'U-'      TO receivers-recnam.
  MOVE 'U'       TO receivers-recesc.
  MOVE 'INT'     TO receivers-sndart.
  MOVE '5'       TO receivers-sortclass.
  APPEND receivers.

ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  find_email_address
*&---------------------------------------------------------------------*
*       Returns the email address for a USERID whether they are an
*       employee or just a user.
*----------------------------------------------------------------------*
*      <--P_RECEXTNAM  Email address
*      -->P_UNAME      USERID
*----------------------------------------------------------------------*
FORM find_email_address USING    p_uname
                        CHANGING p_recextnam LIKE soos1-recextnam.

  DATA:   t_pernr LIKE pa0105-pernr,
          t_email LIKE pa0105-usrid_long.

* STEP 1 - Find personnel number for UNAME
  SELECT SINGLE pernr INTO t_pernr
    FROM pa0105
    WHERE subty = '0001' AND
          endda >= sy-datum AND
          begda <= sy-datum AND
          usrid = p_uname.

* Employee record found
  IF sy-subrc = 0.
* STEP 2 - Find email address for personnel number
    SELECT SINGLE usrid_long INTO t_email
      FROM pa0105
      WHERE pernr = t_pernr AND
            subty = '0022' AND
            endda >= sy-datum AND
            begda <= sy-datum.
    IF sy-subrc = 0.
      IF NOT p_recextnam IS INITIAL.
        CONCATENATE p_recextnam t_email
          INTO p_recextnam SEPARATED BY ','.
      ELSE.
        p_recextnam = t_email.
      ENDIF.
    ELSE.  "Never found an email address for employee
*** WHO SHOULD WE SEND TO HERE??? ***
    ENDIF.
* Employee number not found. Search in ADR6 table
  ELSE.

* See if user name is in the username table.
* i.e. The user is not an employee
    SELECT SINGLE addrnumber persnumber
      INTO (usr21-addrnumber, usr21-persnumber)
      FROM usr21
      WHERE bname = p_uname.
    IF sy-subrc = 0.
* Found user name now let's get the email address
      SELECT SINGLE smtp_addr INTO t_email
        FROM adr6
        WHERE addrnumber = usr21-addrnumber AND
              persnumber = usr21-persnumber.
      IF sy-subrc <> 0 OR t_email IS INITIAL.
*** WHO SHOULD WE SEND TO HERE??? ***
      ELSE.  "Found email
        IF NOT p_recextnam IS INITIAL.
          CONCATENATE p_recextnam t_email
            INTO p_recextnam SEPARATED BY ','.
        ELSE.
          p_recextnam = t_email.
        ENDIF.
      ENDIF.
    ELSE.   "UNAME just does not have an email address addigned at all
*** WHO SHOULD WE SEND TO HERE??? ***
    ENDIF.

  ENDIF.

ENDFORM.                    " find_email_address
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Programming Techniques | Приемы программирования -> Mail All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.