Skip to Content
author's profile photo Former Member
Former Member

email attachment with .txt file (first line blank in the file)

Hi all,

This is my code .Iam always getting first line blank in my file ,but file in the server does not contain in the original file . can any one resolve this issue?

REPORT ZTEST_FILE .

DATA:BEGIN OF t_upload occurs 0,

matnr LIKE zwplcsmev-matnr,

zwgehrrg LIKE zwplcsmev-zwgehrrg,

zwgehrct LIKE zwplcsmev-zwgehrct,

zwgbev LIKE zwplcsmev-zwgbev,

zwpldt LIKE zwplcsmev-zwpldt,

zwacdt LIKE zwplcsmev-zwacdt,

END OF t_upload.

*DATA: maildata type sodocchgi1.

*DATA: mailtxt type table of solisti1 with header line.

*DATA: mailrec type table of somlrec90 with header line.

DATA: it_message TYPE STANDARD TABLE OF solisti1 INITIAL SIZE 0

WITH HEADER LINE.

DATA:REC(80) TYPE C.

DATA g_mask(20) TYPE c VALUE ',., ..'.

DATA: it_attach TYPE STANDARD TABLE OF solisti1 INITIAL SIZE 0

WITH HEADER LINE.

DATA:P_ERROR(3).

DATA:P_REFO(3).

DATA: gd_error TYPE sy-subrc,

gd_reciever TYPE sy-subrc.

DATA: t_packing_list LIKE sopcklsti1 OCCURS 0 WITH HEADER LINE,

t_contents LIKE solisti1 OCCURS 0 WITH HEADER LINE,

t_receivers LIKE somlreci1 OCCURS 0 WITH HEADER LINE,

t_attachment LIKE solisti1 OCCURS 0 WITH HEADER LINE,

t_object_header LIKE solisti1 OCCURS 0 WITH HEADER LINE,

w_cnt TYPE i,

w_sent_all(1) TYPE c,

w_doc_data LIKE sodocchgi1.

selection-screen begin of block b1 with frame title text-001.

parameters:p_file type localfile.

parameter:p_email type ad_smtpadr.

selection-screen end of block b1.

----


--


At Selection-Screen- -

----


AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

CALL FUNCTION 'TB_LIMIT_WS_FILENAME_GET'

EXPORTING

def_filename = p_file

mask = g_mask

  • mode = 'S'

title = 'INPUT FILE'

IMPORTING

filename = p_file

EXCEPTIONS

selection_cancel = 1

selection_error = 2

OTHERS = 3.

IF sy-subrc <> 0.

  • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

  • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

START-OF-SELECTION.

PERFORM process_file.

if p_error = 'X' AND P_REFO <> 'X'.

PERFORM populate_message_body.

ENDIF.

IF P_REFO = 'X' AND P_ERROR <> 'X'.

it_message = 'Please find Attached file'.

APPEND it_message.

PERFORM send_attachment tables it_message

it_attach

using p_email

'Crest to Plc Data'

'TXT'

p_file

' '

' '

' '

changing gd_error

gd_reciever.

ENDIF.

  • Instructs mail send program for SAPCONNECT to send email(rsconn01)

  • PERFORM initiate_mail_execute_program.

END-OF-SELECTION.

&----


*& Form process_file

&----


  • text

----


FORM process_file.

*CONSTANTS: con_cret TYPE x VALUE '0D', "OK for non Unicode

  • con_tab TYPE x VALUE '09'. "OK for non Unicode

DATA :l_path TYPE string.

l_path = p_file.

CONSTANTS: con_tab type c value cl_abap_char_utilities=>HORIZONTAL_TAB

,

con_cret type c value cl_abap_char_utilities=>newline.

OPEN DATASET P_FILE FOR INPUT IN TEXT MODE encoding default.

if sy-subrc = 0.

do.

read dataset p_file into IT_ATTACH.

IF SY-SUBRC NE 0 .

EXIT.

ELSE.

T_UPLOAD = IT_ATTACH.

CONCATENATE con_cret it_attach INTO it_attach .

append it_attach.

P_REFO = 'X'.

APPEND T_UPLOAD .

clear t_upload.

ENDIF.

ENDDO.

else.

p_error = 'X'.

ENDIF.

ENDFORM. "process_file

&----


*& Form populate_message_body

&----


  • text

----


FORM populate_message_body.

w_doc_data-obj_name = 'TEST'.

w_doc_data-obj_descr = 'Crest to Plc Data'.

w_doc_data-obj_langu = sy-langu.

it_message = 'File Not Found'.

APPEND it_message.

t_receivers-receiver = p_email.

t_receivers-rec_type = 'U'.

append t_receivers.

call function 'SO_NEW_DOCUMENT_SEND_API1'

exporting

document_data = w_doc_data

document_type = 'RAW'

put_in_outbox = 'X'

tables

object_header = it_message

object_content = it_message

receivers = t_receivers

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.

commit work.

SUBMIT rsconn01 USING SELECTION-SET 'SAP&CONNECTINT' AND RETURN.

else.

MESSAGE s027(vv) WITH 'E-mail not sent'.

endif.

ENDFORM. "populate_message_body

&----


*& Form send_attachment

&----


  • text

----


FORM send_attachment tables pit_message

pit_attach

using p_email

p_mtitle

p_format

p_filename

p_attdescription

p_sender_address

p_sender_addres_type

changing p_error

p_reciever.

DATA: ld_error TYPE sy-subrc,

ld_reciever TYPE sy-subrc,

ld_mtitle LIKE sodocchgi1-obj_descr,

ld_email LIKE somlreci1-receiver,

ld_format TYPE so_obj_tp ,

ld_attdescription TYPE so_obj_nam ,

ld_attfilename TYPE so_obj_des ,

ld_sender_address LIKE soextreci1-receiver,

ld_sender_address_type LIKE soextreci1-adr_typ,

ld_receiver LIKE sy-subrc.

ld_email = p_email.

ld_mtitle = p_mtitle.

ld_format = p_format.

ld_attdescription = p_attdescription.

ld_attfilename = p_filename.

ld_sender_address = p_sender_address.

ld_sender_address_type = p_sender_addres_type.

w_doc_data-doc_size = 1.

w_doc_data-obj_langu = sy-langu.

w_doc_data-obj_name = 'SAPRPT'.

w_doc_data-obj_descr = ld_mtitle .

w_doc_data-sensitivty = 'F'.

  • Fill the document data and get size of attachment

CLEAR w_doc_data.

READ TABLE it_attach INDEX w_cnt.

w_doc_data-doc_size =

( w_cnt - 1 ) * 255 + STRLEN( it_attach ).

w_doc_data-obj_langu = sy-langu.

w_doc_data-obj_name = 'SAPRPT'.

w_doc_data-obj_descr = ld_mtitle.

w_doc_data-sensitivty = 'F'.

CLEAR t_attachment.

REFRESH t_attachment.

t_attachment[] = pit_attach[].

  • clear t_attachment.

  • Describe the body of the message

CLEAR t_packing_list.

REFRESH t_packing_list.

t_packing_list-transf_bin = space.

t_packing_list-head_start = 1.

t_packing_list-head_num = 0.

t_packing_list-body_start = 1.

DESCRIBE TABLE it_message LINES t_packing_list-body_num.

t_packing_list-doc_type = 'RAW'.

APPEND t_packing_list.

  • Create attachment notification

t_packing_list-transf_bin = 'X'.

t_packing_list-head_start = 1.

t_packing_list-head_num = 1.

t_packing_list-body_start = 1.

DESCRIBE TABLE t_attachment LINES t_packing_list-body_num.

t_packing_list-doc_type = ld_format.

t_packing_list-obj_descr = ld_attdescription.

t_packing_list-obj_name = ld_attfilename.

t_packing_list-doc_size = t_packing_list-body_num * 255.

APPEND t_packing_list.

  • Add the recipients email address

CLEAR t_receivers.

REFRESH t_receivers.

t_receivers-receiver = ld_email.

t_receivers-rec_type = 'U'.

t_receivers-com_type = 'INT'.

t_receivers-notif_del = 'X'.

t_receivers-notif_ndel = 'X'.

APPEND t_receivers.

CALL FUNCTION 'SO_DOCUMENT_SEND_API1'

EXPORTING

document_data = w_doc_data

put_in_outbox = 'X'

sender_address = ld_sender_address

sender_address_type = ld_sender_address_type

commit_work = 'X'

IMPORTING

sent_to_all = w_sent_all

TABLES

packing_list = t_packing_list

contents_bin = t_attachment

contents_txt = it_message

receivers = t_receivers

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.

commit work.

SUBMIT rsconn01 USING SELECTION-SET 'SAP&CONNECTINT' AND RETURN.

else.

MESSAGE s027(vv) WITH 'E-mail not sent'.

endif.

  • Populate error return code

ld_error = sy-subrc.

  • Populate zreceiver return code

LOOP AT t_receivers.

ld_receiver = t_receivers-retrn_code.

ENDLOOP.

ENDFORM. "send_attachment

&----


*& Form INITIATE_MAIL_EXECUTE_PROGRAM

&----


  • Instructs mail send program for SAPCONNECT to send email.

----


FORM initiate_mail_execute_program.

WAIT UP TO 2 SECONDS.

SUBMIT rsconn01 WITH mode = 'INT'

WITH output = 'X'

AND RETURN.

ENDFORM. " INITIATE_MAIL_EXECUTE_PROGRAM

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

1 Answer

  • author's profile photo Former Member
    Former Member
    Posted on Jan 09, 2007 at 08:15 PM

    Hi Priya,

    con_cret in the CONCATENATE statement is giving you blank as the first line, change the code as in bold.

    Try with this code.

    read dataset p_file into IT_ATTACH.

    IF SY-SUBRC NE 0 .

    EXIT.

    ELSE.

    T_UPLOAD = IT_ATTACH.

    *CONCATENATE con_cret it_attach INTO it_attach .

    <b>CONCATENATE it_attach con_cret INTO it_attach .</b>

    append it_attach.

    Thanks,

    Vinay

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.