Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
surajarafath
Contributor

Sending email to persons is become quite common, and i found it is very simple to send email using CL_BCS. One more thing which is often needed is attachment in the email. I usually do in this way, like convert whatever format (either excel,pdf,doc,etc..,) into XSTRING then attach it in the mail. Then after some times, we need to format the email content, Color, table,Bigger Font, Bold........ So i thought its better to use HTML, so we can format it as per our need. And another thing for receipients, we will often have user name but need to write logic to get email address (ie. SU01) .  These things are often used, so i write a Simple Function with these features..

Example For Importing Parameters:

I_SUBJECT = 'Email Subject'.

APPEND '<b> Dear XXX </b>' to IT_CONTENT.
APPEND 'This is the Sample emali' to IT_CONTENT.
APPEND 'Thank You' to IT_CONTENT.

LS_RECEIPIENTS-IUSRID = 'USERNAME'.
APPEND LS_RECEIPIENTS TO IT_RECEIPIENTS.
LS_RECEIPIENTS-EMAIL = 'abc@email.com'.
APPEND LS_RECEIPIENTS TO IT_RECEIPIENTS.

  CALL FUNCTION 'ZSEND_EMAIL'
    EXPORTING
     i_subject              = I_SUBJECT
     it_content             = IT_CONTENT
     it_receipients         = IT_RECEIPIENTS.

If attachment needs to be added, remember to pass all the parameters for attachment.

I_ATTACH_TYPE = 'PDF'.
I_ATTACH_SUBJECT = 'Test Document.pdf'.
I_ATTACH_XSTRING = XSTRING.

  CALL FUNCTION 'ZSEND_EMAIL'
    EXPORTING
     i_subject              = I_SUBJECT
     it_content             = IT_CONTENT
     it_receipients         = IT_RECEIPIENTS
     i_attach_type          = I_ATTACH_TYPE
     i_attach_subject       = I_ATTACH_SUBJECT
     i_attach_xstring       = I_ATTACH_XSTRING .

FUNCTION zsend_email.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(I_SUBJECT) TYPE  SO_OBJ_DES
*"     REFERENCE(IT_CONTENT) TYPE  SOLI_TAB
*"     REFERENCE(IT_RECEIPIENTS) TYPE  UIYT_IUSR
*"     REFERENCE(I_ATTACH_TYPE) TYPE  SO_OBJ_TP OPTIONAL
*"     REFERENCE(I_ATTACH_SUBJECT) TYPE  SO_OBJ_DES OPTIONAL
*"     REFERENCE(I_ATTACH_XSTRING) TYPE  XSTRING OPTIONAL
*"  EXPORTING
*"     REFERENCE(BAPIRETURN) TYPE  BAPIRET2_T
*"----------------------------------------------------------------------
*"----------------------------------------------------------------------
*   Created By  : Surajarafath
*   Created On  : 27.03.2012
*   Description : This function Sends Email to Recipeint, with / without
*                       attachment
*   Notes       : Function can also get the email id of Username Passed
*"----------------------------------------------------------------------
* MODIFICATION LOG :
* Date           Modified by      Changes
*"----------------------------------------------------------------------

  DATA:  lt_email_content TYPE TABLE OF soli,
         ls_email_content TYPE soli.
  DATA:  lt_receipients TYPE TABLE OF uiys_iusr,
         ls_receipients TYPE uiys_iusr.
  FIELD-SYMBOLS <fs_receipients> TYPE uiys_iusr.
  DATA:  lv_email_subject TYPE so_obj_des.
  DATA   lt_message TYPE TABLE OF soli.
  DATA   ls_return  TYPE bapiret2.
  DATA   lt_return  TYPE bapiret2_t.
  DATA   lv_text    TYPE soli-line.

  DATA   send_email TYPE REF TO cl_bcs.
  DATA   document TYPE REF TO cl_document_bcs.
  DATA   recipient TYPE REF TO if_recipient_bcs.
  DATA   sent_to_all TYPE os_boolean.
  DATA   lv_error_occured TYPE os_boolean.
  DATA   lv_email_sent TYPE char1.

  DATA lv_attach_content_solix TYPE solix_tab.
  DATA lv_attach_size        TYPE so_obj_len.

*Initialization
  lt_email_content[] = it_content[].
  lv_email_subject = i_subject.


*Get Receipients
  IF it_receipients[] IS INITIAL.
    CLEAR ls_return.
    ls_return-type = 'E'.
    ls_return-message = 'No email Receipients Found'.
    APPEND ls_return TO bapireturn.
    RETURN.
  ELSE.
    lt_receipients[] = it_receipients[].
  ENDIF.
  "Get Email Id For the User Name
  LOOP AT lt_receipients ASSIGNING <fs_receipients> WHERE email IS INITIAL.
    SELECT SINGLE b~smtp_addr INTO <fs_receipients>-email
    FROM usr21 AS a
    INNER JOIN adr6  AS b
    ON  a~persnumber  = b~persnumber
    AND a~addrnumber  = b~addrnumber
    WHERE a~bname     = <fs_receipients>-iusrid
    AND   b~date_from LE sy-datum.
  ENDLOOP.

*Get the Content of the Mail
  APPEND '<body>' TO lt_message.
  LOOP AT lt_email_content INTO ls_email_content.
    CONCATENATE '<p>' ls_email_content-line '</p>'
    INTO lv_text.
    APPEND lv_text TO lt_message.
  ENDLOOP.
  APPEND '</body>' TO lt_message.
  APPEND '<br>' TO lt_message.

  TRY.

*Initiate
      send_email = cl_bcs=>create_persistent( ).

*Create Document
      document =  cl_document_bcs=>create_document(
          i_type        =  'HTM'
          i_subject     =  lv_email_subject
          i_text        =  lt_message ).

*Add Attachment
      IF  i_attach_type IS NOT INITIAL
      AND i_attach_subject IS NOT INITIAL.
      AND i_attach_xstring IS NOT INITIAL.
          lv_attach_content_solix = cl_document_bcs=>xstring_to_solix( i_attach_xstring ).
          lv_attach_size = XSTRLEN( i_attach_xstring ).
        document->add_attachment(
                i_attachment_type     = i_attach_type
                i_attachment_subject  = i_attach_subject
                i_attachment_size     = lv_attach_size
                i_att_content_hex     = lv_attach_content_solix        ) .
      ENDIF.

*Set Document to the Mail
      send_email->set_document( document ).

*Add Receipients to Mail
      LOOP AT lt_receipients INTO ls_receipients WHERE email IS NOT INITIAL.
        recipient = cl_cam_address_bcs=>create_internet_address( ls_receipients-email ).
        send_email->add_recipient( i_recipient = recipient ).
      ENDLOOP.
      IF sy-subrc NE 0.
        CLEAR ls_return.
        ls_return-type = 'E'.
        ls_return-message = 'No email Receipients Found'.
        APPEND ls_return TO bapireturn.
        RETURN.
      ENDIF.

*Send Email
      sent_to_all = send_email->send( i_with_error_screen = 'X' ).
      COMMIT WORK.
      IF sent_to_all EQ 'X'.
        CLEAR ls_return.
        ls_return-type = 'S'.
        ls_return-message = 'Email has been sent successfully'.
        APPEND ls_return TO bapireturn.
      ELSE.
        CLEAR ls_return.
        ls_return-type = 'E'.
        ls_return-message = 'Error Occured While Sending email'.
        APPEND ls_return TO bapireturn.
        RETURN.
      ENDIF.

    CATCH cx_address_bcs.
    CATCH cx_document_bcs .
    CATCH cx_send_req_bcs.
  ENDTRY.
ENDFUNCTION.

*It is a simple function for basic email with/without one attachment file.

1 Comment