Skip to Content
0
Former Member
May 03, 2011 at 09:49 AM

Attaching file to an email using CL_CRM_EMAIL_DATA

113 Views

Hi experts, I am new in abap, I am trying to attach a rar file to an email in oop using class CL_CRM_EMAIL_DATA. However, I don't seem to get it right since the attached file seems to be empty.

Here is my sample code below:

REPORT  ztestmailoop.

PARAMETERS: p_file TYPE rlgrap-filename.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  CALL FUNCTION 'WS_FILENAME_GET'
    EXPORTING
      def_filename     = p_file
      mode             = 'O'
      title            = 'Upload File'(078)
    IMPORTING
      filename         = p_file
    EXCEPTIONS
      inv_winsys       = 1
      no_batch         = 2
      selection_cancel = 3
      selection_error  = 4
      OTHERS           = 5.

*- Data Declaration
DATA: lr_mail_data       TYPE REF TO CL_CRM_EMAIL_DATA.
DATA: lv_to              TYPE CRMS_EMAIL_RECIPIENT.
DATA: lv_from            TYPE CRMS_EMAIL_RECIPIENT.
DATA: ls_struc_mail      TYPE CRMS_EMAIL_MIME_STRUC.
DATA: lv_send_request    TYPE SYSUUID_X.

*- create mail object
CREATE OBJECT lr_mail_data.

*- Email subject
lr_mail_data->subject = 'Some subject'.

*- To recipient (may be more)
lv_to-address = <insert email address>.
lv_to-name = <insert email address>.
APPEND lv_to TO lr_mail_data->to.

*- From recipient
MOVE 'SAP CRM <insert email address>' TO lv_from-address.
MOVE 'SAP CRM <insert email address>' TO lv_from-name.
MOVE 'SAP CRM <insert email address>' TO lv_from-id.
lr_mail_data->from = lv_from.

*- Email Body (HTML)
MOVE 'text/html' TO ls_struc_mail-mime_type.
MOVE 'body.htm' TO ls_struc_mail-file_name.
MOVE '<br>Some HTML text<br><br>cool?<br>' TO ls_struc_mail-content_ascii.
APPEND ls_struc_mail TO lr_mail_data->body.
CLEAR ls_struc_mail.

MOVE 'application/x-rar-compressed' TO ls_struc_mail-mime_type.
MOVE p_file TO ls_struc_mail-file_name.
MOVE 'X' TO ls_struc_mail-is_attachment.
APPEND ls_struc_mail TO lr_mail_data->body.

*- Send Email
lv_send_request = CL_CRM_EMAIL_UTILITY_BASE=>SEND_EMAIL( iv_mail_data = lr_mail_data ).

Thanks in advance.