Hi, I provide training in SAP UI5. Also feel free to checkout my new web page https://saptechblog.com
ABAP

Triggering mail in SAP

Purpose :  A Mail triggering function module was created for the purpose of triggering the mail. Since SAP is recommending to use the class CL_BCS for the purpose of communication services a function module was required to assemble different method calls of the class that are required for sending mail.

Interface of the Function Module:

Importing Parameter:

clip_image002

Exporting Parameter:

clip_image004

 

 FUNCTION ZFM_MAIL_BCS.
*"----------------------------------------------------------------------

*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(IM_MESSAGE) TYPE  SOLI_TAB
*"     REFERENCE(IM_SUBJECT) TYPE  STRING
*"     REFERENCE(IM_SENDER) TYPE  ADR6-SMTP_ADDR
*"     REFERENCE(IM_RECEIVER) TYPE  ADR6-SMTP_ADDR
*"     REFERENCE(IM_FOOTER) TYPE  SOLI_TAB
*"     REFERENCE(IM_FILE_SIZE) TYPE  SOOD-OBJLEN OPTIONAL
*"     REFERENCE(IM_FILE_CONTENT) TYPE  SOLI_TAB OPTIONAL
*"  EXPORTING
*"     REFERENCE(EX_RESULT) TYPE  BOOLEAN
*"----------------------------------------------------------------------

*----------------------------------------------------------------------*
* DATA DECLARATIONS
*----------------------------------------------------------------------*
 
DATA :   LO_MAIL            TYPE REF TO CL_BCS,                       " Object reference for the class BCS
           LO_EMAIL_BODY     
TYPE REF TO CL_DOCUMENT_BCS,              " Object reference for the document class BCS
           LO_SENDER         
TYPE REF TO IF_SENDER_BCS,                " Object reference : Sender
           LV_SEND_RESULT    
TYPE C,                                   " Flag : Send result
           LO_RECEIVER       
TYPE REF TO IF_RECIPIENT_BCS,             " Object reference : Receiver
           LX_EXCEPTION      
TYPE REF TO CX_BCS,                       " Exception reference for the class BCS
           LO_STATUS         
TYPE REF TO CL_SEND_REQUEST_BCS,          " Object reference to set the status
           LV_STAT           
TYPE BCS_STML VALUE 'N',                  " Status flag
           IT_FINAL          
TYPE STANDARD TABLE OF W3HTML,            " Internal TABLE FINAL MESSAGE BODY
           WA_FINAL          
TYPE W3HTML,                              " Work area message body
           WA_MESSAGE        
TYPE SOLI.                                " Work area message imported

 
CONSTANTS : C_HTM(3TYPE C        VALUE 'HTM',                      " HTML Document type
              C_NO     
TYPE C        VALUE 'N'.                        " No Delivery status : flag


 


* Processing the message internal table.

 
LOOP AT IM_MESSAGE INTO WA_MESSAGE.

   
CONCATENATE WA_MESSAGE-LINE '<BR>' INTO WA_FINAL-LINE
.
   
APPEND WA_FINAL-LINE TO
IT_FINAL.
   
CLEAR
WA_FINAL.

 
ENDLOOP
.


* Processing the footer internal table.


 
LOOP AT IM_FOOTER INTO WA_MESSAGE.

   
CONCATENATE WA_MESSAGE-LINE '<BR>' INTO WA_FINAL-LINE
.
   
APPEND WA_FINAL-LINE TO
IT_FINAL.
   
CLEAR
WA_FINAL.

 
ENDLOOP
.



 
TRY
.

* Getting the object reference for the class


     
CALL METHOD
CL_BCS=>CREATE_PERSISTENT
        RECEIVING
          RESULT = LO_MAIL.


 


* set the status of the send request

      LO_STATUS = LO_MAIL->SEND_REQUEST.


* Creating a document which is to be sent via mail

      LO_EMAIL_BODY = CL_DOCUMENT_BCS=>CREATE_DOCUMENT(
      I_TYPE = C_HTM                                                                                         
" 'HTM'
      I_TEXT = IT_FINAL
      I_SUBJECT =
'' ).

* Adding an Pdf attachment


     
CALL METHOD LO_EMAIL_BODY->ADD_ATTACHMENT
       
EXPORTING

          I_ATTACHMENT_TYPE     =
'PDF'
          I_ATTACHMENT_SUBJECT  =
'PF_SETTLEMENT'
          I_ATTACHMENT_SIZE     = IM_FILE_SIZE
          I_ATT_CONTENT_TEXT    = IM_FILE_CONTENT
          .



* Set the subject of the mail

     
CALL METHOD LO_MAIL->SET_MESSAGE_SUBJECT
       
EXPORTING

          IP_SUBJECT = IM_SUBJECT.

* set the document for mail


      LO_MAIL->SET_DOCUMENT( LO_EMAIL_BODY ).


* Add the sender

      LO_SENDER = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( IM_SENDER ).

     
CALL METHOD LO_MAIL->SET_SENDER
       
EXPORTING

          I_SENDER = LO_SENDER.

* Set the receipient address

      LO_RECEIVER = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( IM_RECEIVER ).

* Adding the email receipent

      LO_MAIL->ADD_RECIPIENT( I_RECIPIENT = LO_RECEIVER
                               I_EXPRESS = ABAP_TRUE ).                                                
" 'X'
* Set the email to trigger immediately


LO_MAIL->SET_SEND_IMMEDIATELY( ABAP_TRUE ).                             
" 'X'


* Set the delivery status of the mail as no delivery status mail required
     
CALL METHOD LO_STATUS->SETU_STATUS_MAIL
       
EXPORTING

          I_STATUS_MAIL = LV_STAT.

     
CALL METHOD LO_MAIL->SET_STATUS_ATTRIBUTES
       
EXPORTING

          I_REQUESTED_STATUS = LV_STAT
          I_STATUS_MAIL      = C_NO.                                                                                
" 'N'


* Triggering the mail

      LO_MAIL->SEND(
EXPORTING
                              I_WITH_ERROR_SCREEN = ABAP_TRUE                                             
" 'X'
                     RECEIVING
                              RESULT = LV_SEND_RESULT ).

      EX_RESULT = LV_SEND_RESULT.

     
COMMIT WORK.

   
CATCH CX_BCS INTO
LX_EXCEPTION.

      EX_RESULT =
' '
.

 
ENDTRY
.

ENDFUNCTION
.

About Arun

1 Comments:

  1. Use the class cl_sapuser_bcs to create the recipient address if you wanted the mail to be triggered to sap user inbox

    ReplyDelete

Powered by Blogger.