Posted: Sun Mar 09, 2008 10:57 am Post subject: Function Module to send email (By user name)
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.
* 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.
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.
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.