Sei sulla pagina 1di 8

*&---------------------------------------------------------------------*

*& Report ZHRR_PFMAIL


*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZHRR_PFMAIL.
*REPORT zsend_mail.
*----------------------------------------------------------------------*
*
TABLES
*
*----------------------------------------------------------------------*
TABLES: SOMLRECI1.
*----------------------------------------------------------------------*
*
GLOBAL DATA DECLARATION
*
*-------------DATA: ITAB LIKE ALSMEX_TABLINE OCCURS 0 WITH HEADER LINE.
* Has the following format:
*
Row number | Colum Number | Value
*
--------------------------------------*
i.e.
1
1
Name1
*
2
1
Joe
TYPES: BEGIN OF T_RECORD,
PFNO LIKE ITAB-VALUE,
MAILID LIKE ITAB-VALUE,
END OF T_RECORD.
DATA: IT_RECORD TYPE STANDARD TABLE OF T_RECORD INITIAL SIZE 0,
WA_RECORD TYPE T_RECORD.
DATA: GD_CURRENTROW TYPE I.
DATA: T_MAILHEX
T_CONTENTS
WA_CONTENTS
W_FILE
W_EXTN(5)
W_MAIL_SUBJ
W_DOCUMENT

TYPE
TYPE
TYPE
TYPE
TYPE
TYPE
TYPE

STANDARD TABLE OF SOLIX,


STANDARD TABLE OF SOLISTI1,
SOLISTI1,
DSVASDOCID,
C,
STRING,
REF TO CL_DOCUMENT_BCS.

DATA: LV_PATH TYPE RLGRAP-FILENAME.


*----------------------------------------------------------------------*
*
CONSTANTS DECLARATION
*
*----------------------------------------------------------------------*
CONSTANTS:
*-- Constants used in the body of the Email (HTML)
C_HTM
TYPE CHAR3 VALUE 'HTM',
C_STYLE_START TYPE CHAR255 VALUE '',
C_NEW_LINE
TYPE CHAR255 VALUE '',
C_LINK_START TYPE CHAR128 VALUE '',
C_LINK_TEXT TYPE CHAR32 VALUE 'Link for learning HTML',
C_LINK_END
TYPE CHAR4 VALUE '',
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)
C_EMP3(6)

TYPE C
TYPE C

VALUE 101002,
VALUE 101003.

*----------------------------------------------------------------------*
*
SELECTION SCREEN
*
*----------------------------------------------------------------------*
*-- Input Details - Block
SELECTION-SCREEN BEGIN OF BLOCK FILE WITH FRAME TITLE TEXT-T01.
PARAMETERS: P_ATTACH TYPE RLGRAP-FILENAME.
PARAMETER P_INFILE LIKE RLGRAP-FILENAME OBLIGATORY.
SELECTION-SCREEN END OF BLOCK FILE.
*----------------------------------------------------------------------*
*
AT SELECTION SCREEN
*
*----------------------------------------------------------------------*
*-- Providing F4 Help for the input file
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_INFILE.
PERFORM FILE_PATH USING 'P_INFILE'.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_ATTACH.
PERFORM FILE_PATH USING 'P_ATTACH'.
*&---------------------------------------------------------------------*
*
START-OF-SELECTION
*
*&---------------------------------------------------------------------*
START-OF-SELECTION.
PERFORM GET_MAILID.
IF P_ATTACH IS INITIAL.
MESSAGE I999(ZZ) WITH 'Please Enter at-least one file name'(001).
EXIT.
ENDIF.
*-- Frame the Body of the Email
PERFORM FRAME_MAIL_BODY.
LV_PATH = P_ATTACH .
LOOP AT IT_RECORD INTO WA_RECORD.
CONDENSE WA_RECORD-PFNO.
CONCATENATE LV_PATH WA_RECORD-PFNO '.PDF' INTO P_ATTACH .
*-- Upload data from Presentation Server to SAP
PERFORM UPLOAD_DATA.
*-- Send Mail
PERFORM SEND_MAIL.
ENDLOOP.
*&--------------------------------------------------------------------*
*& 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
DYNAME
DYNUMB
FILETYPE
LOCATION
SERVER

=
=
=
=
=
=

FP_FILE
SY-CPROG
SY-DYNNR
'P'
'P'
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
EXCEPTIONS
FILE_OPEN_ERROR
FILE_READ_ERROR
NO_BATCH
GUI_REFUSE_FILETRANSFER
INVALID_TYPE
NO_AUTHORITY
UNKNOWN_ERROR
BAD_DATA_FORMAT
HEADER_NOT_ALLOWED
SEPARATOR_NOT_ALLOWED
HEADER_TOO_LONG
UNKNOWN_DP_ERROR
ACCESS_DENIED
DP_OUT_OF_MEMORY
DISK_FULL
DP_TIMEOUT
OTHERS

= T_MAILHEX
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17.

IF SY-SUBRC IS NOT INITIAL.


MESSAGE i999(zz) 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.
*-- 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: WA_CONTENTS.
WA_CONTENTS-LINE = 'Please find attached Annual PF Statement for year 2011-12'
.
APPEND WA_CONTENTS TO T_CONTENTS.
WA_CONTENTS-LINE = 'Regards'.
APPEND WA_CONTENTS TO T_CONTENTS.
WA_CONTENTS-LINE = 'Ravi Kant Agrawal, P&A Dept, Jindal Power Limited'.
APPEND WA_CONTENTS TO T_CONTENTS.
W_MAIL_SUBJ = 'Annual PF Statement:2011-12'.

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 = WA_RECORD-MAILID. "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(zz) WITH
'Mail Sent Successfully'(003).
ENDIF.

CATCH CX_BCS INTO L_BCS_EXCEPTION.


IF L_RESULT NE 'X'.
*
MESSAGE s999(zz) WITH
*
'Mail Not Successful'(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
*&---------------------------------------------------------------------*
*&
Form GET_MAILID
*&---------------------------------------------------------------------*
*
text
*----------------------------------------------------------------------*
* --> p1
text
* <-- p2
text
*----------------------------------------------------------------------*
FORM GET_MAILID .
CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
EXPORTING
FILENAME
= P_INFILE
I_BEGIN_COL
= '1'
I_BEGIN_ROW
= '2' "Do not require headings
I_END_COL
= '14'
I_END_ROW
= '31'
TABLES
INTERN
= ITAB
EXCEPTIONS
INCONSISTENT_PARAMETERS = 1
UPLOAD_OLE
= 2
OTHERS
= 3.
IF SY-SUBRC <> 0.
MESSAGE E010(ZZ) WITH TEXT-001. "Problem uploading Excel Spreadsheet
ENDIF.
* Sort table by rows and colums
SORT ITAB BY ROW COL.
* Get first row retrieved
READ TABLE ITAB INDEX 1.
* Set first row retrieved to current row
GD_CURRENTROW = ITAB-ROW.
*

LOOP AT ITAB.
Reset values for next row
IF ITAB-ROW NE GD_CURRENTROW.
APPEND WA_RECORD TO IT_RECORD.
CLEAR WA_RECORD.
GD_CURRENTROW = ITAB-ROW.
ENDIF.

CASE ITAB-COL.
WHEN '0001'.
"PF No.
WA_RECORD-PFNO = ITAB-VALUE.
WHEN '0002'.
"Mail ID
WA_RECORD-MAILID = ITAB-VALUE.
ENDCASE.
ENDLOOP.
APPEND WA_RECORD TO IT_RECORD.
*!! Excel data is now contained within the internal table IT_RECORD
ENDFORM.

" GET_MAILID