Sending email from SAP CRM

Problem: You want to send an email to an outside inbox.

Solution: Thanks to the Object Oriented programming techniques used in CRM this task is neat and easy. Objects will allow you to quickly construct email sending procedure. An example of this procedure can be found below. This is the most commonly needed solution. More complex ones can be build by extending it.

  TYPE-POOLS: abap.
 
  DATA: lt_mail_text TYPE soli_tab,
        ls_mail_text TYPE soli,
        lo_email TYPE REF TO cl_bcs,
        lo_recipient TYPE REF TO if_recipient_bcs,
        lo_sender TYPE REF TO if_sender_bcs,
        lv_send_result TYPE os_boolean,
        lo_root_ex TYPE REF TO cx_root,
        lo_document TYPE REF TO if_document_bcs,
        lv_mail_subject TYPE so_obj_des,
        lv_subject TYPE string.
 
  TRY.
      CALL METHOD cl_cam_address_bcs=>create_internet_address
        EXPORTING
          i_address_string = 'john.doe@some.domain.com'
        RECEIVING
          result           = lo_recipient.
 
      CALL METHOD cl_bcs=>create_persistent
        RECEIVING
          result = lo_email.
 
      CALL METHOD lo_email->add_recipient
        EXPORTING
          i_recipient = lo_recipient.
 
      lv_subject = 'This is an example subject of the email'.
      CALL METHOD lo_email->set_message_subject
        EXPORTING
          ip_subject = lv_subject.
 
      " internet address with description
      CALL METHOD cl_cam_address_bcs=>create_internet_address
        EXPORTING
          i_address_string = 'jane.doe@example.domain.com'
          i_address_name   = 'Jane Doe'
        RECEIVING
          result           = lo_sender.
 
      CALL METHOD lo_email->set_sender
        EXPORTING
          i_sender = lo_sender.
 
      ls_mail_text-line = 'This is an example line of text '.
      append ls_mail_text to lt_mail_text.
      ls_mail_text-line = 'Email can contain <strong>html tags</strong>.'.
      append ls_mail_text to lt_mail_text.
 
      lv_mail_subject = lv_subject.
      CALL METHOD cl_document_bcs=>create_document
        EXPORTING
          i_type    = 'HTM'
          i_subject = lv_mail_subject
          i_text    = lt_mail_text
        RECEIVING
          result    = lo_document.
 
      CALL METHOD lo_email->set_document
        EXPORTING
          i_document = lo_document.
 
      lo_email->set_send_immediately( abap_true ).
      CALL METHOD lo_email->send
        EXPORTING
          i_with_error_screen = abap_true
        RECEIVING
          result              = lv_send_result.
 
* possible exceptions at this point are:
* cx_address_bcs, cx_document_bcs, cx_send_req_bcs
    CATCH cx_root INTO lo_root_ex.
 ENDTRY.
  TYPE-POOLS: abap.

  DATA: lt_mail_text TYPE soli_tab,
        ls_mail_text TYPE soli,
        lo_email TYPE REF TO cl_bcs,
        lo_recipient TYPE REF TO if_recipient_bcs,
        lo_sender TYPE REF TO if_sender_bcs,
        lv_send_result TYPE os_boolean,
        lo_root_ex TYPE REF TO cx_root,
        lo_document TYPE REF TO if_document_bcs,
        lv_mail_subject TYPE so_obj_des,
        lv_subject TYPE string.

  TRY.
      CALL METHOD cl_cam_address_bcs=>create_internet_address
        EXPORTING
          i_address_string = 'john.doe@some.domain.com'
        RECEIVING
          result           = lo_recipient.

      CALL METHOD cl_bcs=>create_persistent
        RECEIVING
          result = lo_email.

      CALL METHOD lo_email->add_recipient
        EXPORTING
          i_recipient = lo_recipient.

      lv_subject = 'This is an example subject of the email'.
      CALL METHOD lo_email->set_message_subject
        EXPORTING
          ip_subject = lv_subject.

      " internet address with description
      CALL METHOD cl_cam_address_bcs=>create_internet_address
        EXPORTING
          i_address_string = 'jane.doe@example.domain.com'
          i_address_name   = 'Jane Doe'
        RECEIVING
          result           = lo_sender.

      CALL METHOD lo_email->set_sender
        EXPORTING
          i_sender = lo_sender.

      ls_mail_text-line = 'This is an example line of text '.
      append ls_mail_text to lt_mail_text.
      ls_mail_text-line = 'Email can contain <strong>html tags</strong>.'.
      append ls_mail_text to lt_mail_text.

      lv_mail_subject = lv_subject.
      CALL METHOD cl_document_bcs=>create_document
        EXPORTING
          i_type    = 'HTM'
          i_subject = lv_mail_subject
          i_text    = lt_mail_text
        RECEIVING
          result    = lo_document.

      CALL METHOD lo_email->set_document
        EXPORTING
          i_document = lo_document.

      lo_email->set_send_immediately( abap_true ).
      CALL METHOD lo_email->send
        EXPORTING
          i_with_error_screen = abap_true
        RECEIVING
          result              = lv_send_result.

* possible exceptions at this point are:
* cx_address_bcs, cx_document_bcs, cx_send_req_bcs
    CATCH cx_root INTO lo_root_ex.
 ENDTRY.

Komentowanie jest zakończone.