TABLES: SOMLRECI1. |
TYPES : BEGIN OF TY_HEADER, |
VAL TYPE STRING, |
MAT TYPE STRING, |
SDC TYPE STRING, |
SDS TYPE STRING, |
BAT TYPE STRING, |
END OF TY_HEADER. |
DATA : T_HEADER TYPE STANDARD TABLE OF TY_HEADER INITIAL SIZE 1, |
W_HEADER TYPE TY_HEADER, |
T_MAIL TYPE STANDARD TABLE OF TY_HEADER INITIAL SIZE 1, |
W_MAIL TYPE TY_HEADER. |
DATA P_ATTACH TYPE RLGRAP-FILENAME. |
SELECT-OPTIONS: S_MAILID FOR SOMLRECI1-RECEIVER NO INTERVALS NO-DISPLAY. |
DATA PATH TYPE STRING. |
*----------------------------------------------------------------------* |
* GLOBAL DATA DECLARATION * |
*----------------------------------------------------------------------* |
DATA: T_MAILHEX TYPE STANDARD TABLE OF SOLIX, |
T_CONTENTS TYPE STANDARD TABLE OF SOLISTI1, |
WA_CONTENTS TYPE SOLISTI1, |
W_FILE TYPE DSVASDOCID, |
W_EXTN(5) TYPE C, |
W_MAIL_SUBJ TYPE STRING, |
W_DOCUMENT TYPE REF TO CL_DOCUMENT_BCS. |
*----------------------------------------------------------------------* |
* CONSTANTS DECLARATION * |
*----------------------------------------------------------------------* |
CONSTANTS: |
*-- Constants used in the body of the Email (HTML) |
C_HTM TYPE CHAR3 VALUE 'HTM', |
C_STYLE_START TYPE CHAR255 VALUE '<FONT face=Arial size=2>', |
C_NEW_LINE TYPE CHAR255 VALUE '<br>', |
*C_LINK_START TYPE CHAR128 VALUE '<A href="www.google.com">', |
*C_LINK_TEXT TYPE CHAR32 VALUE 'Link for learning HTML', |
*C_LINK_END TYPE CHAR4 VALUE '</A>', |
C_SPACE(6) TYPE C VALUE ' ', |
*-- Used as an Example for displaying space between texts in Email body |
C_EMP1(6) TYPE C VALUE 101001, |
C_EMP2(6) TYPE C VALUE 101002, |
C_EMP3(6) TYPE C VALUE 101003. |
FORM MAIL_HEAD. |
LOOP AT T_RESULT INTO WA_RESULT. |
W_MAIL-VAL = WA_RESULT-FLAG. |
W_MAIL-MAT = WA_RESULT-MATNR. |
W_MAIL-SDC = WA_RESULT-SDCLST. |
W_MAIL-SDS = WA_RESULT-LTEXT. |
W_MAIL-BAT = WA_RESULT-CHARG. |
W_MAIL-ORD = WA_RESULT-AUFNR. |
APPEND W_MAIL TO T_MAIL. |
CLEAR WA_RESULT. |
ENDLOOP. |
CLEAR W_HEADER. |
W_HEADER-VAL = 'Valid'. |
W_HEADER-MAT = 'Material No'. |
W_HEADER-SDC = 'material Class'. |
W_HEADER-SDS = 'material Stage'. |
W_HEADER-BAT = 'Batch No'. |
W_HEADER-ORD = 'Order No'. |
W_HEADER-OPR = 'Operation'. |
APPEND W_HEADER TO T_HEADER. |
CALL METHOD CL_GUI_FRONTEND_SERVICES=>GET_DESKTOP_DIRECTORY |
CHANGING |
DESKTOP_DIRECTORY = PATH |
EXCEPTIONS |
CNTL_ERROR = 1 |
ERROR_NO_GUI = 2 |
NOT_SUPPORTED_BY_GUI = 3 |
OTHERS = 4 |
. |
IF SY-SUBRC <> 0. |
ENDIF. |
CALL METHOD CL_GUI_CFW=>UPDATE_VIEW. |
IF P_VORNR EQ '1'. |
CONCATENATE PATH '\Order_Confirmation.XLS' INTO PATH. |
ELSEIF P_VORNR EQ '2'. |
CONCATENATE PATH '\First_Inspection_Confirmations.XLS' INTO PATH. |
ELSEIF P_VORNR EQ '3'. |
CONCATENATE PATH '\Second_Inspection_Confirmations.XLS' INTO PATH. |
ELSEIF P_VORNR EQ '4'. |
CONCATENATE PATH '\Third_Inspection_Confirmations.XLS' INTO PATH. |
ELSEIF P_VORNR EQ '5'. |
CONCATENATE PATH '\DC_Confirmations.XLS' INTO PATH. |
ENDIF. |
CALL FUNCTION 'GUI_DOWNLOAD' |
EXPORTING |
FILENAME = PATH |
FILETYPE = 'ASC' |
APPEND = ' ' |
WRITE_FIELD_SEPARATOR = 'X' |
TABLES |
DATA_TAB = T_HEADER |
EXCEPTIONS |
OTHERS = 22. |
IF SY-SUBRC <> 0. |
* Implement suitable error handling here |
ENDIF. |
CALL FUNCTION 'GUI_DOWNLOAD' |
EXPORTING |
FILENAME = PATH |
FILETYPE = 'ASC' |
APPEND = 'X' |
WRITE_FIELD_SEPARATOR = 'X' |
TABLES |
DATA_TAB = T_MAIL |
EXCEPTIONS |
OTHERS = 22. |
IF SY-SUBRC <> 0. |
* Implement suitable error handling here |
ENDIF. |
P_ATTACH = PATH. |
CLEAR S_MAILID. |
S_MAILID-SIGN = 'I'. |
S_MAILID-OPTION = 'EQ'. |
S_MAILID-LOW = 'abc@gmail.com'. |
APPEND S_MAILID. |
CLEAR S_MAILID. |
S_MAILID-SIGN = 'I'. |
S_MAILID-OPTION = 'EQ'. |
S_MAILID-LOW = 'def@gmail.com'. |
APPEND S_MAILID. |
CLEAR S_MAILID. |
ENDFORM. |
*&--------------------------------------------------------------------* |
*& Form Name : File_path |
*&--------------------------------------------------------------------* |
*& Description : F4 help for File Name / Path |
*&--------------------------------------------------------------------* |
*& Parameters : FP_FILE --> File Name |
*&--------------------------------------------------------------------* |
FORM FILE_PATH USING FP_FILE TYPE ANY. |
*-- Selects the directory list |
CALL FUNCTION 'F4_DXFILENAME_4_DYNP' |
EXPORTING |
DYNPFIELD_FILENAME = FP_FILE |
DYNAME = SY-CPROG |
DYNUMB = SY-DYNNR |
FILETYPE = 'P' |
LOCATION = 'P' |
SERVER = SPACE. |
ENDFORM. " file_path |
*&---------------------------------------------------------------------* |
*& Form Name : Upload_Data * |
*&---------------------------------------------------------------------* |
*& Description : Upload data into SAP from Presentation Server * |
*&---------------------------------------------------------------------* |
*& Parameters : None * |
*&---------------------------------------------------------------------* |
FORM UPLOAD_DATA. |
*-- Local data declaration |
DATA: L_FILE TYPE STRING, |
L_INDEX TYPE SY-TABIX, |
*-- For holding the split file name |
TL_SPLITFILE TYPE STANDARD TABLE OF RLGRAP-FILENAME, |
WL_SPLITFILE TYPE RLGRAP-FILENAME. |
L_FILE = P_ATTACH. |
**-- Function module to split the Filename and Extension from the Path |
CALL FUNCTION 'CH_SPLIT_FILENAME' |
EXPORTING |
COMPLETE_FILENAME = L_FILE |
IMPORTING |
EXTENSION = W_EXTN |
NAME = W_FILE. |
*-- Split the filename at '.' |
SPLIT L_FILE AT '.' INTO TABLE TL_SPLITFILE. |
DESCRIBE TABLE TL_SPLITFILE LINES L_INDEX. |
*-- In case the filename contains more than one dot |
IF L_INDEX GT 2. |
CLEAR: WL_SPLITFILE, W_EXTN. |
*-- Get the Extension of the file |
READ TABLE TL_SPLITFILE INTO WL_SPLITFILE INDEX L_INDEX. |
W_EXTN = WL_SPLITFILE. |
DELETE TL_SPLITFILE INDEX L_INDEX. |
DELETE TL_SPLITFILE INDEX 1. |
CLEAR WL_SPLITFILE. |
*-- Get the Actual filename |
LOOP AT TL_SPLITFILE INTO WL_SPLITFILE. |
CONCATENATE '.' WL_SPLITFILE INTO WL_SPLITFILE. |
ENDLOOP. |
CONCATENATE W_FILE WL_SPLITFILE INTO W_FILE. |
ENDIF. |
CONDENSE W_EXTN. |
*-- Upload File |
CALL FUNCTION 'GUI_UPLOAD' |
EXPORTING |
FILENAME = L_FILE |
FILETYPE = 'BIN' |
TABLES |
DATA_TAB = T_MAILHEX |
EXCEPTIONS |
FILE_OPEN_ERROR = 1 |
FILE_READ_ERROR = 2 |
NO_BATCH = 3 |
GUI_REFUSE_FILETRANSFER = 4 |
INVALID_TYPE = 5 |
NO_AUTHORITY = 6 |
UNKNOWN_ERROR = 7 |
BAD_DATA_FORMAT = 8 |
HEADER_NOT_ALLOWED = 9 |
SEPARATOR_NOT_ALLOWED = 10 |
HEADER_TOO_LONG = 11 |
UNKNOWN_DP_ERROR = 12 |
ACCESS_DENIED = 13 |
DP_OUT_OF_MEMORY = 14 |
DISK_FULL = 15 |
DP_TIMEOUT = 16 |
OTHERS = 17. |
IF SY-SUBRC IS NOT INITIAL. |
MESSAGE I999(YMSG) WITH 'Error in reading file for upload'(002) |
W_FILE. |
ENDIF. |
ENDFORM. " upload_data |
*&---------------------------------------------------------------------* |
*& Form Name : frame_mail_body * |
*&---------------------------------------------------------------------* |
*& Description : To frame the body part of the Email to be sent * |
*&---------------------------------------------------------------------* |
*& Parameters : None * |
*&---------------------------------------------------------------------* |
FORM FRAME_MAIL_BODY. |
*-- Local data declaration to hold the textpool |
DATA: TL_TEXTPOOL TYPE STANDARD TABLE OF TEXTPOOL, |
WL_TEXTPOOL TYPE TEXTPOOL. |
*-- Read the Entire Textpool into an Internal table |
READ TEXTPOOL SY-REPID INTO TL_TEXTPOOL LANGUAGE SY-LANGU. |
IF SY-SUBRC IS INITIAL. |
SORT TL_TEXTPOOL BY ID KEY. |
ENDIF. |
*-- Font start |
CLEAR WA_CONTENTS. |
WA_CONTENTS-LINE = C_STYLE_START. |
APPEND WA_CONTENTS TO T_CONTENTS. |
*-- New line |
CLEAR WA_CONTENTS. |
WA_CONTENTS-LINE = C_NEW_LINE. |
APPEND WA_CONTENTS TO T_CONTENTS. |
*-- Program name : Email Attachment |
CLEAR: WL_TEXTPOOL, WA_CONTENTS. |
READ TABLE TL_TEXTPOOL INTO WL_TEXTPOOL |
WITH KEY ID = 'I' KEY = 'T02' |
BINARY SEARCH. |
IF SY-SUBRC EQ 0. |
WA_CONTENTS-LINE = WL_TEXTPOOL-ENTRY. |
*-- "#" Present in the Text Element will be replaced by the below value |
REPLACE: '#' WITH 'Email Attachment' INTO WA_CONTENTS-LINE. |
APPEND WA_CONTENTS TO T_CONTENTS. |
ENDIF. |
*-- New line |
CLEAR WA_CONTENTS. |
WA_CONTENTS-LINE = C_NEW_LINE. |
APPEND WA_CONTENTS TO T_CONTENTS. |
*-- New line |
CLEAR WA_CONTENTS. |
WA_CONTENTS-LINE = C_NEW_LINE. |
APPEND WA_CONTENTS TO T_CONTENTS. |
*-- You can also change or add the text here... |
CLEAR: WL_TEXTPOOL, WA_CONTENTS. |
READ TABLE TL_TEXTPOOL INTO WL_TEXTPOOL |
WITH KEY ID = 'I' KEY = 'T03' |
BINARY SEARCH. |
IF SY-SUBRC EQ 0. |
WA_CONTENTS-LINE = WL_TEXTPOOL-ENTRY. |
APPEND WA_CONTENTS TO T_CONTENTS. |
ENDIF. |
*-- New line |
CLEAR WA_CONTENTS. |
WA_CONTENTS-LINE = C_NEW_LINE. |
APPEND WA_CONTENTS TO T_CONTENTS. |
*-- New line |
CLEAR WA_CONTENTS. |
WA_CONTENTS-LINE = C_NEW_LINE. |
APPEND WA_CONTENTS TO T_CONTENTS. |
*-- For giving spaces between texts, you can use... |
CLEAR: WL_TEXTPOOL, WA_CONTENTS. |
READ TABLE TL_TEXTPOOL INTO WL_TEXTPOOL |
WITH KEY ID = 'I' KEY = 'T05' |
BINARY SEARCH. |
IF SY-SUBRC EQ 0. |
WA_CONTENTS-LINE = WL_TEXTPOOL-ENTRY. |
APPEND WA_CONTENTS TO T_CONTENTS. |
ENDIF. |
*-- New line |
CLEAR WA_CONTENTS. |
WA_CONTENTS-LINE = C_NEW_LINE. |
APPEND WA_CONTENTS TO T_CONTENTS. |
*-- For Ex; Employee Numbers : |
CLEAR: WL_TEXTPOOL, WA_CONTENTS. |
READ TABLE TL_TEXTPOOL INTO WL_TEXTPOOL |
WITH KEY ID = 'I' KEY = 'T06' |
BINARY SEARCH. |
IF SY-SUBRC EQ 0. |
WA_CONTENTS-LINE = WL_TEXTPOOL-ENTRY. |
*-- How to give Spaces in between texts |
CONCATENATE WA_CONTENTS-LINE C_SPACE C_EMP1 C_SPACE C_EMP2 |
C_SPACE C_EMP3 INTO WA_CONTENTS-LINE. |
APPEND WA_CONTENTS TO T_CONTENTS. |
ENDIF. |
*-- New line |
CLEAR WA_CONTENTS. |
WA_CONTENTS-LINE = C_NEW_LINE. |
APPEND WA_CONTENTS TO T_CONTENTS. |
*-- New line |
CLEAR WA_CONTENTS. |
WA_CONTENTS-LINE = C_NEW_LINE. |
APPEND WA_CONTENTS TO T_CONTENTS. |
*-- For more Information on HTML.. |
CLEAR: WL_TEXTPOOL, WA_CONTENTS. |
READ TABLE TL_TEXTPOOL INTO WL_TEXTPOOL |
WITH KEY ID = 'I' KEY = 'T07' |
BINARY SEARCH. |
IF SY-SUBRC EQ 0. |
WA_CONTENTS-LINE = WL_TEXTPOOL-ENTRY. |
APPEND WA_CONTENTS TO T_CONTENTS. |
ENDIF. |
*-- New line |
CLEAR WA_CONTENTS. |
WA_CONTENTS-LINE = C_NEW_LINE. |
APPEND WA_CONTENTS TO T_CONTENTS. |
*-- New line |
CLEAR WA_CONTENTS. |
WA_CONTENTS-LINE = C_NEW_LINE. |
APPEND WA_CONTENTS TO T_CONTENTS. |
**-- Hyperlink |
* CLEAR WA_CONTENTS. |
* CONCATENATE C_LINK_START C_LINK_TEXT C_LINK_END INTO WA_CONTENTS-LINE. |
* APPEND WA_CONTENTS TO T_CONTENTS. |
*-- Subject of the Mail |
CONCATENATE TEXT-T01 W_MAIL_SUBJ INTO W_MAIL_SUBJ. |
ENDFORM. " frame_mail_body |
*&---------------------------------------------------------------------* |
*& Form Name : Send_mail * |
*&---------------------------------------------------------------------* |
*& Description : To set the recipients and send the mail * |
*&---------------------------------------------------------------------* |
*& Parameters : None * |
*&---------------------------------------------------------------------* |
FORM SEND_MAIL. |
*-- Local data declaration for sending mail |
DATA: L_SEND_REQUEST TYPE REF TO CL_BCS, |
L_DOCUMENT TYPE REF TO CL_DOCUMENT_BCS, |
L_SENDER TYPE REF TO CL_SAPUSER_BCS, |
L_SUB TYPE CHAR50, |
L_RECIPIENT TYPE REF TO IF_RECIPIENT_BCS, |
TL_CONTENTS TYPE STANDARD TABLE OF SOLI, |
L_DOC_LEN TYPE SO_OBJ_LEN, |
L_CNT TYPE SY-TABIX, |
L_RCV_EMAIL TYPE ADR6-SMTP_ADDR, |
L_RESULT TYPE SY-BINPT, |
L_BCS_EXCEPTION TYPE REF TO CX_BCS, |
L_SUBJ TYPE STRING, |
WL_MAILID LIKE LINE OF S_MAILID. |
TRY. |
*-- Create persistent send request |
L_SEND_REQUEST = CL_BCS=>CREATE_PERSISTENT( ). |
TL_CONTENTS[] = T_CONTENTS[]. |
*-- Get the length of the Document |
DESCRIBE TABLE TL_CONTENTS LINES L_CNT. |
READ TABLE TL_CONTENTS INTO WA_CONTENTS INDEX L_CNT. |
L_DOC_LEN = ( L_CNT - 1 ) * 255 + STRLEN( WA_CONTENTS ). |
*-- Subject of the mail |
L_SUB = W_MAIL_SUBJ. |
*-- Create Document |
L_DOCUMENT = CL_DOCUMENT_BCS=>CREATE_DOCUMENT( |
I_TYPE = C_HTM |
I_TEXT = TL_CONTENTS |
I_LENGTH = L_DOC_LEN |
I_SUBJECT = L_SUB |
I_LANGUAGE = SY-LANGU |
I_IMPORTANCE = '1' ). |
*-- Subject of the mail |
MOVE W_MAIL_SUBJ TO L_SUBJ. |
W_DOCUMENT = L_DOCUMENT. |
TRY. |
*-- Set the Message Subject |
CALL METHOD L_SEND_REQUEST->SET_MESSAGE_SUBJECT |
EXPORTING |
IP_SUBJECT = L_SUBJ. |
CATCH CX_SY_DYN_CALL_ILLEGAL_METHOD. |
ENDTRY. |
*-- Add document to send request |
CALL METHOD L_SEND_REQUEST->SET_DOCUMENT( L_DOCUMENT ). |
*-- Do send delivery info for successful mails |
CALL METHOD L_SEND_REQUEST->SET_STATUS_ATTRIBUTES |
EXPORTING |
I_REQUESTED_STATUS = 'E' |
I_STATUS_MAIL = 'A'. |
*-- Set sender |
L_SENDER = CL_SAPUSER_BCS=>CREATE( SY-UNAME ). |
CALL METHOD L_SEND_REQUEST->SET_SENDER |
EXPORTING |
I_SENDER = L_SENDER. |
*-- To frame the attachments for the mail |
PERFORM FRAME_ATTACHMENTS. |
*-- Add the recipients to the Send mail |
LOOP AT S_MAILID INTO WL_MAILID. |
L_RCV_EMAIL = WL_MAILID-LOW. |
CHECK NOT L_RCV_EMAIL IS INITIAL. |
L_RECIPIENT = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( |
L_RCV_EMAIL ). |
CALL METHOD L_SEND_REQUEST->ADD_RECIPIENT |
EXPORTING |
I_RECIPIENT = L_RECIPIENT |
I_EXPRESS = 'X'. |
ENDLOOP. |
*-- Send Email |
CALL METHOD L_SEND_REQUEST->SEND( |
EXPORTING |
I_WITH_ERROR_SCREEN = 'X' |
RECEIVING |
RESULT = L_RESULT ). |
IF L_RESULT = 'X'. |
MESSAGE S999(YMSG) WITH |
'Status Log Mail Sent Successfully'(003). |
ENDIF. |
CATCH CX_BCS INTO L_BCS_EXCEPTION. |
IF L_RESULT NE 'X'. |
MESSAGE S999(YMSG) WITH |
'Status Log Mail Not Sent'(004). |
ENDIF. |
ENDTRY. |
COMMIT WORK. "Commit Work |
ENDFORM. " send_mail |
*&---------------------------------------------------------------------* |
*& Form Name : frame_attachments * |
*&---------------------------------------------------------------------* |
*& Description : To frame the attachments for the mail to be sent * |
*&---------------------------------------------------------------------* |
*& Parameters : None * |
*&---------------------------------------------------------------------* |
FORM FRAME_ATTACHMENTS. |
*-- Local Data declaration |
DATA: L_SUBJECT TYPE SO_OBJ_DES, |
L_ATT_TYPE TYPE SOODK-OBJTP. |
*-- Subject of the Attachment |
L_SUBJECT = W_FILE. |
*-- Format of the Attachment |
L_ATT_TYPE = W_EXTN. |
IF T_MAILHEX[] IS NOT INITIAL. |
TRY. |
*-- Add Attachment to the Document |
CALL METHOD W_DOCUMENT->ADD_ATTACHMENT |
EXPORTING |
I_ATTACHMENT_TYPE = L_ATT_TYPE |
I_ATTACHMENT_SUBJECT = L_SUBJECT |
I_ATT_CONTENT_HEX = T_MAILHEX. |
CATCH CX_DOCUMENT_BCS. |
ENDTRY. |
ENDIF. |
ENDFORM. " frame_attachments |
ABAP Material,ABAP Techincal Guide,SAP ABAP Sample Programs, ABAP Example Programs, Data Dictionary, BDC, Reports, ALV Reports, Classical Reports, LSMB, Module Pool Programming. ALE/IDOC, RFC, BAPI.
Wednesday, July 24, 2013
ABAP Email Program
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment