FUNCTION yfm_send_email.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" VALUE(IV_UNAME) TYPE XUBNAME
*" VALUE(IV_SUBJECT) TYPE STRING
*" EXPORTING
*" VALUE(ET_MESSAGES) TYPE BAPIRET2_T
*" TABLES
*" IT_EMAILS TYPE YST_T_LISTEMAIL
*" IT_TEXT TYPE SOLI_TAB
Structure of YST_T_LISTEMIL - table type
UNAME 1 Types XUBNAME
TYPE 1 Types YMTYPE -(1 char with Default values 1 - From 2, To , 3- CC)
EMAIL 1 Types AD_SMTPADR
*"----------------------------------------------------------------------
DATA : ls_emails TYPE yst_s_listemail,
lt_obj_head TYPE TABLE OF solisti1,
ls_obj_head TYPE solisti1,
ls_text TYPE soli,
lv_sent_to_all TYPE os_boolean,
gr_sender TYPE REF TO cl_sapuser_bcs,
lo_sender TYPE REF TO if_sender_bcs,
gr_send_request TYPE REF TO cl_bcs,
gr_bcs_exception TYPE REF TO cx_bcs,
gr_recipient TYPE REF TO if_recipient_bcs,
gr_document TYPE REF TO cl_document_bcs.
CLASS cl_bcs DEFINITION LOAD.
TRY.
"Create send request
gr_send_request = cl_bcs=>create_persistent( ).
" Get the sender name
CLEAR ls_emails.
READ TABLE it_emails INTO ls_emails WITH KEY type = gc_typfm.
IF sy-subrc EQ 0.
"Email FROM...
* gr_sender = cl_sapuser_bcs=>create( ls_emails-uname ).
lo_sender = cl_cam_address_bcs=>create_internet_address(
i_address_string = ls_emails-email
i_address_name = ls_emails-email ).
"Add sender to send request
CALL METHOD gr_send_request->set_sender
EXPORTING
i_sender = lo_sender. "gr_sender.
ENDIF.
" CC Emails
CLEAR ls_emails.
LOOP AT it_emails INTO ls_emails WHERE type = gc_typcc.
CLEAR gr_recipient.
gr_recipient = cl_cam_address_bcs=>create_internet_address( ls_emails-email ).
"Add recipient to send request
CALL METHOD gr_send_request->add_recipient
EXPORTING
i_recipient = gr_recipient
i_express = 'X'.
CLEAR : ls_emails.
ENDLOOP.
" To Emails
CLEAR ls_emails.
LOOP AT it_emails INTO ls_emails WHERE type = gc_typto.
CLEAR gr_recipient.
gr_recipient = cl_cam_address_bcs=>create_internet_address( ls_emails-email ).
"Add recipient to send request
CALL METHOD gr_send_request->add_recipient
EXPORTING
i_recipient = gr_recipient.
* i_copy = 'X'.
CLEAR : ls_emails.
ENDLOOP.
REFRESH : lt_obj_head[].
LOOP AT it_text INTO ls_text.
CLEAR ls_obj_head.
ls_obj_head-line = ls_text-line.
APPEND ls_obj_head TO lt_obj_head .
CLEAR ls_text.
ENDLOOP.
* Message body and subject
*Create Email document
gr_document = cl_document_bcs=>create_document( "create document
i_type = 'HTM' "Type of document HTM, TXT etc
i_text = lt_obj_head "it_text "email body internal table
i_subject = ' '). "email subject here p_sub input parameter
CALL METHOD gr_send_request->set_message_subject
EXPORTING
ip_subject = iv_subject.
"Add document to send request
CALL METHOD gr_send_request->set_document( gr_document ).
"Send email
CLEAR lv_sent_to_all.
CALL METHOD gr_send_request->send(
EXPORTING
i_with_error_screen = 'X'
RECEIVING
result = lv_sent_to_all ).
IF lv_sent_to_all = 'X'.
* WRITE 'Email sent!'.
REFRESH : et_messages[].
PERFORM update_message TABLES et_messages
USING c_msgcls
'057'
c_s.
"Commit to send email
COMMIT WORK.
ENDIF.
"Exception handling
CATCH cx_bcs INTO gr_bcs_exception.
REFRESH : et_messages[].
PERFORM update_message TABLES et_messages
USING c_msgcls
'058'
c_e.
* gr_bcs_exception->error_type.
ENDTRY.
ENDFUNCTION.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" VALUE(IV_UNAME) TYPE XUBNAME
*" VALUE(IV_SUBJECT) TYPE STRING
*" EXPORTING
*" VALUE(ET_MESSAGES) TYPE BAPIRET2_T
*" TABLES
*" IT_EMAILS TYPE YST_T_LISTEMAIL
*" IT_TEXT TYPE SOLI_TAB
Structure of YST_T_LISTEMIL - table type
UNAME 1 Types XUBNAME
TYPE 1 Types YMTYPE -(1 char with Default values 1 - From 2, To , 3- CC)
EMAIL 1 Types AD_SMTPADR
*"----------------------------------------------------------------------
DATA : ls_emails TYPE yst_s_listemail,
lt_obj_head TYPE TABLE OF solisti1,
ls_obj_head TYPE solisti1,
ls_text TYPE soli,
lv_sent_to_all TYPE os_boolean,
gr_sender TYPE REF TO cl_sapuser_bcs,
lo_sender TYPE REF TO if_sender_bcs,
gr_send_request TYPE REF TO cl_bcs,
gr_bcs_exception TYPE REF TO cx_bcs,
gr_recipient TYPE REF TO if_recipient_bcs,
gr_document TYPE REF TO cl_document_bcs.
CLASS cl_bcs DEFINITION LOAD.
TRY.
"Create send request
gr_send_request = cl_bcs=>create_persistent( ).
" Get the sender name
CLEAR ls_emails.
READ TABLE it_emails INTO ls_emails WITH KEY type = gc_typfm.
IF sy-subrc EQ 0.
"Email FROM...
* gr_sender = cl_sapuser_bcs=>create( ls_emails-uname ).
lo_sender = cl_cam_address_bcs=>create_internet_address(
i_address_string = ls_emails-email
i_address_name = ls_emails-email ).
"Add sender to send request
CALL METHOD gr_send_request->set_sender
EXPORTING
i_sender = lo_sender. "gr_sender.
ENDIF.
" CC Emails
CLEAR ls_emails.
LOOP AT it_emails INTO ls_emails WHERE type = gc_typcc.
CLEAR gr_recipient.
gr_recipient = cl_cam_address_bcs=>create_internet_address( ls_emails-email ).
"Add recipient to send request
CALL METHOD gr_send_request->add_recipient
EXPORTING
i_recipient = gr_recipient
i_express = 'X'.
CLEAR : ls_emails.
ENDLOOP.
" To Emails
CLEAR ls_emails.
LOOP AT it_emails INTO ls_emails WHERE type = gc_typto.
CLEAR gr_recipient.
gr_recipient = cl_cam_address_bcs=>create_internet_address( ls_emails-email ).
"Add recipient to send request
CALL METHOD gr_send_request->add_recipient
EXPORTING
i_recipient = gr_recipient.
* i_copy = 'X'.
CLEAR : ls_emails.
ENDLOOP.
REFRESH : lt_obj_head[].
LOOP AT it_text INTO ls_text.
CLEAR ls_obj_head.
ls_obj_head-line = ls_text-line.
APPEND ls_obj_head TO lt_obj_head .
CLEAR ls_text.
ENDLOOP.
* Message body and subject
*Create Email document
gr_document = cl_document_bcs=>create_document( "create document
i_type = 'HTM' "Type of document HTM, TXT etc
i_text = lt_obj_head "it_text "email body internal table
i_subject = ' '). "email subject here p_sub input parameter
CALL METHOD gr_send_request->set_message_subject
EXPORTING
ip_subject = iv_subject.
"Add document to send request
CALL METHOD gr_send_request->set_document( gr_document ).
"Send email
CLEAR lv_sent_to_all.
CALL METHOD gr_send_request->send(
EXPORTING
i_with_error_screen = 'X'
RECEIVING
result = lv_sent_to_all ).
IF lv_sent_to_all = 'X'.
* WRITE 'Email sent!'.
REFRESH : et_messages[].
PERFORM update_message TABLES et_messages
USING c_msgcls
'057'
c_s.
"Commit to send email
COMMIT WORK.
ENDIF.
"Exception handling
CATCH cx_bcs INTO gr_bcs_exception.
REFRESH : et_messages[].
PERFORM update_message TABLES et_messages
USING c_msgcls
'058'
c_e.
* gr_bcs_exception->error_type.
ENDTRY.
ENDFUNCTION.