I don't remember who wrote this function, however I use it every time I need to send an info mail.
The function simply send the mail, you have to supply the mail body, the recipients, the subject, etc.
You need to create two structures and two table types:
Component | Type | Data Type | Length | Decimal | Short description |
---|---|---|---|---|---|
TYPE | SO_OBJ_TP | CHAR | 3 | 0 | Code for document class |
SUBJECT | SO_OBJ_DES | CHAR | 50 | 0 | Short description of contents |
CONTENT_TEXT | SOLI_TAB | 0 | 0 | Objcont and Objhead as Table Type | |
CONTENT_HEX | SOLIX_TAB | 0 | 0 | GBT: SOLIX as Table Type |
Component | Type | Data Type | Length | Decimal | Short description |
---|---|---|---|---|---|
UNAME | SYUNAME | CHAR | 12 | 0 | User Name |
C_ADDRESS | AD_SMTPADR | CHAR | 241 | 0 | E-Mail Address |
I_EXPRESS | OS_BOOLEAN | CHAR | 1 | 0 | Boolean |
I_COPY | OS_BOOLEAN | CHAR | 1 | 0 | Boolean |
I_BLIND_COPY | OS_BOOLEAN | CHAR | 1 | 0 | Boolean |
I_NO_FOWARD | OS_BOOLEAN | CHAR | 1 | 0 | Boolean |
View the report Massive Send Mail for a usage example.
The source code:
FUNCTION z_send_mail. *"---------------------------------------------------------------------- *"*"Local Interface: *" IMPORTING *" VALUE(REQUESTED_STATUS) TYPE BCS_RQST DEFAULT 'E' *" VALUE(DOCUMENTS) TYPE ZES_KEG_EMAIL_DOCUMENTS *" VALUE(RECIPIENTS) TYPE ZES_KEG_EMAIL_RECIPIENTS *" REFERENCE(MAIL_SENDER) TYPE UNAME OPTIONAL *" RAISING *" CX_BCS *"---------------------------------------------------------------------- DATA: line TYPE so_raw255. *----------------------------------------------------------------------* * CLASS-DEFINITIONS * *----------------------------------------------------------------------* DATA: send_request TYPE REF TO cl_bcs. DATA: document TYPE REF TO cl_document_bcs. DATA: sender TYPE REF TO cl_sapuser_bcs. DATA: recipient TYPE REF TO if_recipient_bcs. DATA: exception_info TYPE REF TO if_os_exception_info, bcs_exception TYPE REF TO cx_bcs. *----------------------------------------------------------------------* * INTERNAL TABLES * *----------------------------------------------------------------------* DATA: l_mailtext TYPE soli_tab. DATA: l_mailhex TYPE solix_tab. DATA: iaddsmtp TYPE bapiadsmtp OCCURS 0 WITH HEADER LINE. DATA: ireturn TYPE bapiret2 OCCURS 0 WITH HEADER LINE. *----------------------------------------------------------------------* * VARIABLES * *----------------------------------------------------------------------* DATA: mail_line LIKE LINE OF l_mailtext. DATA: mailx_line LIKE LINE OF l_mailhex. DATA: bapiadsmtp TYPE bapiadsmtp. *----------------------------------------------------------------------* * CONSTANTS * *----------------------------------------------------------------------* CONSTANTS: kimball_domain(20) TYPE c VALUE '@my_domain.it'. "Insert your mail domain here CLASS cl_cam_address_bcs DEFINITION LOAD. CLASS cl_abap_char_utilities DEFINITION LOAD. TRY. " Create persistent send request send_request = cl_bcs=>create_persistent( ). DATA: first(1) TYPE c. CLEAR first. DATA: documents_line LIKE LINE OF documents. LOOP AT documents INTO documents_line. IF first IS INITIAL. MOVE 'X' TO first. " Build the Main Document IF documents_line-content_hex[] IS INITIAL. document = cl_document_bcs=>create_document( i_type = documents_line-type i_text = documents_line-content_text i_subject = documents_line-subject ). ELSE. document = cl_document_bcs=>create_document( i_type = documents_line-type i_hex = documents_line-content_hex i_subject = documents_line-subject ). ENDIF. ELSE. IF documents_line-content_hex[] IS INITIAL. " Add Attachment CALL METHOD document->add_attachment EXPORTING i_attachment_type = documents_line-type i_attachment_subject = documents_line-subject i_att_content_text = documents_line-content_text. ELSE. CALL METHOD document->add_attachment EXPORTING i_attachment_type = documents_line-type i_attachment_subject = documents_line-subject i_att_content_hex = documents_line-content_hex. ENDIF. ENDIF. ENDLOOP. " Add document to send request CALL METHOD send_request->set_document( document ). " Get sender object IF mail_sender IS INITIAL. sender = cl_sapuser_bcs=>create( sy-uname ). ELSE. sender = cl_sapuser_bcs=>create( mail_sender ). ENDIF. " Add sender CALL METHOD send_request->set_sender EXPORTING i_sender = sender. DATA: recipients_line LIKE LINE OF recipients. LOOP AT recipients INTO recipients_line. IF recipients_line-c_address IS INITIAL. " Create recipient CLEAR iaddsmtp. REFRESH iaddsmtp. CLEAR bapiadsmtp. CLEAR recipient. " Read the E-Mail address for the user CALL FUNCTION 'BAPI_USER_GET_DETAIL' EXPORTING username = recipients_line-uname TABLES return = ireturn addsmtp = iaddsmtp. LOOP AT iaddsmtp WHERE std_no = 'X'. CLEAR bapiadsmtp. MOVE iaddsmtp TO bapiadsmtp. ENDLOOP. " If no E-mail address was found, create one. IF bapiadsmtp-e_mail = ''. CONCATENATE recipients_line-uname kimball_domain INTO recipients_line-c_address. ELSE. MOVE bapiadsmtp-e_mail TO recipients_line-c_address. ENDIF. ENDIF. recipient = cl_cam_address_bcs=>create_internet_address( recipients_line-c_address ). " Add recipient with its respective attributes to send request CALL METHOD send_request->add_recipient EXPORTING i_recipient = recipient i_express = recipients_line-i_express i_copy = recipients_line-i_copy i_blind_copy = recipients_line-i_blind_copy i_no_forward = recipients_line-i_no_foward. ENDLOOP. " Set that you don't need a Return Status E-mail DATA: status_mail TYPE bcs_stml. status_mail = requested_status. CALL METHOD send_request->set_status_attributes EXPORTING i_requested_status = requested_status i_status_mail = status_mail. " set send immediately flag send_request->set_send_immediately( 'X' ). " Send document CALL METHOD send_request->send( ). COMMIT WORK. CATCH cx_bcs INTO bcs_exception. RAISE EXCEPTION bcs_exception. ENDTRY. ENDFUNCTION.