SAP Community Network Wiki - Code Gallery - Abap EMAIL Program Abap EMAIL Program View Comments (0) Welcome Guest Info Additional Features Added by Runal Singh , last edited by Bhumika Mahawar on Mar 05, 2009 (view change) Labels: (None) Author: Runal Singh Submitted: 16/01/2008 FUNCTION z_send_email_itab_uname. *"----------------------------------------------------------------------*" *"Local interface: *" IMPORTING *" VALUE(APPLICATION) LIKE *" VALUE(EMAILTITLE) LIKE *" VALUE(RECEXTNAM) LIKE *" VALUE(SENDER) LIKE *" *" VALUE(UNAME) LIKE SOOS1-RECEXTNAM OPTIONAL SOUD-USRNAM DEFAULT SY-UNAME SOOS1-RECEXTNAM OPTIONAL EXPORTING *" *" SOOD1-OBJNAM SOOD1-OBJDES 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* WHILE recextnam CS ','. http://wiki.sdn.sap.com/wiki/display/Snippets/Abap+EMAIL+Program[2010-08-03 오후 3:26:41] SAP Community Network Wiki - Code Gallery - Abap EMAIL Program PERFORM init_rec TABLES receivers. READ TABLE receivers INDEX 1. r eceivers-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 USING receivers STRUCTURE soos1 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 = oke 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!'. http://wiki.sdn.sap.com/wiki/display/Snippets/Abap+EMAIL+Program[2010-08-03 오후 3:26:41] SAP Community Network Wiki - Code Gallery - Abap EMAIL Program 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. http://wiki.sdn.sap.com/wiki/display/Snippets/Abap+EMAIL+Program[2010-08-03 오후 3:26:41] SAP Community Network Wiki - Code Gallery - Abap EMAIL Program * 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 http://wiki.sdn.sap.com/wiki/display/Snippets/Abap+EMAIL+Program[2010-08-03 오후 3:26:41]