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: 

ITAB data as an mail attachment

Former Member
0 Kudos

Hi all,

I wantto send the ITAB Data as an mail attachment. But i amgetting error. Mail not been sent . What is the mistake i am doing ?

TABLES:adr6.

TYPES: BEGIN OF t_test,

x(3),

y(3),

z(3),

END OF t_test.

DATA: itab TYPE STANDARD TABLE OF t_test,

wa TYPE t_test.

SELECT-OPTIONS : s_rcvr FOR adr6-smtp_addr LOWER CASE VISIBLE LENGTH 30

NO INTERVALS OBLIGATORY.

wa-x = 12.

wa-y = 13.

wa-z = 14.

APPEND wa to itab.

PERFORM send_email.

&----


*& Form send_email

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


form send_email .

DATA : lo_mail_docu TYPE REF TO cl_document_bcs,

lo_sender TYPE REF TO if_sender_bcs VALUE IS INITIAL,

lo_recipient TYPE REF TO if_recipient_bcs VALUE IS INITIAL ,

lo_send_request TYPE REF TO cl_bcs VALUE IS INITIAL ,

l_oref TYPE REF TO cx_root,

l_message_body TYPE bcsy_text VALUE IS INITIAL,

l_message_line LIKE LINE OF l_message_body,

l_doc_subject TYPE so_obj_des,

l_file_subject TYPE so_obj_des,

l_mail_subject TYPE string,

l_text TYPE string.

DATA itab_bin TYPE TABLE OF solix.

CONCATENATE 'Error' ' Log -' INTO l_doc_subject SEPARATED BY space.

l_mail_subject = l_doc_subject.

CALL FUNCTION 'SCMS_TEXT_TO_BINARY'

TABLES

text_tab = itab

binary_tab = itab_bin.

IF sy-subrc <> 0.

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

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

ENDIF.

CONCATENATE 'Attached is the' 'log file generated '

INTO l_message_line-line SEPARATED BY space.

APPEND l_message_line TO l_message_body.

APPEND '' TO l_message_body. APPEND '' TO l_message_body.

TRY.

lo_mail_docu = cl_document_bcs=>create_document(

i_type = 'RAW'

i_text = l_message_body

i_subject = l_doc_subject ).

lo_mail_docu->add_attachment(

EXPORTING

i_attachment_type = 'TXT'

i_attachment_subject = l_file_subject

i_att_content_hex = itab_bin ).

lo_send_request = cl_bcs=>create_persistent( ).

CALL METHOD lo_send_request->set_message_subject

EXPORTING

ip_subject = l_mail_subject.

LOOP AT s_rcvr .

lo_recipient = cl_cam_address_bcs=>create_internet_address( s_rcvr-low ).

lo_send_request->add_recipient(

EXPORTING

i_recipient = lo_recipient

i_express = '' ).

ENDLOOP.

lo_send_request->set_document( lo_mail_docu ).

lo_send_request->set_send_immediately( 'X' ).

lo_send_request->send( ).

CATCH : cx_send_req_bcs INTO l_oref,

cx_document_bcs INTO l_oref,

cx_address_bcs INTO l_oref.

ENDTRY.

COMMIT WORK.

FREE : lo_mail_docu, lo_send_request, lo_sender, lo_recipient, l_oref.

REFRESH : l_message_body, itab_bin.

CLEAR : l_message_line, l_doc_subject, l_mail_subject, l_file_subject,

l_text.

endform.

Thanks in advance

Krupali

7 REPLIES 7

Former Member
0 Kudos

Hi,

Try below code.

TABLES:adr6.

TYPES: BEGIN OF t_test,

x(3),

y(3),

z(3),

END OF t_test.

DATA: itab TYPE STANDARD TABLE OF t_test,

wa TYPE t_test.

SELECT-OPTIONS : s_rcvr FOR adr6-smtp_addr LOWER CASE VISIBLE LENGTH 30

NO INTERVALS OBLIGATORY.

wa-x = 12.

wa-y = 13.

wa-z = 14.

APPEND wa to itab.

PERFORM send_email.

*&----


*& Form send_email

*&----


form send_email .

DATA : lo_mail_docu TYPE REF TO cl_document_bcs,

lo_sender TYPE REF TO if_sender_bcs VALUE IS INITIAL,

lo_recipient TYPE REF TO if_recipient_bcs VALUE IS INITIAL ,

lo_send_request TYPE REF TO cl_bcs VALUE IS INITIAL ,

l_oref TYPE REF TO cx_root,

l_message_body TYPE bcsy_text VALUE IS INITIAL,

l_message_line LIKE LINE OF l_message_body,

l_doc_subject TYPE so_obj_des,

l_file_subject TYPE so_obj_des,

l_mail_subject TYPE string,

l_text TYPE string.

DATA itab_bin TYPE TABLE OF solix.

CONCATENATE 'Error' ' Log -' INTO l_doc_subject SEPARATED BY space.

l_mail_subject = l_doc_subject.

CALL FUNCTION 'SCMS_TEXT_TO_BINARY'

TABLES

text_tab = itab

binary_tab = itab_bin.

IF sy-subrc <> 0.

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

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

ENDIF.

CONCATENATE 'Attached is the' 'log file generated '

INTO l_message_line-line SEPARATED BY space.

APPEND l_message_line TO l_message_body.

APPEND '' TO l_message_body. APPEND '' TO l_message_body.

TRY.

lo_mail_docu = cl_document_bcs=>create_document(

i_type = 'RAW'

i_text = l_message_body

i_subject = l_doc_subject ).

lo_mail_docu->add_attachment(

EXPORTING

i_attachment_type = 'TXT'

i_attachment_subject = l_file_subject

i_att_content_hex = itab_bin ).

lo_send_request = cl_bcs=>create_persistent( ).

CALL METHOD lo_send_request->set_message_subject

EXPORTING

ip_subject = l_mail_subject.

LOOP AT s_rcvr .

lo_recipient = cl_cam_address_bcs=>create_internet_address( s_rcvr-low )

.

lo_send_request->add_recipient(

EXPORTING

i_recipient = lo_recipient

i_express = '' ).

ENDLOOP.

lo_send_request->set_document( lo_mail_docu ).

lo_send_request->set_send_immediately( 'X' ).

lo_send_request->send( ).

CATCH : cx_send_req_bcs INTO l_oref,

cx_document_bcs INTO l_oref,

cx_address_bcs INTO l_oref.

ENDTRY.

COMMIT WORK.

FREE : lo_mail_docu, lo_send_request, lo_sender, lo_recipient, l_oref.

REFRESH : l_message_body, itab_bin.

CLEAR : l_message_line, l_doc_subject, l_mail_subject, l_file_subject,

l_text.

endform.

Regards

Jana

0 Kudos

HI,

Still i am facing the same problem.

Can anyone help me out ??

0 Kudos

HI,

Your not getting mails or Complete data?

Regards

Jana

0 Kudos

Hi jana,

I am not getting the mail.

0 Kudos

Hi KR,

I am getting mails with same code, try to give mail id of your company mail id.

Regards

jana

0 Kudos

Hi,

Pl go through this demo code, its working absolutely fine at my end.. just have a glance on this code.

FIELD-SYMBOLS: <field> TYPE ANY.

DATA : BEGIN OF i_dload OCCURS 0 ,

dload(1000) ,

END OF i_dload .

DATA: it_receivers LIKE somlreci1 OCCURS 1 WITH HEADER LINE,

w_object_content LIKE solisti1 OCCURS 1 WITH HEADER LINE.

DATA: w_doc_data LIKE sodocchgi1 OCCURS 0 WITH HEADER LINE.

DATA :w_records TYPE zres_records.

DATA: w_rsnum(10),

w_bdmng(14),

w_meins(4).

*---for internal e_mail address

DATA: ws_email LIKE adr6-smtp_addr.

*--Internal table declaration

DATA: it_objpack LIKE sopcklsti1 OCCURS 2 WITH HEADER LINE.

DATA: it_objhead LIKE solisti1 OCCURS 1 WITH HEADER LINE.

DATA: it_objbin LIKE solisti1 OCCURS 10 WITH HEADER LINE.

DATA: it_objtxt LIKE solisti1 OCCURS 10 WITH HEADER LINE.

DATA: it_reclist LIKE somlreci1 OCCURS 5 WITH HEADER LINE.

DATA: doc_chng LIKE sodocchgi1.

DATA: tab_lines LIKE sy-tabix.

DATA: w_longtext(255) TYPE c.

DATA: v_date(10) TYPE c.

DATA: text(200) TYPE c.

DATA: w_text(50) TYPE c.

*---declaration for diaplaying msg in the body of mail.

DATA: w_msga(18) TYPE c,

w_msgb(22) TYPE c,

w_msgc(27) TYPE c.

*--Get receipient address

CLEAR it_reclist.

*--To send error to internal mail

ws_email = p_email.

*---setting name of file for csv

CONCATENATE text-003 p_bdoc text-004 INTO w_filename.

to send error file.

IF p_email IS NOT INITIAL.

  • build body of message

*---text-009 'Errors in file '

CONCATENATE text-009 ' ' INTO it_objtxt

SEPARATED BY space.

APPEND it_objtxt.

CLEAR it_objtxt.

CLEAR: w_msga, w_msgb, w_msgc.

w_msga = text-012.

w_msgb = text-013.

w_msgc = text-014.

CONCATENATE w_msga w_msgb w_msgc INTO it_objtxt

SEPARATED BY space.

APPEND it_objtxt.

CLEAR it_objtxt.

*---end of code by sparta05_02+

it_reclist-receiver = ws_email.

it_reclist-rec_type = c_u.

it_reclist-express = ' '.

it_reclist-com_type = c_int.

APPEND it_reclist.

  • Set title of object and email

CLEAR: w_text.

doc_chng-obj_descr = text-005.

doc_chng-obj_name = c_inbound.

DESCRIBE TABLE it_objtxt LINES tab_lines.

READ TABLE it_objtxt INDEX tab_lines.

doc_chng-doc_size = ( tab_lines - 1 ) * 255 + STRLEN( it_objtxt ).

*--Move the Report Name as the Heading line for email file

CLEAR: w_longtext.

*---text-016 contains 'name of report for testing'

w_longtext = text-008.

it_objbin = w_longtext.

APPEND it_objbin. CLEAR it_objbin.

*--Append Blank Lines

APPEND it_objbin. CLEAR it_objbin.

APPEND it_objbin. CLEAR it_objbin.

  • ---appending heading to Internal table of Email

CLEAR: w_longtext, w_xblnr_1 , w_aubel_1, w_vemng_1,

w_matnr_1, w_harmdesc_1, w_hc_1, w_cooland1_1,

w_brgew_1, w_tknum_1, w_vbeln_1, w_vbeln1_1,

w_exidv_1, w_vhilm_1, w_pack_no_1,

w_brgew1_1, w_laeng_1, w_breit_1,

w_hoehe_1, w_vol_1,

w_gr_wt_1,

w_ni_wt_1,

w_imp_ln_1,

w_imp_wd_1,

w_imp_ht_1,

w_cu_ft_1.

w_xblnr_1 = 'Cust PO'.

w_aubel_1 = 'Order'.

w_vemng_1 = 'Qty'.

w_matnr_1 = 'Part#'.

w_harmdesc_1 = 'Harm Desc'.

w_hc_1 = 'HTS#'.

w_cooland1_1 = 'COO'.

w_brgew_1 = 'NW(KG)'.

w_tknum_1 = 'Ship ID'.

w_vbeln_1 = 'Bill#'.

w_vbeln1_1 = 'Delivery#'.

w_exidv_1 = 'ID#'.

w_vhilm_1 = 'Pkg'.

w_pack_no_1 = 'PCS'.

w_brgew1_1 = 'GW(KG)'.

w_laeng_1 = 'L(CM)'.

w_breit_1 = 'W(CM)'.

w_hoehe_1 = 'H(CM)'.

w_vol_1 = 'CBM'.

w_gr_wt_1 = 'GW(LB)'. "added by sparta01_01+

w_ni_wt_1 = 'NW(LB)'. "added by sparta05_04+

w_imp_ln_1 = 'L(IN)'.

w_imp_wd_1 = 'W(IN)'.

w_imp_ht_1 = 'H(IN)'.

w_cu_ft_1 = 'CUFT'.

CONCATENATE w_xblnr_1 w_aubel_1 w_vemng_1

w_matnr_1 w_harmdesc_1 w_hc_1 w_cooland1_1

w_brgew_1

w_tknum_1 w_vbeln_1 w_vbeln1_1

w_exidv_1 w_vhilm_1 w_pack_no_1

w_brgew1_1 w_laeng_1 w_breit_1

w_hoehe_1

w_vol_1

w_gr_wt_1 "added by sparta01_01+

w_ni_wt_1 "added by sparta05_04+

w_imp_ln_1 w_imp_wd_1

w_imp_ht_1

w_cu_ft_1

INTO w_longtext SEPARATED BY c_cma.

" changed by sparta05_03+ c_cma for ','.

it_objbin = w_longtext.

APPEND it_objbin. CLEAR it_objbin.

*--Append the details to Internal table of Email

LOOP AT it_tab.

DO .

*---to avoid display of error column

IF sy-index = c_25. "changed by sparta05_03+ (c_31 to c_25)

EXIT.

ENDIF.

ASSIGN COMPONENT sy-index OF STRUCTURE it_tab TO <field>.

IF sy-subrc <> 0.

EXIT.

ENDIF.

SEARCH <field> FOR ',' IN CHARACTER MODE.

  • If search for commas was successful.

IF sy-subrc = 0.

REPLACE ALL OCCURRENCES OF ','

IN <field> WITH space . "added by sparta05_04+

ENDIF.

IF sy-index = 1.

i_dload-dload = <field>.

SHIFT: i_dload-dload LEFT DELETING LEADING space.

CONDENSE i_dload-dload .

ELSE.

  • Put Comma as a separator for values in IT_DLOAD internal table

SHIFT: i_dload-dload LEFT DELETING LEADING space.

CONDENSE i_dload-dload .

CONCATENATE i_dload-dload <field> INTO i_dload-dload

SEPARATED BY c_cma IN CHARACTER MODE.

"changed by sparta05_03+ c_cma for ','

ENDIF.

ENDDO.

it_objbin = i_dload-dload.

APPEND it_objbin. CLEAR it_objbin.

IF NOT it_tab-error IS INITIAL.

it_objtxt+0(18) = it_tab-matnr.

it_objtxt+21(41) = it_tab-exidv.

it_objtxt+46(66) = it_tab-error.

APPEND it_objtxt.

CLEAR it_objtxt.

ENDIF.

ENDLOOP.

flag = 0.

ENDIF.

-Convert to correct format----


CALL FUNCTION 'SO_RAW_TO_RTF'

TABLES

objcont_old = it_objbin

objcont_new = it_objbin.

  • create the control table entry for the main email

DESCRIBE TABLE it_objtxt LINES tab_lines.

CLEAR it_objpack-transf_bin.

it_objpack-head_start = 1.

it_objpack-head_num = 0.

it_objpack-body_start = 1.

it_objpack-body_num = tab_lines.

it_objpack-doc_type = c_raw.

APPEND it_objpack.

DESCRIBE TABLE it_objbin LINES tab_lines.

it_objpack-head_start = 1.

it_objpack-head_num = 0.

it_objpack-body_start = 1.

it_objpack-body_num = tab_lines.

it_objpack-transf_bin = c_x.

it_objpack-doc_type = c_csv.

it_objpack-obj_descr = w_filename.

it_objpack-obj_name = c_billing. "code added by sparta05_02+

it_objpack-doc_size = tab_lines * 255.

APPEND it_objpack.

CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'

EXPORTING

document_data = doc_chng

  • PUT_IN_OUTBOX = c_x

commit_work = c_x

TABLES

packing_list = it_objpack

  • object_header = it_objhead

contents_bin = it_objbin

contents_txt = it_objtxt

receivers = it_reclist

EXCEPTIONS

too_many_receivers = 1

document_not_sent = 2

operation_no_authorization = 4

OTHERS = 99.

IF sy-subrc <> 0.

WRITE: / 'failure in sending mail'.

ELSE.

IF flag = 0.

MESSAGE s999(pp) WITH text-007.

ENDIF.

IF flag = 1.

MESSAGE s999(pp) WITH text-006.

ENDIF.

ENDIF.

ENDFORM. " p_send_email

Thanks,

Krishna

Subhankar
Active Contributor
0 Kudos

Hi .

See this example and check the sap out box. It also send mail to external Email ID.

REPORT z_test_subha3.

TABLES:adr6.

TYPES: BEGIN OF t_test,

x(3),

y(3),

z(3),

END OF t_test.

DATA: itab TYPE STANDARD TABLE OF t_test,

wa TYPE t_test.

SELECT-OPTIONS : s_rcvr FOR adr6-smtp_addr LOWER CASE VISIBLE LENGTH 30

NO INTERVALS OBLIGATORY.

wa-x = 12.

wa-y = 13.

wa-z = 14.

APPEND wa TO itab.

PERFORM send_email.

&----


*& Form send_email

&----


  • text

----


FORM send_email .

  • Local data declaration

DATA: l_email_doc TYPE sodocchgi1, "Email document

i_objpack TYPE STANDARD TABLE OF sopcklsti1 INITIAL SIZE 0,

i_objtext TYPE STANDARD TABLE OF solisti1 INITIAL SIZE 0,

i_receivers TYPE STANDARD TABLE OF somlreci1 INITIAL SIZE 0,

wa_objpack TYPE sopcklsti1, "Email structure

wa_objtext TYPE solisti1, "Content

l_tab_lines LIKE sy-tabix, "No. of lines

l_wa_receivers TYPE somlreci1. "Receiver list

*Email description

l_email_doc-obj_name = 'ERROR LOG'.

l_email_doc-obj_descr = 'Error records'(025).

  • Attachment header

wa_objtext-line+0 = 'Field 1'.

wa_objtext-line+12 = 'Field 2'.

wa_objtext-line+24 = 'Field 3'.

APPEND wa_objtext TO i_objtext.

CLEAR wa_objtext.

  • Blank line

APPEND wa_objtext TO i_objtext.

  • Attacment content

LOOP AT itab into wa.

wa_objtext-line+0 = wa-x.

wa_objtext-line+12 = wa-y.

wa_objtext-line+24 = wa-z.

APPEND wa_objtext TO i_objtext.

CLEAR wa_objtext.

ENDLOOP.

DESCRIBE TABLE i_objtext LINES l_tab_lines.

  • Creating the entry for the compressed document

wa_objpack-transf_bin = ' ' .

wa_objpack-head_start = 1.

wa_objpack-head_num = 0.

wa_objpack-body_start = 1.

wa_objpack-body_num = 0.

wa_objpack-doc_type = 'RAW'.

wa_objpack-obj_name = ' '.

wa_objpack-obj_descr = ' '.

wa_objpack-obj_langu = ' '.

wa_objpack-doc_size = ' '.

APPEND wa_objpack TO i_objpack.

CLEAR wa_objpack.

  • Line 2

wa_objpack-transf_bin = 'X'.

wa_objpack-head_start = 1.

wa_objpack-head_num = 0.

wa_objpack-body_start = 1.

wa_objpack-body_num = l_tab_lines.

wa_objpack-doc_type = 'RAW'.

wa_objpack-obj_name = 'Attachment'(011).

wa_objpack-obj_descr = 'Error_File'(012).

wa_objpack-obj_langu = ' '.

wa_objpack-doc_size = l_tab_lines * 255.

APPEND wa_objpack TO i_objpack.

  • Receipient information

l_wa_receivers-receiver = s_rcvr-low.

l_wa_receivers-rec_type = 'A'.

  • l_wa_receivers-express = 'X'. "Express message type

APPEND l_wa_receivers TO i_receivers.

  • Send mail

CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1' "#EC *

EXPORTING

document_data = l_email_doc

put_in_outbox = 'X'

commit_work = 'X'

TABLES

packing_list = i_objpack

contents_bin = i_objtext

receivers = i_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.

ENDIF.

ENDFORM. "send_email