Shared Top Border

Enterprise Resource
Planning Portal

 

Advertise | Founder BLOG

ERPGenie.COM ABAP Tips and Tricks Database

THE ultimate
ERP website

 

Forums | Vote for us |

Google    Other Search Options

Login

Login to view more content!!!





Lost Password?
No account yet? Register

Registered Access

Poll

What area of ABAP are you interested in?
 
Home arrow Sample Code arrow ABAP Reports arrow Sending email via Function Module
Sending email via Function Module PDF Print E-mail
User Rating: / 0
PoorBest 
Written by Kevin Wilson   
Saturday, 20 January 2007
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.  Author: Kevin Wilson

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


 
< Prev   Next >

Google Search

Statistics

Contribution Activity
Utilities: 38
Tips and Tricks: 330
Sample Code: 164
Total Contributions: 545

Member Activity
Members: 6061 since 2/1/2007!
New: 0 since yesterday!
Visitors: 959237
We have 3 guests online

Newest Members

Welcome our newest members:

Google Ads

Shared Bottom Border

Contact Us | Polls | Add URL | Contribute | Privacy | Terms | Feedback

Discussion Forum | BLOG | Consultants: Post your resume | Companies: Advertise on ERPGenie.COM | Post Job
Financials Consultant | Consultant Review | Gallia Consulting | Supply Chain Project | SAP Financials Forum
GenieHoldings.COM, Inc. | Genie Press | WorkflowGenie | ESAGenie | ERPTopSites | ABAP Tips and Tricks | SAP Solutions Database

EDIGenie | Searching Survivor