Sending Smartform PDF as an E-Mail Attachment either to SAP User or to External User
- Till, our previous part we saw, how we can download our smartform in form of PDF format.
- In this part, we will see, how to send the PDF into mail.
- To send mail into PDF we will make use of the various standard classes of SAP related to BCS ( Business Communication Services ).
- Also, we have SBWP transaction code where we can see the mail which we have sent.
- We can use SOST transaction code while sending the mail to external system in order to check whether everything is perfect fine or not in our system.
Important Classes used for Email Purpose :-
- The various standard classes we will use for this purpose are as follows :-
- CL_BCS :-
- This class is used for creating the send request, adding the recipient, sending the document etc.
- CL_DOCUMENT_BCS :-
- This class is used tor creating the document, adding the attachment etc.
- We will use this class for writing documents
- CL_SAPUSER_BCS :-
- This class is used to create SAP users.
- This class is used to sending mail within the SAP.
- CL_CAM_ADDRESS_BCS :-
- This class is used to create the external recipients.
- This class is used for sending the mail to an outside address of SAP.
- CL_BCS :-
- Out of these four classes first two are most important for Email purpose.
Steps Used for Email Writing Normally :-
- When ever send any mail, we use below steps.
- We pass the receipent address ( where we want to send the mail ).
- We pass the subject
- We write the text for the email purpose (i.e. Description for mail ) .
- We attach the document for the same.
- Then we have to click on send to send the mail.
- This whole process is maintained by the first two classes.
- CL_BCS
- CL_DOCUMENT_BCS
Steps to Send Smartform PDF as an E-Mail Attachment :-
- Get the BIN_FILE of the smartform.
- Convert the BIN_FILE from XSTING format to binary format.
- Create the send request.
- Create the SAP user/external user ( recipient ).
- Add the recipient.
- Create the document
- Add the attachment.
- Set the document
- Activate/deactivate immediate sending.
- Send
- Commit
Solution :-
Step 1 :- Create a executable program in ABAP Editor.
Step 2 :- Call the function module of our smartform in our program and get the OTF data.
-
Step 3 :- Now we have OTF data, so we will convert it to BIN_FILE using CONVERT_OTF function module, It will return me the BIN_FILE.
-
Step 4 :- We have got our BIN_FILE in form of XSTRING format, now we need to convert it into binary format. So, we will use SCMS_XSTRING_TO_BINARY function module for the same purpose.
-
Step 5 :- Now, we have the contents which I want to send a email, So now I will create a send request using CL_BCS class.
- We have a CREATE_PERSISTENT method in CL_BCS class which we will use for create request purpose.
- It will return me the object for CL_BCS class.
-
Step 6 :- Now, we need to create the user where I want to send the email, Suppose I want to send the mail to a SAP user, so we will use CL_SAPUSER_BCS class to create SAP user.
- We will use CREATE method , It will return us the SAP user object
-
Step 7 :- Since, I have got the user, so now, I can add the recipient, so for adding the recipient I will CL_BCS class.
- We have ADD_RECIPIENT method in CL_BCS which we can use to add the recipient.
- In this method we will pass the recipient ( i.e. object of SAP user class which we got above ).
-
Step 8 :- In the above step we have created a SAP user, now Suppose I want to send my document to any external user, then I will have to create a recipient for external user.
-
For the same purpose, we have CL_CAM_ADDRESS_BCS class.
-
-
Step 9 :- So, we have got the external address, then we will add this recipient to CL_BCS class in the same way we added the SAP user to CL_BCS class.
-
Step 10 :- Now, we have added the recipient, now we will create the document for the same purpose.
- We will provide, subject, text and will attach the PDF for the same purpose in the document part.
- We will use CREATE_DOCUMENT method of CL_DOCUMENT_BCS class to create the document.
-
Step 11 :- Now we need to attach the Title for Our Smartform, for the same purpose. We will use ADD_ATTACHMENT method for CL_DOCUMENT_BCS.
-
Also we will attach the binary file our smartform.
-
Step 12 :- Now, we have to set the document before sending it.
- So, we will use the SET_DOCUMENT method of CL_BCS class to set the document.
- We will pass the object of CL_DOCUMENT_BCS class into this method.
-
Step 13 :- Since, I want to send my mail immediately, I will use SET_SEND_IMMEDIATELY method of CL_BCS class and pass it to true.
-
Step 14 :- Now we will send the mail.
- For this purpose, we will use SEND method of CL_BCS class.
- It will return me true or false, whether mail is send or not.
-
Step 15 :- If the SEND returns the true value we, can commit the work.
Code :-
*&---------------------------------------------------------------------*
*& Report ZAR_SMARTFORM_MAIL
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zar_smartform_mail.
DATA : lv_fm_name TYPE rs38l_fnam.
PARAMETERS : p_emp_id TYPE zar_employee_id.
CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
EXPORTING
formname = 'ZAR_SMARTFORM_IMAGE'
* VARIANT = ' '
* DIRECT_CALL = ' '
IMPORTING
fm_name = lv_fm_name
EXCEPTIONS
no_form = 1
no_function_module = 2
OTHERS = 3.
DATA : ls_control TYPE ssfctrlop,
ls_job_output TYPE ssfcrescl.
ls_control-getotf = 'X'.
CALL FUNCTION lv_fm_name
EXPORTING
control_parameters = ls_control
p_emp_id = p_emp_id
IMPORTING
* DOCUMENT_OUTPUT_INFO =
job_output_info = ls_job_output
* JOB_OUTPUT_OPTIONS =
EXCEPTIONS
formatting_error = 1
internal_error = 2
send_error = 3
user_canceled = 4
OTHERS = 5.
DATA : lt_lines TYPE TABLE OF tline.
DATA : lv_bin_file TYPE xstring.
CALL FUNCTION 'CONVERT_OTF'
EXPORTING
format = 'PDF'
* MAX_LINEWIDTH = 132
IMPORTING
* BIN_FILESIZE =
bin_file = lv_bin_file
TABLES
otf = ls_job_output-otfdata
lines = lt_lines
EXCEPTIONS
err_max_linewidth = 1
err_format = 2
err_conv_not_possible = 3
err_bad_otf = 4
OTHERS = 5.
DATA : lt_binary_tab TYPE solix_tab.
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = lv_bin_file
* APPEND_TO_TABLE = ' '
* IMPORTING
* OUTPUT_LENGTH =
TABLES
binary_tab = lt_binary_tab.
DATA : lo_bcs TYPE REF TO cl_bcs.
TRY.
CALL METHOD cl_bcs=>create_persistent
RECEIVING
result = lo_bcs.
CATCH cx_send_req_bcs.
ENDTRY.
DATA : lo_cl_sapuser_bcs TYPE REF TO cl_sapuser_bcs.
TRY.
CALL METHOD cl_sapuser_bcs=>create
EXPORTING
i_user = 'S419AH02'
RECEIVING
result = lo_cl_sapuser_bcs.
CATCH cx_address_bcs.
ENDTRY.
TRY.
CALL METHOD lo_bcs->add_recipient
EXPORTING
i_recipient = lo_cl_sapuser_bcs
* i_express =
* i_copy =
* i_blind_copy =
* i_no_forward =
.
CATCH cx_send_req_bcs.
ENDTRY.
DATA : lo_external_user TYPE REF TO cl_cam_address_bcs.
TRY.
CALL METHOD cl_cam_address_bcs=>create_internet_address
EXPORTING
i_address_string = 'atulsing2505@gmail.com'
* i_address_name =
* i_incl_sapuser =
RECEIVING
result = lo_external_user.
CATCH cx_address_bcs.
ENDTRY.
TRY.
CALL METHOD lo_bcs->add_recipient
EXPORTING
i_recipient = lo_external_user
* i_express =
* i_copy =
* i_blind_copy =
* i_no_forward =
.
CATCH cx_send_req_bcs.
ENDTRY.
DATA : lt_text TYPE TABLE OF soli,
ls_text TYPE soli.
ls_text-line = 'Dear Sir'.
APPEND ls_text TO lt_text.
CLEAR ls_text.
ls_text-line = 'Below is the attached Smartform'.
APPEND ls_text TO lt_text.
CLEAR ls_text.
ls_text-line = 'Thanx and Regards'.
APPEND ls_text TO lt_text.
CLEAR ls_text.
ls_text-line = 'Amrit Raj'.
APPEND ls_text TO lt_text.
CLEAR ls_text.
DATA : lo_document type ref to CL_DOCUMENT_BCS.
TRY.
CALL METHOD cl_document_bcs=>create_document
EXPORTING
i_type = 'RAW'
i_subject = 'Employee Details'
* i_length =
* i_language = SPACE
* i_importance =
* i_sensitivity =
i_text = lt_text
* i_hex =
* i_header =
* i_sender =
* iv_vsi_profile =
RECEIVING
result = lo_document.
CATCH cx_document_bcs.
ENDTRY.
DATA : lo_title type SO_OBJ_DES.
CONCATENATE 'Employee Id' ':' p_emp_id into lo_title.
TRY.
CALL METHOD lo_document->add_attachment
EXPORTING
i_attachment_type = 'PDF'
i_attachment_subject = lo_title
* i_attachment_size =
* i_attachment_language = SPACE
* i_att_content_text =
i_att_content_hex = lt_binary_tab
* i_attachment_header =
* iv_vsi_profile =
.
CATCH cx_document_bcs.
ENDTRY.
TRY.
CALL METHOD lo_bcs->set_document
EXPORTING
i_document = lo_document
.
CATCH cx_send_req_bcs.
ENDTRY.
TRY.
CALL METHOD lo_bcs->set_send_immediately
EXPORTING
i_send_immediately = 'X'
.
CATCH cx_send_req_bcs.
ENDTRY.
DATA : lv_result type OS_BOOLEAN.
TRY.
CALL METHOD lo_bcs->send
* EXPORTING
* i_with_error_screen = SPACE
RECEIVING
result = lv_result
.
CATCH cx_send_req_bcs.
ENDTRY.
If lv_result is NOT INITIAL.
COMMit work.
message 'Mail is sent.' type 'S'.
else.
message 'Message Mail is not sent' type 'E'.
endif.
Execute the Code :-
-
For employee id :- 102
Comments
Post a Comment