07-25-2008 12:23 PM
how send the smart forms and sap scripts through emails .plz help me in this session .
thanks..........
07-25-2008 12:30 PM
07-25-2008 12:39 PM
Hi Swathi,
Code
*&---------------------------------------------------------------------*
*& Report YH1056_SENTEMAILPDF
*&
*&---------------------------------------------------------------------*
*&Convert smartform output in .PDF format and Sent e-mail to destination
*&Address
*&---------------------------------------------------------------------*
REPORT yh1056_sentemailpdf.
DATA: lf_fm_name TYPE rs38l_fnam.
DATA: ls_control_param TYPE ssfctrlop.
DATA: ls_composer_param TYPE ssfcompop.
DATA: ls_recipient TYPE swotobjid.
DATA: ls_sender TYPE swotobjid.
DATA: lf_formname TYPE tdsfname.
DATA: ls_addr_key LIKE addr_key.
DATA: document_output_info TYPE ssfcrespd,
tab_otf_data TYPE ssfcrescl,
job_output_options TYPE ssfcresop.
DATA: file_size TYPE i ,
bin_filesize TYPE i ,
my_tabix TYPE sy-tabix,
w_complines TYPE i .
TYPES:BEGIN OF ty_tab,
vbeln TYPE vbap-vbeln,
posnr TYPE vbap-posnr,
matnr TYPE vbap-matnr,
matkl TYPE matkl,
kwmeng TYPE vbap-kwmeng,
END OF ty_tab.
DATA : pdf_tab LIKE tline OCCURS 0 WITH HEADER LINE ,
pdf_buffered LIKE tline OCCURS 0 WITH HEADER LINE,
itab TYPE TABLE OF ty_tab,
itab_final TYPE itcoo OCCURS 0 WITH HEADER LINE,
t_attach_data TYPE TABLE OF solisti1,
t_objcont TYPE TABLE OF soli.
* wa_attach_data LIKE LINE OF t_Attach_data.
DATA : w_email TYPE string VALUE 'put your email id'.
TABLES: nast, "Messages
*nast, "Messages
tnapr, "Programs & Forms
itcpo, "Communicationarea for Spool
arc_params, "Archive parameters
toa_dara, "Archive parameters
addr_key. "Adressnumber for ADDRESS
DATA price_print_mode(1) TYPE c. "Print-mode
DATA: retcode LIKE sy-subrc. "Returncode
DATA: repeat(1) TYPE c.
DATA: xscreen(1) TYPE c. "Output on printer or screen
DATA: BEGIN OF steu, "Controldata for output
vdkex(1) TYPE c,
vdpex(1) TYPE c,
kbkex(1) TYPE c,
kbpex(1) TYPE c,
END OF steu,
*"Work Areas for Sending Mail..........................................
wa_doc_data TYPE sodocchgi1, " Work Area to hold Attributes of New Document
wa_pack TYPE sopcklsti1, " Work Area for Packing List of Main Document
wa_objhead TYPE solisti1, " Work Area to store Header Text
wa_messbody TYPE solisti1, " Work Area to store Message Body
wa_receivers TYPE somlrec90, " Work Area to store Receiver Information
wa_attach_data TYPE solisti1, " Work Area to store the Attached Document
wa_objcont TYPE soli, " Work Area to store data upto 255 Lines.
t_receivers LIKE TABLE OF wa_receivers,
t_pack LIKE STANDARD TABLE OF wa_pack,
*"Work Variables for Sending Mail......................................
w_tablines TYPE i, " Variable to store Table Lines
w_line TYPE so_text255, " Line of text
w_title LIKE kna1-anred. "#EC NEEDED
PERFORM set_print_param CHANGING ls_control_param
ls_composer_param
ls_recipient
ls_sender
retcode.
*Get the Smart Form name.
* IF NOT tnapr-sform IS INITIAL.
* lf_formname = tnapr-sform.
* ELSE.
* MESSAGE e001(/smb40/ssfcomposer).
* ENDIF.
* determine smartform function module for invoice
CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
EXPORTING
formname = 'YH1056_TEST4'
IMPORTING
fm_name = lf_fm_name
EXCEPTIONS
no_form = 1
no_function_module = 2
OTHERS = 3.
CALL FUNCTION lf_fm_name
EXPORTING
* ARCHIVE_INDEX = ARCHIVE_INDEX
* ARCHIVE_INDEX_TAB = ARCHIVE_INDEX_TAB
* ARCHIVE_PARAMETERS = ARCHIVE_PARAMETERS
control_parameters = ls_control_param
* MAIL_APPL_OBJ = MAIL_APPL_OBJ
* MAIL_RECIPIENT = MAIL_RECIPIENT
* MAIL_SENDER = MAIL_SENDER
output_options = ls_composer_param
user_settings = 'X'
IMPORTING
* DOCUMENT_OUTPUT_INFO = DOCUMENT_OUTPUT_INFO
job_output_info = tab_otf_data
* JOB_OUTPUT_OPTIONS = JOB_OUTPUT_OPTIONS
EXCEPTIONS
formatting_error = 1
internal_error = 2
send_error = 3
user_canceled = 4
.
IF sy-subrc <> 0.
* retcode = sy-subrc.
* PERFORM protocol_update.
*
** get SmartForm protocoll and store it in the NAST protocoll
* PERFORM add_smfrm_prot.
ENDIF.
itab_final[] = tab_otf_data-otfdata[].
DELETE tab_otf_data-otfdata WHERE tdprintcom = '//'.
READ TABLE itab_final WITH KEY tdprintcom = 'EP'.
my_tabix = sy-tabix + 1.
INSERT lines of tab_otf_data-otfdata INTO itab_final INDEX my_tabix.
PERFORM otf_to_pdf.
*PERFORM subj_of_mail.
*PERFORM body_of_mail.
*PERFORM sending_of_mail.
*&---------------------------------------------------------------------*
*& Form set_print_param
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_LS_CONTROL_PARAM text
* <--P_LS_COMPOSER_PARAM text
* <--P_LS_RECIPIENT text
* <--P_LS_SENDER text
* <--P_RETCODE text
*----------------------------------------------------------------------*
FORM set_print_param CHANGING cs_control_param TYPE ssfctrlop
cs_composer_param TYPE ssfcompop
cs_recipient TYPE swotobjid
cs_sender TYPE swotobjid
cf_retcode TYPE sy-subrc.
DATA: ls_itcpo TYPE itcpo.
DATA: lf_repid TYPE sy-repid.
DATA: lf_device TYPE tddevice.
DATA: ls_recipient TYPE swotobjid.
DATA: ls_sender TYPE swotobjid.
* lf_repid = sy-repid.
*
* CALL FUNCTION 'WFMC_PREPARE_SMART_FORM'
* EXPORTING
** pi_nast = nast
* pi_repid = lf_repid
* IMPORTING
** pe_returncode = cf_retcode
** pe_itcpo = ls_itcpo
** pe_device = lf_device
** pe_recipient = cs_recipient
** pe_sender = cs_sender.
IF cf_retcode = 0.
* MOVE-CORRESPONDING ls_itcpo TO cs_composer_param.
cs_composer_param-tddest = 'LP01'. "Note 591576
* cs_control_param-device = ''.
cs_control_param-no_dialog = 'X'.
cs_control_param-preview = space.
cs_control_param-getotf = 'X'.
* cs_control_param-langu = nast-spras.
ENDIF.
ENDFORM. " set_print_param
*&---------------------------------------------------------------------*
*& Form protocol_update
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM protocol_update .
CHECK xscreen = space.
CALL FUNCTION 'NAST_PROTOCOL_UPDATE'
EXPORTING
msg_arbgb = syst-msgid
msg_nr = syst-msgno
msg_ty = syst-msgty
msg_v1 = syst-msgv1
msg_v2 = syst-msgv2
msg_v3 = syst-msgv3
msg_v4 = syst-msgv4
EXCEPTIONS
OTHERS = 1.
ENDFORM. " protocol_update
*&---------------------------------------------------------------------*
*& Form add_smfrm_prot
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM add_smfrm_prot .
DATA: lt_errortab TYPE tsferror.
DATA: lf_msgnr TYPE sy-msgno.
DATA: l_s_log TYPE bal_s_log,
p_loghandle TYPE balloghndl,
l_s_msg TYPE bal_s_msg.
FIELD-SYMBOLS: <fs_errortab> TYPE LINE OF tsferror.
* get smart form protocoll
CALL FUNCTION 'SSF_READ_ERRORS'
IMPORTING
errortab = lt_errortab.
* add smartform protocoll to nast protocoll
LOOP AT lt_errortab ASSIGNING <fs_errortab>.
CLEAR lf_msgnr.
lf_msgnr = <fs_errortab>-errnumber.
CALL FUNCTION 'NAST_PROTOCOL_UPDATE'
EXPORTING
msg_arbgb = <fs_errortab>-msgid
msg_nr = lf_msgnr
msg_ty = <fs_errortab>-msgty
msg_v1 = <fs_errortab>-msgv1
msg_v2 = <fs_errortab>-msgv2
msg_v3 = <fs_errortab>-msgv3
msg_v4 = <fs_errortab>-msgv4
EXCEPTIONS
OTHERS = 1.
ENDLOOP.
* open the application log
l_s_log-extnumber = sy-uname.
CALL FUNCTION 'BAL_LOG_CREATE'
EXPORTING
i_s_log = l_s_log
IMPORTING
e_log_handle = p_loghandle
EXCEPTIONS
OTHERS = 1.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
LOOP AT lt_errortab ASSIGNING <fs_errortab>.
MOVE-CORRESPONDING <fs_errortab> TO l_s_msg.
CALL FUNCTION 'BAL_LOG_MSG_ADD'
EXPORTING
i_log_handle = p_loghandle
i_s_msg = l_s_msg
EXCEPTIONS
OTHERS = 1.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDLOOP.
** Function module to display error logs during
** smart form processing
** Notice , the function 'BAL_DSP_LOG_DISPLAY' can
** not be used when you using output dispatch time
** 4 (Send immediately), so the statement is comment
** out by default.
** You can enable the function call statement
** if your form can not be output and you want to
** see the error log. Set output dispatch time to 3
** before save your order, then print or preview the
** output.
DATA lv_debug.
IF NOT lv_debug IS INITIAL.
CALL FUNCTION 'BAL_DSP_LOG_DISPLAY'.
ENDIF.
ENDFORM. " add_smfrm_prot
*&---------------------------------------------------------------------*
*& Form otf_to_pdf
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM otf_to_pdf .
CALL FUNCTION 'CONVERT_OTF'
EXPORTING
format = 'PDF'
max_linewidth = 132
* ARCHIVE_INDEX = ' '
* COPYNUMBER = 0
* ASCII_BIDI_VIS2LOG = ' '
* PDF_DELETE_OTFTAB = ' '
IMPORTING
bin_filesize = bin_filesize
* * BIN_FILE = BIN_FILE
TABLES
otf = itab_final
lines = pdf_tab
* EXCEPTIONS
* ERR_MAX_LINEWIDTH = 1
* ERR_FORMAT = 2
* ERR_CONV_NOT_POSSIBLE = 3
* ERR_BAD_OTF = 4
.
*
* LOOP AT pdf_tab.
* wa_attach_data = pdf_tab-tdline.
* APPEND wa_attach_data TO t_attach_data.
* ENDLOOP.
*
PERFORM conversion_of_size.
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
bin_filesize = bin_filesize
filename = 'D:\extra\pdftest.pdf'
filetype = 'BIN'
* APPEND = ' '
* WRITE_FIELD_SEPARATOR = ' '
* HEADER = '00'
* TRUNC_TRAILING_BLANKS = ' '
* WRITE_LF = 'X'
* COL_SELECT = ' '
* COL_SELECT_MASK = ' '
* DAT_MODE = ' '
* CONFIRM_OVERWRITE = ' '
* NO_AUTH_CHECK = ' '
* CODEPAGE = ' '
* IGNORE_CERR = ABAP_TRUE
* REPLACEMENT = '#'
* WRITE_BOM = ' '
* TRUNC_TRAILING_BLANKS_EOL = 'X'
* WK1_N_FORMAT = ' '
* WK1_N_SIZE = ' '
* WK1_T_FORMAT = ' '
* WK1_T_SIZE = ' '
IMPORTING
filelength = file_size
TABLES
data_tab = pdf_tab
* FIELDNAMES = FIELDNAMES
EXCEPTIONS
file_write_error = 1
no_batch = 2
gui_refuse_filetransfer = 3
invalid_type = 4
no_authority = 5
unknown_error = 6
header_not_allowed = 7
separator_not_allowed = 8
filesize_not_allowed = 9
header_too_long = 10
dp_error_create = 11
dp_error_send = 12
dp_error_write = 13
unknown_dp_error = 14
access_denied = 15
dp_out_of_memory = 16
disk_full = 17
dp_timeout = 18
file_not_found = 19
dataprovider_exception = 20
control_flush_error = 21.
*"Setting up Document Data Attribute...................................
wa_doc_data-obj_langu = sy-langu.
wa_doc_data-obj_name = 'Attachment'.
wa_doc_data-obj_descr = 'This is a Mail sent through SAP Report'.
wa_doc_data-sensitivty = 'F'.
wa_doc_data-doc_size = w_tablines.
t_attach_data = t_objcont.
DESCRIBE TABLE t_attach_data LINES w_tablines.
*"Setting up Packing List Attributes...................................
wa_pack-transf_bin = 'X'.
wa_pack-head_start = 0.
wa_pack-head_num = 0.
wa_pack-body_start = 0.
wa_pack-body_num = w_tablines.
wa_pack-doc_size = w_tablines * 255.
wa_pack-doc_type = 'PDF'.
wa_pack-obj_name = 'Attachment'.
wa_pack-obj_descr = 'This is a SAP Report'.
APPEND wa_pack TO t_pack.
*"Setting up Receiver Attributes.......................................
wa_receivers-receiver = w_email.
wa_receivers-rec_type = 'U'.
APPEND wa_receivers TO t_receivers.
CALL FUNCTION 'SO_DOCUMENT_SEND_API1'
EXPORTING
document_data = wa_doc_data
put_in_outbox = 'X'
* SENDER_ADDRESS = SY-UNAME
* SENDER_ADDRESS_TYPE = 'B'
commit_work = 'X'
* IMPORTING
* SENT_TO_ALL = SENT_TO_ALL
* NEW_OBJECT_ID = NEW_OBJECT_ID
* SENDER_ID = SENDER_ID
TABLES
packing_list = t_pack
* OBJECT_HEADER = OBJECT_HEADER
contents_bin = t_attach_data
* CONTENTS_TXT = CONTENTS_TXT
* CONTENTS_HEX = CONTENTS_HEX
* OBJECT_PARA = OBJECT_PARA
* OBJECT_PARB = OBJECT_PARB
receivers = t_receivers
EXCEPTIONS
too_many_receivers = 1
document_not_sent = 2
document_type_not_exist = 3
operation_no_authorization = 4
parameter_error = 5
x_error = 6
enqueue_error = 7
.
IF sy-subrc EQ 0.
WRITE: 'Mail Sent to '.
ELSE.
WRITE: 'Message Sending Failed'.
ENDIF. " IF SY-SUBRC EQ 0.
ENDFORM. " otf_to_pdf.
*&---------------------------------------------------------------------*
*& Form conversion_of_size
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM conversion_of_size .
*"Declaring Local Constants............................................
CONSTANTS:
cnv_hexconst_zero TYPE x VALUE '00'.
*"--------------------------------------------------------------------*
* Internal table to hold 255 Char's Single Line. *
*"--------------------------------------------------------------------*
DATA:
lv_big_lines(268) TYPE c
OCCURS 0 WITH HEADER LINE.
*"Local Work Variables.................................................
DATA:
lfl_flag TYPE c,
lv_left_t(268) TYPE c,
lv_left_i TYPE i,
tv_left_i TYPE i,
lv_curr_i TYPE i.
FIELD-SYMBOLS: <f>.
* Get the lines into a table of 268 char as the first step to put it in
* the pdf file of 255 chars
CLEAR lfl_flag.
LOOP AT pdf_tab INTO pdf_tab.
IF lfl_flag EQ ' '.
CLEAR lv_big_lines .
ASSIGN lv_big_lines(134) TO <f>.
<f> = pdf_tab.
lfl_flag = 'X'.
ELSE.
lv_big_lines+134 = pdf_tab.
APPEND lv_big_lines.
CLEAR: lfl_flag.
ENDIF. " If lfl_flag = ''..
ENDLOOP. " Loop at t_pdf
IF lfl_flag EQ 'X'.
APPEND lv_big_lines.
ENDIF. " If lflf_flag eq 'X'..
* Next fill it into a 255 char table
CLEAR: lv_left_t, lv_left_i, tv_left_i.
lv_curr_i = 255.
LOOP AT lv_big_lines.
IF lv_left_i NE 0.
IF lv_curr_i NE 0.
wa_objcont(lv_left_i) = lv_left_t(lv_left_i).
wa_objcont+lv_left_i(lv_curr_i) = lv_big_lines(lv_curr_i).
ELSE.
wa_objcont = lv_left_t(lv_left_i).
ENDIF. " IF lv_curr_i NE 0
ELSE.
wa_objcont = lv_big_lines(lv_curr_i).
ENDIF. " IF lv_left_i NE 0
APPEND wa_objcont TO t_objcont.
tv_left_i = 268 - lv_curr_i.
IF tv_left_i > 255.
wa_objcont = lv_big_lines+lv_curr_i(255).
APPEND wa_objcont TO t_objcont.
lv_left_i = tv_left_i - 255.
tv_left_i = 255 + lv_curr_i.
lv_curr_i = 255 - lv_left_i.
lv_left_t = lv_big_lines+tv_left_i.
ELSE.
lv_left_t = lv_big_lines+lv_curr_i.
lv_left_i = 268 - lv_curr_i.
lv_curr_i = 255 - lv_left_i.
ENDIF. " IF tv_left_i > 255
ENDLOOP. " LOOP AT lv_big_lines.
CLEAR wa_objcont .
ASSIGN wa_objcont(lv_left_i) TO <f>.
<f> = lv_left_t(lv_left_i).
APPEND wa_objcont TO t_objcont.
ENDFORM. " conversion_of_size
Regards,
Sandeep