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: 

How to send an internal table as an attachement to a mail.

Former Member
0 Kudos

hi all,

Can any one provide any sol for this,

How to send an internal table as an attachement to a mail.

Thanks in advance.

4 REPLIES 4

Former Member
0 Kudos

Hi

see this sample code

Mailing with Attachment by ABAP Coding

Refer this link:

FORM send_list_to_basis .

DATA: w_path LIKE rlgrap OCCURS 0 WITH HEADER LINE,

lt_index TYPE sy-tabix,

doc_type(3) TYPE c,

descr LIKE it_objpack_basis-obj_descr,

temp_data LIKE w_path,

temp1 TYPE string,

tab_lines TYPE i,

langu(15) TYPE c,

expirydate TYPE so_obj_edt,

L_FILE1(100).

CONCATENATE 'C:\' sy-repid '_' sy-datum '.XLS' INTO L_FILE1.

W_PATH-FILENAME = L_FILE1.

APPEND w_path.

CLEAR w_path.

wa_doc_chng-obj_descr = 'User List not logged on for 180 days'.

wa_doc_chng-obj_langu = 'E'.

wa_doc_chng-obj_expdat = sy-datum.

CLEAR w_subject.

CONCATENATE 'Please find attached document with list of users'

'not logged on for 180 days for client' sy-mandt

INTO w_subject SEPARATED BY space.

it_objtxt_basis-line = w_subject.

APPEND it_objtxt_basis.

CLEAR it_objtxt_basis.

it_objtxt_basis-line = text-004.

APPEND it_objtxt_basis.

CLEAR it_objtxt_basis.

CLEAR w_tab_line.

DESCRIBE TABLE it_objtxt_basis LINES w_tab_line.

READ TABLE it_objtxt_basis INDEX w_tab_line INTO l_cline.

wa_doc_chng-doc_size =

( w_tab_line - 1 ) * 255 + STRLEN( l_cline ).

CLEAR it_objpack_basis-transf_bin.

it_objpack_basis-head_start = 1.

it_objpack_basis-head_num = 0.

it_objpack_basis-body_start = 1.

it_objpack_basis-body_num = w_tab_line.

it_objpack_basis-doc_type = 'RAW'.

APPEND it_objpack_basis.

CLEAR it_objpack_basis.

LOOP AT w_path.

temp1 = w_path.

descr = w_path.

CALL FUNCTION 'STRING_REVERSE'

EXPORTING

string = descr

lang = 'E'

IMPORTING

rstring = descr.

CALL FUNCTION 'STRING_SPLIT'

EXPORTING

delimiter = '\'

string = descr

IMPORTING

head = descr

tail = temp_data.

CALL FUNCTION 'STRING_REVERSE'

EXPORTING

string = descr

lang = 'E'

IMPORTING

rstring = descr.

CALL FUNCTION 'STRING_SPLIT'

EXPORTING

delimiter = '.'

string = descr

IMPORTING

head = temp_data

tail = doc_type.

CALL FUNCTION 'GUI_UPLOAD'

EXPORTING

filename = temp1

filetype = 'BIN'

header_length = 0

read_by_line = 'X'

replacement = '#'

TABLES

data_tab = it_upload.

DESCRIBE TABLE it_upload LINES tab_lines.

DESCRIBE TABLE it_objbin_basis LINES lt_index.

lt_index = lt_index + 1.

LOOP AT it_upload.

wa_objbin_basis-line = it_upload-line.

APPEND wa_objbin_basis TO it_objbin_basis.

CLEAR wa_objbin_basis.

ENDLOOP.

it_objpack_basis-transf_bin = 'X'.

it_objpack_basis-head_start = 0.

it_objpack_basis-head_num = 0.

it_objpack_basis-body_start = lt_index.

it_objpack_basis-body_num = tab_lines.

it_objpack_basis-doc_type = doc_type.

it_objpack_basis-obj_descr = descr.

it_objpack_basis-doc_size = tab_lines * 255.

APPEND it_objpack_basis.

CLEAR it_objpack_basis.

ENDLOOP.

it_reclist_basis-receiver = 'XXX@.com'.

it_reclist_basis-rec_type = 'U'.

APPEND it_reclist_basis.

CLEAR it_reclist_basis.

CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'

EXPORTING

document_data = wa_doc_chng

put_in_outbox = 'X'

commit_work = 'X'

TABLES

packing_list = it_objpack_basis

contents_txt = it_objtxt_basis

contents_bin = it_objbin_basis

receivers = it_reclist_basis

EXCEPTIONS

too_many_receivers = 1

document_not_sent = 2

operation_no_authorization = 4

OTHERS = 99.

IF sy-subrc EQ 0.

SUBMIT rsconn01 WITH mode = 'INT' AND RETURN.

ENDIF.

ENDFORM. " send_list_to_basis

Reward points for useful Answers

Regards

Anji

Former Member
0 Kudos

Hi

You can download the contents of the internal table into a file and you can attach this file and mail it using FM <b>SO_NEW_DOCUMENT_ATT_SEND_API1</b> .

Regards

Raj

Former Member
0 Kudos

hi

go to Wiki link in abap.

click on the link Abap General

see the 3rd last link

regards

ravish

reward if useful

Former Member
0 Kudos

Hi venkata

I wrote I program recently to do the same be low is the code (it is only the form statements and data statements)

Basically how it works the email program has a table “documents” this table contains another table “content_text” where you enter the data you want to display. The first entry in the table “documents” will be body text of the email. And any additional entries will be added as an attachment.

This code was derived from the following post. You can also use this for more information.

/people/thomas.jung3/blog/2004/09/08/sending-e-mail-from-abap--version-610-and-higher--bcs-interface

&----


*& Include ZNOTIFICATIONS_BGJ_TOP Report ZNOTIFICATIONS_BGJ

*&

&----


REPORT ZNOTIFICATIONS_BGJ.

*----


*Tables

*----


Tables: hrp1000, pa0105.

*----


*Types

*----


Types: Begin of ty_quali,

pernr like pa0001-pernr,

Vorna like pa0002-vorna,

nachn like pa0002-nachn,

objid like hrp1000-objid,

stext like hrp1000-stext,

orgeh like hrp1000-stext,

endda like hrp1001-endda,

email like pa0105-USRID_LONG,

end of ty_quali.

Types : begin of ty_email_docs,

type type SO_OBJ_TP,

subject type so_obj_des,

content_text type soli_tab,

content_hex type solix_tab,

end of ty_email_docs.

Types : begin of ty_recip,

uname type sy-uname,

C_Address type ad_smtpadr,

I_Express type os_boolean,

i_copy type os_boolean,

I_Blind_copy type os_boolean,

I_NO_foward type os_boolean,

end of ty_recip.

*----


*DATA

*----


data : ok_code like sy-ucomm.

  • -----General ------

data : from_date like p0001-endda,

to_date like p0001-endda.

data : it_quali type standard table of zqualifications,

wa_quali like line of it_quali.

  • ----- Email -------

data : requested_status type bcs_rqst,

documents type ty_email_docs occurs 0 with header line,

recipients type ty_recip occurs 0 with header line.

  • ----- ALV -------

data : it_notifications type standard table of ty_quali,

wa_notifications like line of it_notifications.

Data : wv_first_display type i value 1.

DATA : wo_container TYPE ref TO cl_gui_custom_container,

wo_grid TYPE ref TO cl_gui_alv_grid.

DATA : wt_exclude TYPE ui_functions,

ws_exclude TYPE ui_func.

DATA : wt_fieldcat TYPE lvc_t_fcat,

ws_fieldcat LIKE LINE OF wt_fieldcat.

DATA : ws_layout TYPE lvc_s_layo.

&----


*& Form SEND_MAIL

&----


  • text

----


FORM SEND_MAIL.

----


  • CLASS-DEFINITIONS *

----


DATA: send_request TYPE REF TO cl_bcs.

DATA: document TYPE REF TO cl_document_bcs.

DATA: sender TYPE REF TO cl_sapuser_bcs.

DATA: recipient TYPE REF TO if_recipient_bcs.

DATA: exception_info TYPE REF TO if_os_exception_info,

bcs_exception TYPE REF TO cx_bcs.

----


  • INTERNAL TABLES *

----


DATA: l_mailtext TYPE soli_tab.

DATA: l_mailhex TYPE solix_tab.

DATA: iaddsmtp TYPE bapiadsmtp OCCURS 0 WITH HEADER LINE.

DATA: ireturn TYPE bapiret2 OCCURS 0 WITH HEADER LINE.

----


  • VARIABLES *

----


DATA: mail_line LIKE LINE OF l_mailtext.

DATA: mailx_line LIKE LINE OF l_mailhex.

DATA: bapiadsmtp TYPE bapiadsmtp.

----


  • CONSTANTS *

----


CONSTANTS:

kimball_domain(12) TYPE c VALUE '@kimball.com'.

CLASS cl_cam_address_bcs DEFINITION LOAD.

CLASS cl_abap_char_utilities DEFINITION LOAD.

TRY.

  • Create persistent send request

send_request = cl_bcs=>create_persistent( ).

DATA: first(1) TYPE c.

CLEAR first.

DATA: documents_line LIKE LINE OF documents.

LOOP AT documents INTO documents_line.

IF first IS INITIAL.

MOVE 'X' TO first.

  • Build the Main Document

IF documents_line-content_hex[] IS INITIAL.

document = cl_document_bcs=>create_document(

i_type = documents_line-type

i_text = documents_line-content_text

i_subject = documents_line-subject ).

ELSE.

document = cl_document_bcs=>create_document(

i_type = documents_line-type

i_hex = documents_line-content_hex

i_subject = documents_line-subject ).

ENDIF.

ELSE.

IF documents_line-content_hex[] IS INITIAL.

  • Add Attachment

CALL METHOD document->add_attachment

EXPORTING

i_attachment_type = documents_line-type

i_attachment_subject = documents_line-subject

i_att_content_text = documents_line-content_text.

ELSE.

CALL METHOD document->add_attachment

EXPORTING

i_attachment_type = documents_line-type

i_attachment_subject = documents_line-subject

i_att_content_hex = documents_line-content_hex.

ENDIF.

ENDIF.

ENDLOOP.

  • Add document to send request

CALL METHOD send_request->set_document( document ).

  • Get sender object

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

  • Add sender

CALL METHOD send_request->set_sender

EXPORTING

i_sender = sender.

DATA: recipients_line LIKE LINE OF recipients.

LOOP AT recipients INTO recipients_line.

IF recipients_line-c_address IS INITIAL.

  • Create recipient

CLEAR iaddsmtp.

REFRESH iaddsmtp.

CLEAR bapiadsmtp.

CLEAR recipient.

  • Read the E-Mail address for the user

CALL FUNCTION 'BAPI_USER_GET_DETAIL'

EXPORTING

username = recipients_line-uname

TABLES

return = ireturn

addsmtp = iaddsmtp.

LOOP AT iaddsmtp WHERE std_no = 'X'.

CLEAR bapiadsmtp.

MOVE iaddsmtp TO bapiadsmtp.

ENDLOOP.

  • If no E-mail address was found, create one.

IF bapiadsmtp-e_mail = ''.

CONCATENATE recipients_line-uname kimball_domain

INTO recipients_line-c_address.

ELSE.

MOVE bapiadsmtp-e_mail TO recipients_line-c_address.

ENDIF.

ENDIF.

recipient = cl_cam_address_bcs=>create_internet_address( recipients_line-c_address ).

  • Add recipient with its respective attributes to send request

CALL METHOD send_request->add_recipient

EXPORTING

i_recipient = recipient

i_express = recipients_line-i_express

i_copy = recipients_line-i_copy

i_blind_copy = recipients_line-i_blind_copy

i_no_forward = recipients_line-i_no_foward.

ENDLOOP.

  • Set that you don't need a Return Status E-mail

DATA: status_mail TYPE bcs_stml.

status_mail = requested_status.

CALL METHOD send_request->set_status_attributes

EXPORTING

i_requested_status = requested_status

i_status_mail = status_mail.

  • set send immediately flag

send_request->set_send_immediately( 'X' ).

  • Send document

CALL METHOD send_request->send( ).

COMMIT WORK.

  • CATCH cx_bcs INTO bcs_exception.

  • RAISE EXCEPTION bcs_exception.

ENDTRY.

ENDFORM. " SEND_MAIL

&----


*& Form get_recipients

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM get_recipients .

data : rec_line like line of recipients.

Clear : recipients, rec_line.

refresh recipients.

if p_crec <> 'X'.

  • collect the recipient data from database

DAta : it_recip like standard TABLE OF ZRECIPIENTS,

wa_recip like line of it_recip.

  • Select the data from database

select * from ZRECIPIENTS into table it_recip.

  • Loop the internal table and add the entries to the recipient list

loop at it_recip into wa_recip.

data check_mail like pa0105-USRID_LONG.

  • Check if the email address data changed

Select single USRID_LONG from pa0105 into check_mail

where pernr = wa_recip-pernr

and subty = '0010'.

if check_mail <> wa_recip-EMAIL and check_mail is not initial.

wa_recip-email = check_mail.

update ZRECIPIENTS set email = check_mail where pernr = wa_recip-pernr.

endif.

if wa_recip-email is not initial.

  • rec_line-uname = sy-uname.

rec_line-C_Address = wa_recip-email.

rec_line-I_Express = 'X'.

append rec_line to recipients.

endif.

endloop.

Else.

  • get the override emails addresses

loop at p_email.

if p_email-low is not initial.

rec_line-C_Address = p_email-low.

rec_line-I_Express = 'X'.

append rec_line to recipients.

endif.

if p_email-high is not initial.

rec_line-C_Address = p_email-high.

rec_line-I_Express = 'X'.

append rec_line to recipients.

endif.

endloop.

endif.

ENDFORM. " get_recipients

&----


*& Form Biuld_Email

&----


  • text

----


  • --> p1 text

  • <-- p2 text

----


FORM Biuld_Email .

data : temp_line like line of documents,

body_line like line of temp_line-content_text.

Data : temp_date1(10) type c,

temp_date2(10) type c.

    • Subject and type

temp_line-type = 'RAW'.

temp_line-subject = 'Notifications for Expiring Qualifications '.

    • Mail Body text

body_line-line = 'Attached is a list of Qualifications that expires within the following dates.'.

append body_line to temp_line-content_text.

concatenate from_Date6(2) '-' from_Date4(2) '-' from_date(4) into temp_date1.

concatenate to_date6(2) '-' to_date4(2) '-' to_date(4) into temp_date2.

Concatenate 'From' temp_date1 'To' temp_date2 into body_line-line separated by space.

append body_line to temp_line-content_text.

body_line-line = 'Please make the necessary precautions.'.

append body_line to temp_line-content_text.

body_line-line = ''.

append body_line to temp_line-content_text.

body_line-line = 'Regards'.

append body_line to temp_line-content_text.

append temp_line to documents.

Clear : body_line,temp_line..

    • Attachment

temp_line-type = 'RAW'.

Concatenate sy-datum6(2) '-' sy-datum4(2) '-' sy-datum(4) into temp_date1.

Concatenate 'Notifications List' temp_date1 into temp_line-subject separated by space.

Data : temp_name(45) type c,

temp_name2(45) type c,

temp_name3(45) type c,

sepa(35) type c,

sepa2(35) type c,

sepa3(35) type c.

data off type i.

    • Get the heading

body_line-line = '============================================================================================================================================='.

append body_line to temp_line-content_text.

body_line-line = '|Employee number |Name |Qualification number |Qualification Name |Organizational unit |Date of Expiry|'.

append body_line to temp_line-content_text.

body_line-line = '============================================================================================================================================='.

append body_line to temp_line-content_text.

if it_notifications is initial.

body_line-line = '| The list contains no data |'.

append body_line to temp_line-content_text.

endif.

loop at it_notifications into wa_notifications.

    • Get the body

    • Construct the name with spaces

sepa = ' |'.

Concatenate wa_notifications-Vorna wa_notifications-nachn '*&^%' into temp_name separated by space.

Find '*&^%' in temp_name Match offset off.

temp_name = temp_name(off).

if off > 2.

Shift sepa by off places.

else.

Shift sepa by 1 places.

endif.

    • Construct the qualification name

sepa2 = ' |'.

Concatenate wa_notifications-STEXT '*&^%' into temp_name2.

Find '*&^%' in temp_name2 Match offset off.

if off > 0.

temp_name2 = temp_name2(off).

else.

temp_name2 = wa_notifications-STEXT.

endif.

if off > 2.

Shift sepa2 by off places.

endif.

    • Construct the Org unit

sepa3 = ' |'.

Concatenate wa_notifications-ORGEH '*&^%' into temp_name3.

Find '*&^%' in temp_name3 Match offset off.

if off > 0.

temp_name3 = temp_name3(off).

else.

temp_name3 = wa_notifications-ORGEH.

endif.

if off > 2.

Shift sepa3 by off places.

endif.

concatenate '|' wa_notifications-pernr ' |' temp_name sepa

wa_notifications-objid ' |' temp_name2 sepa2

temp_name3 sepa3

wa_notifications-endda ' |' into body_line-line.

append body_line to temp_line-content_text.

endloop.

body_line-line = '----


'.

append body_line to temp_line-content_text.

append temp_line to documents.

requested_status = 'N'.

ENDFORM. " Biuld_Email

<a href="/people/thomas.jung3/blog/2004/09/08/sending-e-mail-from-abap--version-610-and-higher--bcs-interface:///people/thomas.jung3/blog/2004/09/08/sending-e-mail-from-abap--version-610-and-higher--bcs-interface href="/people/thomas.jung3/blog/2004/09/08/sending-e-mail-from-abap--version-610-and-higher--bcs-interface:///people/thomas.jung3/blog/2004/09/08/sending-e-mail-from-abap--version-610-and-higher--bcs-interface