FUNCTION zcrm_send_email.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" VALUE(IV_SUBJECT) TYPE SO_OBJ_DES OPTIONAL
*" VALUE(IV_SENDER_EMAL) TYPE ADR6-SMTP_ADDR OPTIONAL
*" VALUE(IV_SENDER_NAME) TYPE ADR6-SMTP_ADDR OPTIONAL
*" VALUE(IV_COMMON_BODY1) TYPE FLAG DEFAULT ABAP_TRUE
*" VALUE(IV_COMMON_BODY2) TYPE FLAG DEFAULT ABAP_TRUE
*" VALUE(IV_EXCEL_AS_BODY) TYPE FLAG OPTIONAL
*" VALUE(IV_EXCEL) TYPE FLAG OPTIONAL
*" VALUE(IV_PDF) TYPE FLAG OPTIONAL
*" VALUE(IT_TABLE) TYPE ANY TABLE OPTIONAL
*" TABLES
*" IT_BODYS STRUCTURE SOLISTI1 OPTIONAL
*" IT_RECIPIENTS_EMAIL STRUCTURE BAPIADSMTP OPTIONAL
*" IT_CC_RECIPIENTS_EMAIL STRUCTURE BAPIADSMTP OPTIONAL
*"----------------------------------------------------------------------
FIELD-SYMBOLS:
<fs_comp> TYPE abap_compdescr,
<fs_value> TYPE any,
<fs_line> TYPE any,
<fs_col> TYPE any.
DATA:
gs_bapiadsmtp TYPE bapiadsmtp,
gr_cx_send_req_bcs TYPE REF TO cx_send_req_bcs,
gv_os_boolean TYPE os_boolean,
gr_send_request TYPE REF TO cl_bcs,
gr_document TYPE REF TO cl_document_bcs,
gr_recipient TYPE REF TO if_recipient_bcs,
gr_bcs_exception TYPE REF TO cx_bcs,
gr_sender TYPE REF TO if_sender_bcs.
TRY.
* create persistent send request ------------------------
FREE gr_send_request.
gr_send_request = cl_bcs=>create_persistent( ).
* Start program frm_set_mail_document-Begin
REFRESH:gt_solisti1.
*IV_COMMON_BODY1-Begin
IF iv_common_body1 = abap_true.
gs_solisti1 = '<html><body>'.
APPEND gs_solisti1 TO gt_solisti1.
ENDIF.
*IV_COMMON_BODY1-End
*IT_BODYS
IF it_bodys[] IS NOT INITIAL.
APPEND LINES OF it_bodys TO gt_solisti1.
ENDIF.
*IT_TABLE 如果非空的话获取共用抬头数据
REFRESH:gt_ddfields.
IF it_table IS NOT INITIAL.
LOOP AT it_table ASSIGNING <gs_line> .
IF gt_ddfields[] IS INITIAL.
gr_cl_abap_structdescr ?= cl_abap_typedescr=>describe_by_data( <gs_line> ).
*表头
IF gt_ddfields[] IS INITIAL.
CALL METHOD gr_cl_abap_structdescr->get_ddic_field_list
EXPORTING
p_langu = sy-langu
RECEIVING
p_field_list = gt_ddfields
EXCEPTIONS
not_found =
no_ddic_type =
OTHERS = .
IF sy-subrc <> .
* Implement suitable error handling here
ENDIF.
ENDIF.
ENDIF.
ENDLOOP.
ENDIF.
*IV_EXCEL_AS_BODY
IF iv_excel_as_body = abap_true.
IF it_table IS NOT INITIAL.
gs_solisti1 = '<table border="1">'.
APPEND gs_solisti1 TO gt_solisti1.
gs_solisti1 = '<tr>'.
APPEND gs_solisti1 TO gt_solisti1.
* Excel抬头
CLEAR:gs_solisti1.
LOOP AT gt_ddfields INTO gs_ddfields.
CLEAR:gs_solisti1.
* IF gs_solisti1 IS INITIAL.
* CONCATENATE '<td>' gs_ddfields-fieldtext '</td>' INTO gs_solisti1.
* ELSE.
* CONCATENATE gs_solisti1 '<td>' gs_ddfields-fieldtext '</td>' INTO gs_solisti1.
* ENDIF.
CONCATENATE '<td>' gs_ddfields-fieldtext '</td>' INTO gs_solisti1.
APPEND gs_solisti1 TO gt_solisti1.
ENDLOOP.
* Excel 数据
LOOP AT it_table ASSIGNING <gs_line>.
*表内容
gs_solisti1 = '<tr>'.
APPEND gs_solisti1 TO gt_solisti1.
LOOP AT gr_cl_abap_structdescr->components ASSIGNING <fs_comp>.
CLEAR:gs_solisti1.
ASSIGN <fs_comp>-name TO <fs_col>.
ASSIGN COMPONENT <fs_col> OF STRUCTURE <gs_line> TO <gs_field>.
WRITE <gs_field> TO gv_field.
CONCATENATE '<td >' gv_field '</td> ' INTO gs_solisti1.
APPEND gs_solisti1 TO gt_solisti1.
ENDLOOP.
gs_solisti1 = '</tr>'.
APPEND gs_solisti1 TO gt_solisti1.
ENDLOOP.
gs_solisti1 = '</table>'.
APPEND gs_solisti1 TO gt_solisti1.
ENDIF.
ENDIF.
*IV_COMMON_BODY2-Begin
IF iv_common_body2 = abap_true.
gs_solisti1 = '<br>'.
APPEND gs_solisti1 TO gt_solisti1.
gs_solisti1 = '<br>'.
APPEND gs_solisti1 TO gt_solisti1.
gs_solisti1 = 'Hisense Service'.
APPEND gs_solisti1 TO gt_solisti1.
gs_solisti1 = '<br>'.
APPEND gs_solisti1 TO gt_solisti1.
gs_solisti1 = '0860 447 3673'.
APPEND gs_solisti1 TO gt_solisti1.
gs_solisti1 = '<br>'.
APPEND gs_solisti1 TO gt_solisti1.
gs_solisti1 = 'service@hisense.co.za'.
APPEND gs_solisti1 TO gt_solisti1.
ENDIF.
*IV_COMMON_BODY2-End
IF iv_common_body1 = abap_true.
gs_solisti1 = '<br>'.
APPEND gs_solisti1 TO gt_solisti1.
gs_solisti1 = '</body></html>'.
APPEND gs_solisti1 TO gt_solisti1.
ENDIF.
gr_document = cl_document_bcs=>create_document(
i_type = 'HTM'
i_text = gt_solisti1
i_subject = iv_subject ).
* Start program frm_set_mail_document-End
* Define Attachment frm_set_mail_attachment-Begin
IF iv_excel = abap_true.
****EXCEL附加抬头
CLEAR:gv_string,gv_lines,gv_number.
DESCRIBE TABLE gt_ddfields LINES gv_lines.
LOOP AT gt_ddfields INTO gs_ddfields.
gv_number = gv_number + .
IF gv_lines = gv_number.
CONCATENATE gv_string gs_ddfields-fieldtext gc_crlf INTO gv_string.
ELSE.
IF gv_string IS INITIAL.
CONCATENATE gs_ddfields-fieldtext gc_tab INTO gv_string.
ELSE.
CONCATENATE gv_string gs_ddfields-fieldtext gc_tab INTO gv_string.
ENDIF.
ENDIF.
ENDLOOP.
****EXCEL附加内容
LOOP AT it_table ASSIGNING <gs_line>.
gr_cl_abap_structdescr ?= cl_abap_typedescr=>describe_by_data( <gs_line> ).
CLEAR:gv_number.
LOOP AT gr_cl_abap_structdescr->components ASSIGNING <fs_comp>.
gv_number = gv_number + .
ASSIGN <fs_comp>-name TO <fs_col>.
ASSIGN COMPONENT <fs_col> OF STRUCTURE <gs_line> TO <gs_field>.
WRITE <gs_field> TO gv_field.
IF gv_lines = gv_number.
CONCATENATE gv_string gv_field gc_crlf INTO gv_string.
ELSE.
CONCATENATE gv_string gv_field gc_tab INTO gv_string.
ENDIF.
ENDLOOP.
ENDLOOP.
* Convert string to xstring type
* 'APPLICATION/MSEXCEL;charset=utf-16le'
CLEAR:gv_xstring.
CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
EXPORTING
text = gv_string
mimetype = gc_mimetype
IMPORTING
buffer = gv_xstring
EXCEPTIONS
failed =
OTHERS = .
* Add the file header for utf-16le. .
IF sy-subrc = .
CONCATENATE cl_abap_char_utilities=>byte_order_mark_little
gv_xstring INTO gv_xstring IN BYTE MODE.
ENDIF.
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = gv_xstring
TABLES
binary_tab = gt_solix.
*Create attachment notification
gr_document->add_attachment(
i_attachment_type = 'XLS'""XXL XLS
i_attachment_subject = iv_subject
i_att_content_hex = gt_solix ).
ENDIF.
*add document object to send request
TRY.
gr_send_request->set_document( gr_document ).
CATCH cx_send_req_bcs INTO gr_cx_send_req_bcs.
* MESSAGE i605(sbcoms) .
* pv_subrc = 4.
ENDTRY.
* Define Attachment frm_set_mail_attachment-End
* Sender addess frm_set_sender-Begin
IF iv_sender_emal IS INITIAL.
iv_sender_emal = iv_sender_name = 'service@hisense.co.za'.
ENDIF.
CALL METHOD cl_cam_address_bcs=>create_internet_address
EXPORTING
i_address_string = iv_sender_emal "'europe@hisense.com'
i_address_name = iv_sender_name "'europe@hisense.com'
RECEIVING
result = gr_sender.
CALL METHOD gr_send_request->set_sender
EXPORTING
i_sender = gr_sender.
* Sender addess frm_set_sender-End
* create receiver list frm_set_receiver_list-Begin
***收件人处理
LOOP AT it_recipients_email INTO gs_bapiadsmtp.
IF sy-uname = zcl_crm_attributes=>gc_chenguoguang.
gs_bapiadsmtp-e_mail = 'airwolf_chen@163.com'.
ENDIF.
FREE gr_recipient.
gr_recipient = cl_cam_address_bcs=>create_internet_address( gs_bapiadsmtp-e_mail ).
gr_send_request->add_recipient( gr_recipient ).
ENDLOOP.
***抄送人处理
LOOP AT it_cc_recipients_email INTO gs_bapiadsmtp.
IF sy-uname = zcl_crm_attributes=>gc_chenguoguang.
gs_bapiadsmtp-e_mail = '744793323@qq.com'.
ENDIF.
FREE gr_recipient.
gr_recipient = cl_cam_address_bcs=>create_internet_address( gs_bapiadsmtp-e_mail ).
gr_send_request->add_recipient( EXPORTING i_copy = 'X' i_recipient = gr_recipient ).
ENDLOOP.
* create receiver list frm_set_receiver_list-End
* send mail
gr_send_request->set_send_immediately( 'X' ).
gv_os_boolean = gr_send_request->send( i_with_error_screen = '' ).
COMMIT WORK AND WAIT.
CATCH cx_bcs INTO gr_bcs_exception.
* MESSAGE i865(so) WITH lo_bcs_exception->error_type.
ENDTRY.
ENDFUNCTION.