Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Attaching binary files to an e-mail with the correct file extension

Former Member
0 Kudos

Right. I'm nearly finished on this. I've read the recent blogs on sending e-mails from an ABAP programme and, with a few tweaks here and there, I've got my programme sending an e-mail to someone with two PDFs attached. One is the content of the e-mail, and the other is a PDF which we want attached to every e-mail. Here's the function module I'm using:

function z_zemail_send.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(REQUESTED_STATUS) TYPE  BCS_RQST DEFAULT 'E'
*"     REFERENCE(DOCUMENTS) TYPE  ZEMAIL_DOCUMENTS_TAB
*"     REFERENCE(RECIPIENTS) TYPE  ZEMAIL_RECIPIENTS_TAB
*"  RAISING
*"      CX_BCS
*"----------------------------------------------------------------------

* class definitions
  data: send_request   type ref to cl_bcs,
        document       type ref to cl_document_bcs,
        sender         type ref to cl_sapuser_bcs,
        recipient      type ref to if_recipient_bcs,
        bcs_exception  type ref to cx_bcs.

* internal tables
  data: iaddsmtp       type table of bapiadsmtp,
        ireturn        type table of bapiret2.

* variables
  data: bapiadsmtp     type bapiadsmtp.

* pointers
  field-symbols: <fs_documents>  type zemail_documents,
                 <fs_iaddsmtp>   type bapiadsmtp,
                 <fs_recipients> type zemail_recipients.

* constants
  constants: marktwo_domain(14) type c value '@marktwo.co.uk'.

  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.

    loop at documents assigning <fs_documents>.
      if first is initial.
        move 'X' to first.
* build the main document
        if <fs_documents>-content_hex[] is initial.
          document = cl_document_bcs=>create_document(
                              i_type    = <fs_documents>-type
                              i_text    = <fs_documents>-content_text
                              i_subject = <fs_documents>-subject ).
        else.
          document = cl_document_bcs=>create_document(
                              i_type    = <fs_documents>-type
                              i_hex     = <fs_documents>-content_hex
                              i_subject = <fs_documents>-subject ).
        endif.
      else.
        if <fs_documents>-content_hex[] is initial.
* add attachment
          call method document->add_attachment
            exporting
              i_attachment_type    = <fs_documents>-type
              i_attachment_subject = <fs_documents>-subject
              i_att_content_text   = <fs_documents>-content_text.
        else.
          call method document->add_attachment
            exporting
              i_attachment_type    = <fs_documents>-type
              i_attachment_subject = <fs_documents>-subject
              i_att_content_hex    = <fs_documents>-content_hex.
        endif.
      endif.
    endloop.
    unassign: <fs_documents>.


* add document to send request
    call method send_request->set_document( document ).

* get sender object
    sender = cl_sapuser_bcs=>create( sy-uname ).

* add sender
    call method send_request->set_sender
      exporting
        i_sender = sender.

    loop at recipients assigning <fs_recipients>.
      if <fs_recipients>-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 = <fs_recipients>-uname
          TABLES
            RETURN   = ireturn
            ADDSMTP  = iaddsmtp.
        loop at iaddsmtp assigning <fs_iaddsmtp> where std_no = 'X'.
          clear bapiadsmtp.
          move <fs_iaddsmtp> TO bapiadsmtp.
        endloop.
        unassign: <fs_iaddsmtp>.
* if no e-mail address was found, create one
        if bapiadsmtp-e_mail = ''.
          concatenate <fs_recipients>-uname marktwo_domain
                    into <fs_recipients>-c_address.
        else.
          move bapiadsmtp-e_mail to <fs_recipients>-c_address.
        endif.
      endif.

      recipient = cl_cam_address_bcs=>create_internet_address(
                  <fs_recipients>-c_address ).
* add recipient with its respective attributes to send request
      CALL METHOD send_request->add_recipient
        EXPORTING
          I_RECIPIENT  = recipient
          I_EXPRESS    = <fs_recipients>-i_express
          I_COPY       = <fs_recipients>-i_copy
          I_BLIND_COPY = <fs_recipients>-i_blind_copy
          I_NO_FORWARD = <fs_recipients>-i_no_forward.

    endloop.
    unassign: <fs_recipients>.

* 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.

And it's called by this programme:

*&---------------------------------------------------------------------*
*& report zrm_emailtest                                                *
*&---------------------------------------------------------------------*

report zrm_emailtest.

* class definitions
data: err_ref type ref to cx_root.

* internal tables
data: xdocuments    type standard table of zemail_documents,
      xrecipients   type standard table of zemail_recipients,
      xcontent_hex  type standard table of solix.

* line structures
data: ls_xdocuments    type zemail_documents,
      ls_xrecipients   type zemail_recipients,
      ls_xcontent_text type soli,
      ls_xcontent_hex  type solix.

* variables
data: zerror(255) type c,
      zfile(255) type c,
      zcounter type i,
      zbyte(1) type x.

selection-screen begin of block b1 with frame.
  parameters: p_data(255) type c lower case,
              p_recp(241) type c lower case,
              p_titl(50)  type c lower case.
selection-screen end of block b1.

move: '.' to ls_xcontent_text-line.
append: ls_xcontent_text to ls_xdocuments-content_text.
clear ls_xcontent_text-line.
append: ls_xcontent_text to ls_xdocuments-content_text.

move: '\mk2cinsapmntPRDDVEBMGS00workleaflet.pdf' to zfile.
open dataset zfile for input in binary mode.
  do.
    read dataset zfile into zbyte maximum length 1.
    if sy-subrc eq 0.
      if zcounter eq 254.
        move: zbyte to ls_xcontent_hex-line+zcounter,
              0 to zcounter.
        append ls_xcontent_hex to xcontent_hex.
        clear ls_xcontent_hex.
      else.
        move: zbyte to ls_xcontent_hex-line+zcounter.
        add 1 to zcounter.
      endif.
    else.
      exit.
    endif.
  enddo.
close dataset zfile.
if not ls_xcontent_hex-line is initial.
  append ls_xcontent_hex to xcontent_hex.
endif.

move: p_data to ls_xcontent_text-line,
      p_recp to ls_xrecipients-c_address,
      'RAW' to ls_xdocuments-type,
      p_titl to ls_xdocuments-subject.

append: ls_xcontent_text to ls_xdocuments-content_text,
        ls_xrecipients to xrecipients,
        ls_xdocuments to xdocuments.

move: 'BIN' to ls_xdocuments-type,
      'Delivery Leaflet.PDF' to ls_xdocuments-subject.

move: xcontent_hex to ls_xdocuments-content_hex.

append: ls_xdocuments to xdocuments.
try.
  clear zerror.
  CALL FUNCTION 'Z_ZEMAIL_SEND'
    EXPORTING
      REQUESTED_STATUS       = 'E'
      DOCUMENTS              = xdocuments
      RECIPIENTS             = xrecipients.
catch cx_bcs into err_ref.
  zerror = err_ref->get_text( ).
cleanup.
  if not zerror is initial.
    message zerror type 'E'.
  endif.
  clear: err_ref,
         zerror.
endtry.

Before anyone says anything, yes, I am going to change that so it reads it into an xstring and parses it internally - it's waiting until I've got time to do it.

Now, the problem is that the file "Delivery Leaflet.PDF" gets attached as "Delivery Leaflet.PDF.BIN". This is obviously not good. Obviously when I just put "Delivery Leaflet" in ls_xdocuments-subject it's attached as "Delivery Leaflet.BIN".

Would anyone know what I need to do to get this file attached without a ".BIN" extension?

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi Robert,

1. move: 'BIN' to ls_xdocuments-type

Change the above line to.

move: 'PDF' to ls_xdocuments-type

Regards,

Amit M.

4 REPLIES 4

Former Member
0 Kudos

Hi Robert,

1. move: 'BIN' to ls_xdocuments-type

Change the above line to.

move: 'PDF' to ls_xdocuments-type

Regards,

Amit M.

former_member188685
Active Contributor
0 Kudos

move: 'BIN' to ls_xdocuments-type,

'Delivery Leaflet.PDF' to ls_xdocuments-subject.

change the above one to

<b>move: 'PDF' to ls_xdocuments-type,

'Delivery Leaflet' to ls_xdocuments-subject.</b>

Former Member
0 Kudos

Cheers guys, 8 out of great.

Former Member
0 Kudos

Dear robert,

kindly inform about the structure of table zemail_documents & zemail_recipients. what are the various field in these two structures.

Thanks

anupma