FUNCTION z_send_mail_with_attach.
*"----------------------------------------------------------------------
*"*"Локальный интерфейс:
*" IMPORTING
*" VALUE(SAVE_IN_SAPOFFICE) TYPE CHAR1 DEFAULT SPACE
*" TABLES
*" DOC_HEADER STRUCTURE SOLISTI1 OPTIONAL
*" DOC_CONTENT STRUCTURE SOLISTI1 OPTIONAL
*" RECEIVERS STRUCTURE SOMLRECI1
*" ATTACHMENTS STRUCTURE SOLISTI1 OPTIONAL
*" EXCEPTIONS
*" NO_RECEIVERS
*" NO_AUTHORITY
*"----------------------------------------------------------------------
CONSTANTS: so_format(11) TYPE c VALUE '&SO_FORMAT='.
DATA: document LIKE sofolenti1,
user LIKE soudnamei1,
user_data LIKE soudatai1,
docdata LIKE sodocchgi1,
hd_dat LIKE sood1,
doc_id LIKE soodk,
l_receivers LIKE somlreci1 OCCURS 0 WITH HEADER LINE,
l_objhead LIKE soli OCCURS 0 WITH HEADER LINE,
l_doc_header LIKE solisti1 OCCURS 1 WITH HEADER LINE,
l_doc_content LIKE solisti1 OCCURS 1 WITH HEADER LINE.
DATA: path_and_file LIKE rlgrap-filename,
file_format LIKE rlgrap-filetype,
bin_filesize LIKE soxwd-doc_length,
object_type LIKE soodk-objtp VALUE 'EXT', " см.табл. TSOTD
l_objcont LIKE soli OCCURS 0,
l_filename LIKE rlgrap-filename,
l_nodialog LIKE sonv-flag VALUE 'X',
l_cancelled LIKE sonv-flag,
put_to_kpro LIKE sonv-flag,
lin TYPE i.
* структуры для задания и срока запуска/период/повторения ФонЗадания
DATA: job LIKE tbtco,
starttime LIKE tbtcstrt,
status LIKE tbtco-status,
released(1).
*
*
AUTHORITY-CHECK OBJECT 'S_OC_SEND'
ID 'COM_MODE' FIELD 'INT' " INT - SMTP
ID 'NUMBER' DUMMY.
IF sy-subrc NE 0.
MESSAGE e094(zr) RAISING no_authority.
ENDIF.
* получатель (получатели)
DESCRIBE TABLE receivers LINES lin.
IF lin < 1.
MESSAGE e093(zr) RAISING no_receivers.
ENDIF.
LOOP AT receivers.
l_receivers-receiver = receivers-receiver.
l_receivers-rec_type = receivers-rec_type.
l_receivers-express = 'X'.
APPEND l_receivers.
ENDLOOP.
* пользователь, от имени которого посылается письмо
user-sapname = sy-uname.
* Заголовок(тема) письма
LOOP AT doc_header.
l_doc_header = doc_header.
APPEND l_doc_header.
ENDLOOP.
* Содержание письма
LOOP AT doc_content.
l_doc_content = doc_content.
APPEND l_doc_content.
ENDLOOP.
* Атрибуты документа SAP_Office
CLEAR docdata.
docdata-obj_name = 'Z_SEND_MAIL_WITH_ATTACH'.
READ TABLE l_doc_header INDEX 1.
IF sy-subrc = 0.
docdata-obj_descr = l_doc_header-line.
ELSE.
docdata-obj_descr = 'без заголовка'.
ENDIF.
docdata-obj_langu = sy-langu.
docdata-sensitivty = 'P'.
*
*
* path_and_file = 'd:\aba.xls'.
* читаем данные пользователя (в основном нам нужна его SAP-папка (ID))
CALL FUNCTION 'SO_USER_READ_API1'
EXPORTING
user = user
prepare_for_folder_access = 'X'
IMPORTING
user_data = user_data
EXCEPTIONS
user_not_exist = 1
parameter_error = 2
x_error = 3
OTHERS = 4.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
* создаем SAP_Office-документ
CALL FUNCTION 'SO_DOCUMENT_INSERT_API1'
EXPORTING
folder_id = user_data-outboxfol
document_data = docdata
document_type = 'RAW'
IMPORTING
document_info = document
TABLES
object_header = l_doc_header
object_content = l_doc_content
EXCEPTIONS
folder_not_exist = 1
document_type_not_exist = 2
operation_no_authorization = 3
parameter_error = 4
x_error = 5
enqueue_error = 6
OTHERS = 7.
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 attachments INTO path_and_file.
CLEAR: hd_dat, l_objcont[].
* загрузка объекта для аттачмента
CALL FUNCTION 'SO_OBJECT_UPLOAD'
EXPORTING
path_and_file = path_and_file
no_dialog = l_nodialog
IMPORTING
f_cancelled = l_cancelled
filelength = bin_filesize
act_filetype = file_format
act_filename = path_and_file
act_objtype = object_type
file_put_to_kpro = put_to_kpro
TABLES
objcont = l_objcont
EXCEPTIONS
invalid_type = 1
object_type_not_allowed = 2
kpro_insert_error = 3
OTHERS = 4.
CASE sy-subrc.
WHEN 0.
IF NOT l_cancelled IS INITIAL.
EXIT.
ENDIF.
hd_dat-objlen = bin_filesize.
IF NOT put_to_kpro IS INITIAL.
hd_dat-extct = 'K'. "reference_type_kpro.
ENDIF.
CALL FUNCTION 'SO_SPLIT_FILE_AND_PATH'
EXPORTING
full_name = path_and_file
IMPORTING
stripped_name = l_filename.
CLEAR: l_objhead, l_objhead[].
INSERT l_filename INTO l_objhead INDEX 1.
l_objhead-line = so_format.
l_objhead-line+11(3) = file_format.
APPEND l_objhead.
hd_dat-file_ext = object_type.
hd_dat-objnam = text-001.
IF hd_dat-objdes IS INITIAL.
SPLIT l_filename AT '.' INTO hd_dat-objdes l_filename.
ENDIF.
MOVE: document-obj_type TO doc_id-objtp,
document-object_id+3(2) TO doc_id-objyr,
document-object_id+5(12) TO doc_id-objno.
WHEN OTHERS.
EXIT.
ENDCASE.
* присоединение аттачмента к созданному документу
CALL FUNCTION 'SO_ATTACHMENT_INSERT'
EXPORTING
object_id = doc_id
attach_type = 'EXT'
object_hd_change = hd_dat
owner = user-sapname
TABLES
objcont = l_objcont
objhead = l_objhead
EXCEPTIONS
active_user_not_exist = 35
communication_failure = 71
object_type_not_exist = 17
operation_no_authorization = 21
owner_not_exist = 22
parameter_error = 23
substitute_not_active = 31
substitute_not_defined = 32
system_failure = 72
x_error = 1000.
ENDLOOP.
* помещаем документ в исходящую очередь на отсылку
CALL FUNCTION 'SO_OLD_DOCUMENT_SEND_API1'
EXPORTING
document_id = document-doc_id
put_in_outbox = space
TABLES
receivers = l_receivers
EXCEPTIONS
too_many_receiver = 1
document_not_sent = 2
document_not_exist = 3
operation_no_authorization = 4
parameter_error = 5
x_error = 6
enqueue_error = 7
OTHERS = 8.
IF sy-subrc <> 0.
MESSAGE s001(zr) WITH 'Error ' sy-subrc ' on send mail'.
EXIT.
ENDIF.
*
job-jobname = 'Z_MAIL_SEND'.
CALL FUNCTION 'JOB_OPEN'
EXPORTING
delanfrep =
'X' " Delete Job After Successful Execution
jobname = job-jobname " Job name
IMPORTING
jobcount =
job-jobcount " ID number of background job !
EXCEPTIONS
cant_create_job = 01
invalid_job_data = 02
jobname_missing = 03.
IF sy-subrc <> 0.
CASE sy-subrc.
WHEN 1.MESSAGE i001(38) WITH 'Job cannot be created, see SYSLOG'.
WHEN 2.MESSAGE i001(38) WITH 'Job cont.invalid job data,см.SYSLOG'.
WHEN 3.MESSAGE i001(38) WITH 'Job name not specified'.
ENDCASE.
EXIT.
ENDIF.
*
SUBMIT rsconn01 AND RETURN
WITH mode = 'INT'
VIA JOB job-jobname NUMBER job-jobcount.
*====== pass a background job to the background processing ===========
starttime-startdttyp = 'D'.
starttime-sdlstrtdt = sy-datum.
starttime-sdlstrttm = sy-uzeit + 3.
*
CALL FUNCTION 'JOB_CLOSE'
EXPORTING
jobcount = job-jobcount
jobname = job-jobname
sdlstrtdt = starttime-sdlstrtdt
sdlstrttm = starttime-sdlstrttm
IMPORTING
job_was_released = released
EXCEPTIONS
cant_start_immediate = 1
invalid_startdate = 2.
IF sy-subrc NE 0.
CALL FUNCTION 'JOB_CLOSE'
EXPORTING
jobcount = job-jobcount
jobname = job-jobname
strtimmed = 'X'
IMPORTING
job_was_released = released
EXCEPTIONS
cant_start_immediate = 1
invalid_startdate = 2.
IF released NE 'X'.
CALL FUNCTION 'BP_JOB_DELETE'
EXPORTING
forcedmode = 'X'
jobname = job-jobname
jobcount = job-jobcount
EXCEPTIONS
OTHERS = 99.
SUBMIT rsconn01 WITH mode = 'INT' AND RETURN.
ENDIF.
ENDIF.
* удалить отправленный документ из SAP-Office ?
CHECK save_in_sapoffice = space.
CALL FUNCTION 'SO_DOCUMENT_DELETE_API1'
EXPORTING
document_id = document-doc_id
unread_delete = 'X'
* PUT_IN_TRASH = 'X'
EXCEPTIONS
document_not_exist = 1
operation_no_authorization = 2
parameter_error = 3
x_error = 4
enqueue_error = 5
OTHERS = 6
.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDFUNCTION.
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG. SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG. Every effort is made to ensure content integrity. Use information on this site at your own risk.