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: 

Problems with specifying File Extension of E-Mail attachment

Former Member
0 Kudos

Hi everybody

I have a problem that seems to be so simple that it almost drives me crazy that I cannot find a solution for it.

I need to send the content of an internal table (some lines) as a mail attachment to a specified mail address.

The format should be normal ASCII, so when I open it with any text editor (e.g. Notepad), I want to have all my lines there (including CR/LF).

The problem now is - The extension of this mail attachment must be ".key", so the attached file should have the name "L_12345.key" (for example).

For E-Mailing I use FM "SO_NEW_DOCUMENT_ATT_SEND_API1".

This part works fine, also the attachment part works in general. (I receive the mail and I receive the attachment).

BUT I get either an attachment with the name "L_12345.txt" or "L_12345.key.txt" and in correct format, or I get an attachment "L_12345.key", where the name is correct but format is wrong (no CR/LF - everything in one line).

Example:

=======

Attachment-Content should be:

Line 1

Line 2

Line 3

Name of Attachment should be: L_12345.key

What I tried:

************

    • TEST 1 **

************

......

  • Declare Attachment in Packing list. ..

ls_packing_list-transf_bin = ' '.

ls_packing_list-head_start = 1.

ls_packing_list-head_num = 0.

ls_packing_list-body_start = 7. "Startline of Attachment

ls_packing_list-body_num = 3. "No of Attachment lines

ls_packing_list-doc_type = 'RAW'.

ls_packing_list-doc_size = 3 * 255.

ls_packing_list-obj_name = 'ATT01'.

ls_packing_list-obj_descr = 'L_12345' "Attachment name

APPEND ls_packing_list TO lt_packing_list.

.....

==> Result: Attachment Filename = L_12345.txt

==> File as I need it (ASCII with CR/LF), but extension wrong

Line 1

Line 2

Line 3

************

    • TEST 2 **

************

  • Same as TEST 1, but with:

ls_packing_list-obj_descr = 'L_12345.KEY' "Attachment name

==> Result: Attachment Filename = L_12345.KEY.txt

==> File as I need it (ASCII with CR/LF), but extension wrong

Line 1

Line 2

Line 3

************

    • TEST 3 **

************

  • Declare Attachment in Packing list. ..

......

ls_packing_list-transf_bin = ' '.

ls_packing_list-head_start = 1.

ls_packing_list-head_num = 0.

ls_packing_list-body_start = 7. "Startline of Attachment

ls_packing_list-body_num = 3. "No of Attachment lines

ls_packing_list-doc_type = 'KEY'.

ls_packing_list-doc_size = 3 * 255.

ls_packing_list-obj_name = 'ATT01'.

ls_packing_list-obj_descr = 'L_12345'. "Attachment name

APPEND ls_packing_list TO lt_packing_list.

......

==> Result: Attachment Filename = L_12345.KEY

==> File Format is wrong:

Line 1 Line 2 Line3

I have tried several other Extensions (DOC_TYPE), it seems, that the File format is only correct with "RAW", but it seems with "RAW" SAP adds ".txt" as extension to the filename.

Does anyone know how I can achieve having an e-mail attachment with multiple text lines and name "<anyname>.key"

Any help (if possible with example code ?) would be highly appreciated.

Many thanks

Harald

4 REPLIES 4

Former Member
0 Kudos

hi,

check the following code.


*&---------------------------------------------------------------------*
*&      Form  send_email
*&---------------------------------------------------------------------*
FORM send_email.

CLASS cl_abap_char_utilities DEFINITION LOAD.
CONSTANTS: con_cret TYPE c VALUE cl_abap_char_utilities=>cr_lf.

*Body of mail
  CLEAR objtxt.
  CLEAR objtxt[].

  objtxt = 'Dear abc'.
  APPEND objtxt.

  CLEAR objtxt.
  APPEND objtxt.

  objtxt = 'Attached herewith is the detail list of materials.'.
  APPEND objtxt.

  CLEAR objtxt.
  APPEND objtxt.

  objtxt = 'Regards,'.
  APPEND objtxt.

  objtxt = 'Admin'.
  APPEND objtxt.

  DESCRIBE TABLE objtxt LINES tab_lines.
  READ TABLE objtxt INDEX tab_lines.

*Mail description
  CLEAR docdata.

  docdata-doc_size  = ( tab_lines - 1 ) * 255 + STRLEN( objtxt ).
  docdata-obj_name  = 'List'.
  docdata-obj_descr = 'List of materials'.
  docdata-obj_langu = sy-langu.

*Packing list for main document
  CLEAR objpack.
  CLEAR objpack[].

  objpack-head_start = 1.
  objpack-head_num   = 0.
  objpack-body_start = 1.
  objpack-body_num   = tab_lines.
  objpack-doc_type   = 'RAW'.
  APPEND objpack.

*Attachment data
  CLEAR objbin.
  CLEAR objbin[].

  CONCATENATE
        'field1' 'field2' 'field3' 'field4'
    INTO objbin SEPARATED BY ','.

  CONCATENATE con_cret objbin INTO objbin.
  APPEND objbin.

  LOOP AT it_material_list INTO wa_material_list.

    MOVE-CORRESPONDING wa_material_list TO wa_char_material_list.

    CONCATENATE
          wa_char_material_list-field1
          wa_char_material_list-field2
          wa_char_material_list-field3
          wa_char_material_list-field4
      INTO objbin SEPARATED BY con_tab.

    CONCATENATE con_cret objbin INTO objbin.
    APPEND objbin.

    CLEAR objbin.
    CLEAR wa_char_employee_list.

  ENDLOOP.

  DESCRIBE TABLE objbin LINES tab_lines.
  READ TABLE objbin INDEX tab_lines.

*Packing list for attachment document
  CLEAR objpack.

  objpack-doc_size = ( tab_lines - 1 ) * 255 + STRLEN( objbin ).
  objpack-transf_bin = 'X'.          "check these values
  objpack-head_start = 1.          "check these values
  objpack-head_num   = 1.          "check these values
  objpack-body_start = 1.          "check these values
  objpack-body_num   = tab_lines.
  objpack-doc_type   = 'KEY'.
  objpack-obj_name   = 'Attachment'.
  objpack-obj_descr  = 'Report.KEY'.
  APPEND objpack.

*Email receiver's list
  CLEAR reclist.
  CLEAR reclist[].

  reclist-receiver = mail_id.
  reclist-rec_type = 'U'.
  APPEND reclist.

*Function module to send email with an attachment
  CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
    EXPORTING
      document_data              = docdata
      put_in_outbox              = 'X'
      commit_work                = 'X'
    TABLES
      packing_list               = objpack
      contents_bin               = objbin
      contents_txt               = objtxt
      receivers                  = reclist
    EXCEPTIONS
      too_many_receivers         = 1
      document_not_sent          = 2
      document_type_not_exist    = 3
      operation_no_authorization = 4
      parameter_error            = 5
      x_error                    = 6
      enqueue_error              = 7
      OTHERS                     = 8.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

ENDFORM.                    "send_email

Edited by: vikram shah on Sep 25, 2008 5:28 PM

Former Member
0 Kudos

Hi

Thanks a lot for your help.

I have now found the way how to do this.

Before coding you must enter a line in table TSOPE and enter you new extension and set the flag "ASCII_TYPE" to "X"

Then in Coding:

After you have entered all attachment lines to an internal tables, use FM 'SO_RAW_TO_RTF' to compress your attachment lines to one (or more - if necessary) lines and add a CR/LF after each line.

After this - Just add the lines to mailbody (as you normally do) and describe it in the packing list as:

- text (transf_bin = space)

- doc_type = <whatever extension you need>

- obj_descr = <filename without extension>

--> send it.

Thanks again.

Harald

0 Kudos

hi,

good to learn something new.

but i am curious to know.

did you try the code mentioned above? if yes, did it work?

0 Kudos

Hi

Yes - I tried your code, but unfortunately it did not work.

I still had some strange characters in the list and some lines without CR/LF.

Anyway - You brought me to the invsetigate deeper and pointed me to the right solution.

I want to thank you again for your quick response and your hints.

Best regards

Harald