*&---------------------------------------------------------------------*
*& REPORT ZATTACH_EMAIL *
*&---------------------------------------------------------------------*
*& Description: Program to upload attachments and send it to the *
*& required Email ID *
*&---------------------------------------------------------------------*
*& Modification Log: *
*& *
*& Date Name User ID Transport # *
*& Description of Modifications *
*&---------------------------------------------------------------------*
*& Amit Bisht *
*& -Initial version *
*&---------------------------------------------------------------------*
REPORT zattach_email MESSAGE-ID zz
LINE-SIZE 132
NO STANDARD PAGE HEADING.
*----------------------------------------------------------------------*
* TABLES *
*----------------------------------------------------------------------*
TABLES: somlreci1.
*----------------------------------------------------------------------*
* 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 '',
c_new_line TYPE char255 VALUE '
',
',
c_link_start TYPE char128 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) TYPE c VALUE 101002,
c_emp3(6) TYPE c VALUE 101003.
*----------------------------------------------------------------------*
* SELECTION SCREEN *
*----------------------------------------------------------------------*
*-- Input Details - Block
SELECTION-SCREEN BEGIN OF BLOCK file WITH FRAME TITLE text-t01.
PARAMETERS: p_attach TYPE rlgrap-filename.
SELECTION-SCREEN END OF BLOCK file.
*-- Email ID of the Recipient
SELECTION-SCREEN BEGIN OF BLOCK mail WITH FRAME TITLE text-t02.
SELECT-OPTIONS: s_mailid FOR somlreci1-receiver NO INTERVALS.
SELECTION-SCREEN END OF BLOCK mail.
*----------------------------------------------------------------------*
* AT SELECTION SCREEN *
*----------------------------------------------------------------------*
*-- Providing F4 Help for the input file
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_attach.
PERFORM file_path USING 'P_ATTACH'.
*&---------------------------------------------------------------------*
* START-OF-SELECTION *
*&---------------------------------------------------------------------*
START-OF-SELECTION.
IF p_attach IS INITIAL.
MESSAGE i999(zz) WITH 'Please Enter at-least one file name'(001).
EXIT.
ENDIF.
*-- Upload data from Presentation Server to SAP
PERFORM upload_data.
*----------------------------------------------------------------------*
* END OF SELECTION *
*----------------------------------------------------------------------*
END-OF-SELECTION.
*-- Frame the Body of the Email
PERFORM frame_mail_body.
*-- Send Mail
PERFORM send_mail.
*&--------------------------------------------------------------------*
*& 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(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.
*-- 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 = 'T03'
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 = 'T04'
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-t08 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(zz) WITH
'Approval Mail Sent Successfully'(003).
ENDIF.
CATCH cx_bcs INTO l_bcs_exception.
IF l_result NE 'X'.
MESSAGE s999(zz) WITH
'Approval 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
No comments:
Post a Comment