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: 

Email attachment problem - unicode?

former_member194198
Active Participant
0 Kudos

Hi All,

I've copied some of the examples from SDN on how to send an attachment via email from ABAP.

The problem is that the text file attachments are incorrect. Each character is separated by another character. If I create a text attachment that reads "Hello" in the ABAP, the resulting text attachment is "H E L L O ".

The SAP system is unicode. I have tried both SO_NEW_DOCUMENT_ATT_SEND_API1 and SO_DOCUMENT_ATT_SEND_API1. Both have the same effect.

Does anyone have any idea how to ensure the email attachments retain the same format?

Cheers

Richard

7 REPLIES 7

former_member223537
Active Contributor
0 Kudos

Hi,

Please check the following sample program & compare your program with the same.

It seems you have missed out some parameter which needs to be passed to the FM.

If still the problem persists, then pls paste your code.

Best regards,

Prashant

Former Member
0 Kudos

Hi Richard,

Please refer to the below mentioned code and compare the Function modules used in your program and the below one.

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

  • Program: ZRAT Creation Date: 12.04.2007 *

  • Program Title : Retirement Age Trigger *

  • Program Author: Puneet Jhari *

  • Program Type : Executable *

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

  • Object ID : *

  • Technical Specification Version: *

  • Technical Specification Author: *

  • Code Quality Reviewer : *

  • Transport No. : SRDK906499 *

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

  • Program Description: *

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

  • Inputs *

  • Tables: *

  • Parameters *

  • External files: *

  • Outputs *

  • Reports: *

  • Tables: *

  • Databases: *

  • Screens: *

  • Parameters *

  • External files: *

  • Other objects: *

  • Return codes set by ABAP: *

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

  • Modification Informaion *

  • Program Version: *

  • Date : DD-MMM-YYYY *

  • Author : *

  • Description : *

  • Transport No.: *

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

REPORT zrat NO STANDARD PAGE HEADING.

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

*DATA DECLARATION

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

DATA : BEGIN OF wa_ccode,

bukrs TYPE t001-bukrs,

END OF wa_ccode.

DATA : itab_ccode LIKE TABLE OF wa_ccode.

DATA : BEGIN OF wa_emp,

pernr TYPE pa0001-pernr,

sachp TYPE pa0001-sachp,

sname TYPE pa0001-sname,

END OF wa_emp.

DATA : itab_emp LIKE TABLE OF wa_emp.

DATA : BEGIN OF wa_bdate,

nachn LIKE pa0002-nachn,

vorna LIKE pa0002-vorna,

pernr TYPE pa0002-pernr,

gbdat TYPE pa0002-gbdat,

END OF wa_bdate.

DATA : itab_bdate LIKE TABLE OF wa_bdate.

DATA : new_date LIKE sy-datum.

DATA : diff TYPE i.

DATA : years LIKE p0347-scryy,

months LIKE p0347-scrmm,

days LIKE p0347-scrdd.

DATA : sup_code LIKE pa0001-sachp,

itab_usrid LIKE t526-usrid,

sup_pernr LIKE pa0105-pernr.

DATA : BEGIN OF wa_email,

usrid_long LIKE pa0105-usrid_long,

END OF wa_email.

DATA : sup_email LIKE TABLE OF wa_email.

DATA : gwa_document_data LIKE sodocchgi1,

gc_name TYPE so_obj_nam VALUE 'RETIREMENT',

gc_senst TYPE so_obj_sns VALUE 'P',

gc_size TYPE so_doc_siz VALUE '510',

gi_obj_cnt LIKE TABLE OF solisti1 WITH HEADER LINE,

gi_recievers LIKE somlreci1 OCCURS 0 WITH HEADER LINE,

gwa_obj_cnt LIKE LINE OF gi_obj_cnt.

DATA : smtp_addr LIKE adr6-smtp_addr.

DATA : wa_date LIKE sy-datum,

entries LIKE sy-tabix,

name(15),

line1(18) TYPE c,

line2(20) TYPE c,

line3(20) TYPE c,

line4(23) TYPE c,

line5(10) TYPE c,

line6(45) type c,

date(2) TYPE c,

month(2) TYPE c,

year(4) TYPE c.

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

*START OF SELECTION

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

SELECT bukrs

FROM t001

INTO TABLE itab_ccode

WHERE land1 EQ 'GB'.

SORT itab_ccode.

DELETE ADJACENT DUPLICATES FROM itab_ccode.

SELECT pernr sachp sname

FROM pa0001

INTO TABLE itab_emp

FOR ALL ENTRIES IN itab_ccode

WHERE bukrs EQ itab_ccode-bukrs AND begda LE sy-datum AND

endda GE sy-datum and persg eq '1'.

SELECT nachn vorna pernr gbdat

FROM pa0002

INTO TABLE itab_bdate

FOR ALL ENTRIES IN itab_emp

WHERE pernr EQ itab_emp-pernr.

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

*TO CHECK EMPLOYEES WHOSE AGE IS NEAR 75 YEARS.

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

LOOP AT itab_bdate INTO wa_bdate.

new_date = wa_bdate-gbdat.

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

*TO CALCULATE THE AGE TILL DATE

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

CALL FUNCTION 'HR_HK_DIFF_BT_2_DATES'

EXPORTING

date1 = sy-datum

date2 = new_date

output_format = '05'

IMPORTING

years = years

months = months

days = days

.

IF sy-subrc <> 0.

ENDIF.

IF years EQ 64 AND months EQ 4.

IF days GE 23 AND days LE 31.

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

*FETCH THE DATA IF THE AGE IS NEARING 75 YEARS

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

SELECT SINGLE sachp FROM pa0001 INTO sup_code WHERE pernr EQ

wa_bdate-pernr AND begda LE sy-datum AND endda GE sy-datum.

SELECT SINGLE usrid FROM t526 INTO itab_usrid WHERE sachx EQ

sup_code.

SELECT SINGLE pernr FROM pa0105 INTO sup_pernr WHERE usrid EQ

itab_usrid AND subty EQ '0001'.

clear sup_email[].

SELECT usrid_long FROM pa0105 INTO TABLE sup_email WHERE pernr EQ

sup_pernr AND subty EQ '0010'.

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

*EMAIL ADDRESS OF THE SUPERVISOR TO BE NOTIFIED

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

CLEAR gi_obj_cnt.

CLEAR gi_recievers.

CLEAR gwa_document_data.

Loop at sup_email into wa_email.

write sy-uline+0(5).

write sy-uline+10(5).

write sy-uline+20(5).

write sy-uline+30(5).

write sy-uline+40(5).

write sy-uline+50(5).

write sy-uline+60(5).

write sy-uline+70(5).

smtp_addr = wa_email-usrid_long.

gi_recievers-receiver = smtp_addr.

gi_recievers-rec_type = 'U'.

gi_recievers-com_type = 'INT'.

APPEND gi_recievers.

Endloop.

smtp_addr = 'Mairi.Davidson@subsea7.com'.

gi_recievers-receiver = smtp_addr.

gi_recievers-rec_type = 'U'.

gi_recievers-com_type = 'INT'.

APPEND gi_recievers.

gwa_document_data-obj_name = gc_name.

gwa_document_data-obj_descr = 'RETIRE'.

gwa_document_data-sensitivty = gc_senst.

gwa_document_data-obj_langu = sy-langu.

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

*CONTENT OF THE EMAIL TO BE SENT

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

line1 = 'Please note that '.

line2 = wa_bdate-vorna.

line3 = wa_bdate-nachn.

line4 = ' will be 65 years on '.

line50(4) = wa_bdate-gbdat4(4).

date = line5+2(2).

month = line5+0(2).

year = wa_bdate-gbdat+0(4) + 65.

CONCATENATE date month year INTO line5 SEPARATED BY '.'.

line6 = '.Please complete the Retirement Procedure.'.

CONCATENATE line1 line2 line3 line4 line5 line6 INTO gwa_obj_cnt

SEPARATED BY space.

APPEND gwa_obj_cnt TO gi_obj_cnt.

CLEAR gwa_obj_cnt.

DESCRIBE TABLE gi_obj_cnt LINES entries.

READ TABLE gi_obj_cnt INDEX entries.

gwa_document_data-doc_size = ( entries - 1 ) * 255 + STRLEN( gi_obj_cnt

).

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

*FUNCTION MODULE TO SEND THE EMAIL TO THE CONCERNED PERSONS

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

CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1'

EXPORTING

document_data = gwa_document_data

document_type = 'RAW'

commit_work = 'X'

TABLES

object_content = gi_obj_cnt

receivers = gi_recievers

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

.

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

*MESSAGE TO BE DISPLAYED AFTER THE EXECUTION

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

  • endif.

CASE sy-subrc.

WHEN 0.

LOOP AT gi_recievers.

IF gi_recievers-receiver = space.

name = gi_recievers-rec_id.

ELSE.

name = gi_recievers-receiver.

ENDIF.

IF gi_recievers-retrn_code = 0.

WRITE: / name, ': succesfully sent'.

ELSE.

WRITE: / name, ': error occured'.

ENDIF.

ENDLOOP.

WHEN 1.

WRITE: / 'Too many receivers specified !'.

WHEN 2.

WRITE: / 'No receiver got the document !'.

WHEN 4.

WRITE: / 'Missing send authority !'.

WHEN OTHERS.

WRITE: / 'Unexpected error occurred !'.

ENDCASE.

ENDIF.

ENDIF.

ENDLOOP.

In case you have any further clarifications,do let me know.

Regards,

Puneet Jhari.

0 Kudos

Sorry, this is as I have coded the function module. The problem lies with the attachment, not the email text so you example is not helpful. The email body is fine. The email attachment is not correct.

former_member194198
Active Participant
0 Kudos

Let me a little more specific.

I know how to send emails via ABAP as I have this code working in other SAP systems. I know that the code works fine for non-unicode systems. The body of the email is correct. The attachment has the problem. The code doesn't work in a unicode system.

The attachment should read "Here is your file". It actually reads " H e r e i s y o u r f i l e".

The code is :

REPORT ZRTTEST.

data: itcpo like itcpo,

tab_lines like sy-tabix.

  • Variables for EMAIL functionality

data: maildata like sodocchgi1.

data: mailpack like sopcklsti1 occurs 2 with header line.

data: mailhead like solisti1 occurs 1 with header line.

data: mailbin like solisti1 occurs 10 with header line.

data: mailtxt like solisti1 occurs 10 with header line.

data: mailrec like somlrec90 occurs 0 with header line.

data: solisti1 like solisti1 occurs 0 with header line.

perform send_form_via_email.

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

  • FORM SEND_FORM_VIA_EMAIL *

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

form send_form_via_email.

clear: maildata, mailtxt, mailbin, mailpack, mailhead, mailrec.

refresh: mailtxt, mailbin, mailpack, mailhead, mailrec.

  • Creation of the document to be sent File Name

maildata-obj_name = 'TEST'.

  • Mail Subject

maildata-obj_descr = 'Subject'.

  • Mail Contents

mailtxt-line = 'Here is your file'.

append mailtxt.

  • Prepare Packing List

perform prepare_packing_list.

  • Set recipient - email address here!!!

mailrec-receiver = 'test@test.com'.

mailrec-rec_type = 'U'.

append mailrec.

  • Sending the document

call function 'SO_NEW_DOCUMENT_ATT_SEND_API1'

exporting

document_data = maildata

put_in_outbox = 'X'

tables

packing_list = mailpack

object_header = mailhead

contents_bin = mailbin

contents_txt = mailtxt

receivers = mailrec

exceptions

too_many_receivers = 1

document_not_sent = 2

operation_no_authorization = 4

others = 99.

endform.

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

  • Form PREPARE_PACKING_LIST

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

form prepare_packing_list.

clear: mailpack, mailbin, mailhead.

refresh: mailpack, mailbin, mailhead.

describe table mailtxt lines tab_lines.

read table mailtxt index tab_lines.

maildata-doc_size = ( tab_lines - 1 ) * 255 + strlen( mailtxt ).

  • Creation of the entry for the compressed document

clear mailpack-transf_bin.

mailpack-head_start = 1.

mailpack-head_num = 0.

mailpack-body_start = 1.

mailpack-body_num = tab_lines.

mailpack-doc_type = 'RAW'.

append mailpack.

mailhead = 'TEST.TXT'.

append mailhead.

  • File 1

mailbin = 'This is file 1'.

append mailbin.

describe table mailbin lines tab_lines.

mailpack-transf_bin = 'X'.

mailpack-head_start = 1.

mailpack-head_num = 1.

mailpack-body_start = 1.

mailpack-body_num = tab_lines.

mailpack-doc_type = 'TXT'.

mailpack-obj_name = 'TEST1'.

mailpack-obj_descr = 'Subject'.

mailpack-doc_size = tab_lines * 255.

append mailpack.

endform.

former_member223537
Active Contributor
0 Kudos

Hi Richard,

Please pass the language in which the document is created in the PACKING LIST.

Best regards,

Prashant

0 Kudos

Adding the language to OBJ_LANGU in the packing list had no effect.

former_member194198
Active Participant
0 Kudos

Apparently, these FM's don't work in UNICODE mode. You have to use the new ABAP Object for email.

TRY.

  • -------- create persistent send request ------------------------

o_send_request = cl_bcs=>create_persistent( ).

  • Body of the email

WRITE sy-datum TO w_date.

CONCATENATE 'CARM Interface created on'(r06)

w_date

INTO st_text-line

SEPARATED BY space.

APPEND st_text TO t_text.

  • -------- create and set document with attachment ---------------

  • create document from internal table with text

o_document = cl_document_bcs=>create_document(

i_type = c_doc_raw

i_text = t_text

i_subject = 'CARM Interface'(r07) ).

  • add attachment to document

REFRESH t_text.

t_text[] = ft_attach[].

CALL METHOD o_document->add_attachment

EXPORTING

i_attachment_type = c_doc_raw

i_attachment_subject = 'CARM Interface'(r07)

i_att_content_text = t_text.

  • add document to send request

CALL METHOD o_send_request->set_document( o_document ).

  • --------- set sender -------------------------------------------

  • note: this is necessary only if you want to set the sender

  • different from actual user (SY-UNAME). Otherwise sender is

  • set automatically with actual user.

o_sender = cl_sapuser_bcs=>create( sy-uname ).

CALL METHOD o_send_request->set_sender

EXPORTING

i_sender = o_sender.

  • --------- add recipient (fax) -----------------------

  • create recipient - please replace fax number !!!

MOVE: p_email TO f_address_string.

o_recipient = cl_cam_address_bcs=>create_internet_address( f_address_string ).

  • add recipient with its respective attributes to send request

CALL METHOD o_send_request->add_recipient

EXPORTING

i_recipient = o_recipient

i_express = c_true.

  • ---------- send document ---------------------------------------

CALL METHOD o_send_request->send(

EXPORTING

i_with_error_screen = c_true

RECEIVING

result = l_sent_to_all ).

COMMIT WORK.