Application Development and Automation 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: 
Read only

text file attachment in UTF-8 encoding

Former Member
0 Likes
10,287

Hi

I have written a program which sends mails to the users with text file attached. the problem is the text file when you save it to the local desktop ( by clicking on save as ) the encoding is by default ANSI. I want to make the encoding as UTF-8. Is it possible to change this in program?.

thanks

sankar

1 ACCEPTED SOLUTION
Read only

athavanraja
Active Contributor
0 Likes
3,766

before sending the mail you have to encode your textfile into utf-8 . here is the code sample for the same.

data: binary_content type solix_tab ,
      text_string type string .
data: xs_content type xstring ,
       app_type(50) .

app_type = 'text/plain; charset=utf-8'.

call function 'SCMS_STRING_TO_XSTRING'
  exporting
    text     = text_string
    mimetype = app_type
  importing
    buffer   = xs_content.


concatenate  cl_abap_char_utilities=>byte_order_mark_utf8
             xs_content
             into xs_content in byte mode.
refresh binary_content .
clear:len, length .
call function 'SCMS_XSTRING_TO_BINARY'
  exporting
    buffer     = xs_content
  importing
  output_length = length
  tables
    binary_tab = binary_content.

now you can use binary_content table for emailing.

Raja

4 REPLIES 4
Read only

athavanraja
Active Contributor
0 Likes
3,767

before sending the mail you have to encode your textfile into utf-8 . here is the code sample for the same.

data: binary_content type solix_tab ,
      text_string type string .
data: xs_content type xstring ,
       app_type(50) .

app_type = 'text/plain; charset=utf-8'.

call function 'SCMS_STRING_TO_XSTRING'
  exporting
    text     = text_string
    mimetype = app_type
  importing
    buffer   = xs_content.


concatenate  cl_abap_char_utilities=>byte_order_mark_utf8
             xs_content
             into xs_content in byte mode.
refresh binary_content .
clear:len, length .
call function 'SCMS_XSTRING_TO_BINARY'
  exporting
    buffer     = xs_content
  importing
  output_length = length
  tables
    binary_tab = binary_content.

now you can use binary_content table for emailing.

Raja

Read only

0 Likes
3,766

Hi

In the binary_content table the data is coming something like this

FBBBF0A534F554D54534F5645524C4159303109544D6F6220554D5453204F7665726C6179<

which seems to in binary form. I am using SO_DOCUMENT_SEND_API1'

to send the mail with attachment. I am passing the text file content to the table parameter contents_bin . Now in this case how do I use this binary_content in this FM?

thanks

sankar

Read only

0 Likes
3,766

pass the binary_content itab to CONTENTS_HEX tables parameter instead of contents_bin

Raja

Read only

Former Member
0 Likes
3,766

OPEN DATASET - encoding

Syntax

... ENCODING { DEFAULT

| {UTF-8 [SKIPPING|WITH BYTE-ORDER MARK]}

| NON-UNICODE } ... .

Alternatives:

1. ... DEFAULT

2. ... UTF-8 [SKIPPING|WITH BYTE-ORDER MARK]

3. ... NON-UNICODE

Effect

: The additions after ENCODING determine the character representation in which the content of the file is handled. The addition ENCODING must be specified in Unicode programs and may only be omitted in non-Unicode programs. If the addition ENCODING is not specified in non-Unicode programs, the addition NON-UNICODE is used implicitly.

Note

: It is recommended that files are always written in UTF-8, if all readers can process this format. Otherwise, the code page can depend on the text environment and it is difficult to identify the code page from the file content.

Alternative 1

... DEFAULT

Effect

: In a Unicode system, the specification DEFAULT corresponds to UTF-8, and in a non-Unicode system, it corresponds to NON-UNICODE.

Alternative 2

... UTF-8 [SKIPPING|WITH BYTE-ORDER MARK]

Addition:

... SKIPPING|WITH BYTE-ORDER MARK

Effect

: The characters in the file are handled according to the Unicode character representation UTF-8.

Notes

: The class CL_ABAP_FILE_UTILITIES contains the method CHECK_UTF8 for determining whether a file is a UTF-8 file.

A UTF-16 file can only be opened as a binary file.

Addition

... SKIPPING|WITH BYTE-ORDER MARK

Effect

: This addition defines how the byte order mark (BOM), with which a file encoded in the UTF-8 format can begin, is handled. The BOM is a sequence of 3 bytes that indicates that a file is encoded in UTF-8.

SKIPPING BYTE-ORDER MARK

is only permitted if the file is opened for reading or changing using FOR INPUT or FOR UPDATE. If there is a BOM at the start of the file, this is ignored and the file pointer is set after it. Without the addition, the BOM is handled as normal file content.

WITH BYTE-ORDER MARK

is only permitted if the file is opened for writing using FOR OUTPUT. When the file is opened, a BOM is inserted at the start of the file. Without the addition, no BOM is inserted.

The addition BYTE-ORDER MARK cannot be used together with the AT POSITION.

Notes

: When opening UTF-8 files for reading, it is recommended to always enter the addition SKIPPING BYTE-ORDER MARK so that a BOM is not handled as file content.

It is recommended to always open a file for reading as a UTF-8 with the addition WITH BYTE-ORDER MARK, if all readers can process this format.

Alternative 3

... NON-UNICODE

Effect

: In a non-Unicode system, the data is read or written without conversion. In a Unicode system, the characters of the file are handled according to the non-Unicode codepage that would be assigned at the time of reading or writing in a non-Unicode system according to the entry in the database table TCP0C of the current text environment.