The below include program is used to send email with CC and attachment functionality:
*&---------------------------------------------------------------------*
*& Include
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
* Purpose:
* Provide generic subroutines for any ABAP programs with intention of
* sending email (SAPOffice or Internet) with attachments
*&---------------------------------------------------------------------*
DATA: objpack LIKE sopcklsti1 OCCURS 2 WITH HEADER LINE. "Packing list
DATA: objhead LIKE solisti1 OCCURS 1 WITH HEADER LINE. "Object header
DATA: objtxt LIKE solisti1 OCCURS 10 WITH HEADER LINE. "Email body message
DATA: objbin LIKE solisti1 OCCURS 10 WITH HEADER LINE. "Binary attachments (PDF)
DATA: objhex LIKE solix OCCURS 10 WITH HEADER LINE. "Hex attachments (XLS)
DATA: reclist LIKE somlreci1 OCCURS 5 WITH HEADER LINE. "Recipient list
DATA: doc_chng LIKE sodocchgi1. "Email information
DATA: tab_lines LIKE sy-tabix. "Number of records of the content table being filled
DATA: pk_lines LIKE sy-tabix. "Number of lines of the attachment packing list
*&---------------------------------------------------------------------*
*& Form initialize_email
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM initialize_email.
REFRESH: objpack,
objhead,
objtxt,
objbin,
objhex,
reclist.
CLEAR: doc_chng, pk_lines.
ENDFORM. "initialize_email
*&---------------------------------------------------------------------*
*& Form build_email_body
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->L_SUBJ text
*----------------------------------------------------------------------*
FORM build_email_body TABLES it_body STRUCTURE solisti1
USING l_subj.
* Setup the email body message
IF NOT it_body[] IS INITIAL.
REFRESH objtxt.
objtxt[] = it_body[].
ELSE.
objtxt = l_subj.
APPEND objtxt.
ENDIF.
* Subject of email
MOVE l_subj TO doc_chng-obj_descr.
DESCRIBE TABLE objtxt LINES tab_lines.
READ TABLE objtxt INDEX tab_lines.
doc_chng-doc_size = ( tab_lines - 1 ) * 255 + STRLEN( objtxt ).
* Prepare the packing list for email body
* DESCRIBE TABLE objpack LINES pk_lines.
CLEAR objpack-transf_bin.
objpack-head_start = 1.
objpack-head_num = 0.
objpack-body_start = 1.
objpack-body_num = tab_lines.
objpack-doc_type = 'RAW'.
APPEND objpack.
ENDFORM. "build_email_body
*&---------------------------------------------------------------------*
*& Form build_hex_attachment
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM build_hex_attachment TABLES it_hex
USING l_filename LIKE sopcklsti1-obj_descr
l_filetype LIKE sopcklsti1-doc_type.
DATA: it_objhex LIKE solix OCCURS 0 WITH HEADER LINE.
IF NOT it_hex[] IS INITIAL.
CALL FUNCTION 'SCMS_TEXT_TO_BINARY'
* EXPORTING
* FIRST_LINE = 0
* LAST_LINE = 0
* APPEND_TO_TABLE = ' '
* MIMETYPE = ' '
* ENCODING =
* IMPORTING
* OUTPUT_LENGTH =
TABLES
text_tab = it_hex
binary_tab = it_objhex
EXCEPTIONS
failed = 1
OTHERS = 2
.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ELSE.
APPEND LINES OF it_objhex TO objhex.
* IT_OBJHEX is used as OBJHEX can contain multiple attachments
DESCRIBE TABLE it_objhex LINES tab_lines.
objpack-transf_bin = 'X'.
objpack-head_start = 1.
objpack-head_num = 0.
objpack-body_start = pk_lines + 1.
objpack-body_num = tab_lines.
objpack-doc_type = l_filetype.
* objpack-obj_name = 'ATTACHMENT'.
objpack-obj_descr = l_filename.
objpack-doc_size = tab_lines * 255.
APPEND objpack.
pk_lines = objpack-body_start + objpack-body_num - 1.
ENDIF.
ENDIF.
ENDFORM. "build_hex_attachment
*&---------------------------------------------------------------------*
*& Form build_recip_list
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM build_recip_list USING l_recipient
l_reciptype LIKE somlreci1-rec_type.
MOVE l_recipient TO reclist-receiver.
reclist-rec_type = l_reciptype.
reclist-copy = ''.
APPEND reclist.
ENDFORM. "build_recip_list
*&---------------------------------------------------------------------*
*& Form build_recip_cc_list
*&---------------------------------------------------------------------*
FORM build_recip_cc_list USING l_recipient
l_reciptype LIKE somlreci1-rec_type.
MOVE l_recipient TO reclist-receiver.
reclist-rec_type = l_reciptype.
reclist-copy = 'X'.
APPEND reclist.
ENDFORM. "build_recip_cc_list
*&---------------------------------------------------------------------*
*& Form send_email
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM send_email.
IF reclist[] IS INITIAL.
PERFORM build_recip_list USING sy-uname
'B'.
ENDIF.
CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
EXPORTING
document_data = doc_chng
* PUT_IN_OUTBOX = ' '
commit_work = 'X'
* IMPORTING
* SENT_TO_ALL =
* NEW_OBJECT_ID =
TABLES
packing_list = objpack
object_header = objhead
*{ REPLACE DEVK935771 2
*\* CONTENTS_BIN =
CONTENTS_BIN = objbin
*} REPLACE
contents_txt = objtxt
contents_hex = objhex
* OBJECT_PARA =
* OBJECT_PARB =
receivers = reclist
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
OTHERS = 8
.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
*{ DELETE DEVK935721 1
*\ ELSE.
*\ SUBMIT rsconn01 AND RETURN.
*} DELETE
ENDIF.
ENDFORM. "send_email
*&---------------------------------------------------------------------*
*& Form send_email_w_from_addr
*&---------------------------------------------------------------------*
* T01K918835
*----------------------------------------------------------------------*
FORM send_email_w_from_addr USING l_from.
TYPES:
BEGIN OF ty_s_imp00,
imp01 TYPE c, "Sent To All
imp02 TYPE sofolenti1-object_id, "New Object ID
imp03 TYPE soudk, "Sender ID
END OF ty_s_imp00.
DATA:
lw_imp00 TYPE ty_s_imp00,
lt_sosct TYPE sosctab,
lt_obcat TYPE TABLE OF sxobjcat.
IF reclist[] IS INITIAL.
PERFORM build_recip_list USING sy-uname
'B'.
ENDIF.
CALL FUNCTION 'SO_DOCUMENT_SEND_API1'
EXPORTING
document_data = doc_chng
put_in_outbox = 'X'
sender_address = l_from
sender_address_type = 'SMTP'
commit_work = 'X'
IMPORTING
sent_to_all = lw_imp00-imp01
new_object_id = lw_imp00-imp02
sender_id = lw_imp00-imp03
TABLES
packing_list = objpack
* contents_bin =
contents_hex = objhex
contents_txt = objtxt
receivers = reclist
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
OTHERS = 8.
.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ELSE.
* Force the email sending immediately.
CLEAR: lt_sosct, lt_obcat.
COMMIT WORK AND WAIT.
SELECT * FROM sosc
INTO TABLE lt_sosct
WHERE objtp = lw_imp00-imp02+0(3) AND
objyr = lw_imp00-imp02+3(2) AND
objno = lw_imp00-imp02+5.
IF sy-subrc = 0.
CALL FUNCTION 'SX_SEND_DISPATCHER'
EXPORTING
sosc_tab = lt_sosct
TABLES
obj_cat = lt_obcat
EXCEPTIONS
internal_error = 1
directsend_failed = 2
OTHERS = 3.
IF sy-subrc <> 0.
MESSAGE s005(z1) WITH space 'Email' 'Sent'.
ELSE.
MESSAGE s004(z1) WITH space 'Email' 'Sent'.
ENDIF.
ELSE.
MESSAGE s005(z1) WITH space 'Email' 'Sent'.
ENDIF.
ENDIF.
ENDFORM. "send_email
*{ INSERT DEVK935771 1
FORM build_pdf_attachment TABLES it_pdf
USING l_filename LIKE sopcklsti1-obj_descr
l_filetype LIKE sopcklsti1-doc_type.
DATA: it_objbin LIKE solisti1 OCCURS 0 WITH HEADER LINE.
if it_pdf[] is not initial.
CALL FUNCTION 'SX_TABLE_LINE_WIDTH_CHANGE'
* EXPORTING
* LINE_WIDTH_SRC =
* LINE_WIDTH_DST =
* TRANSFER_BIN = ' '
TABLES
content_in = it_pdf
content_out = it_objbin
EXCEPTIONS
ERR_LINE_WIDTH_SRC_TOO_LONG = 1
ERR_LINE_WIDTH_DST_TOO_LONG = 2
ERR_CONV_FAILED = 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.
else.
APPEND LINES OF it_objbin TO objbin.
* IT_OBJBIN is used as OBJBIN can contain multiple attachments
DESCRIBE TABLE it_objbin LINES tab_lines.
objpack-transf_bin = 'X'.
objpack-head_start = 1.
objpack-head_num = 0.
objpack-body_start = pk_lines + 1.
objpack-body_num = tab_lines.
objpack-doc_type = l_filetype.
* objpack-obj_name = 'ATTACHMENT'.
objpack-obj_descr = l_filename.
objpack-doc_size = tab_lines * 255.
APPEND objpack.
pk_lines = objpack-body_start + objpack-body_num - 1.
ENDIF.
ENDIF.
ENDFORM.
*} INSERT
No comments:
Post a Comment