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

how to attach & send mail (Excel)

Former Member
0 Likes
3,570

i want to attach below data in excel and send mail.

IT_DATA-COL1 = 'Action'.

APPEND IT_DATA.

CLEAR IT_DATA.

IT_DATA-COL1 = V_DATE.

APPEND IT_DATA.

CLEAR IT_DATA.

IT_DATA-COL1 = 'Execution '.

IT_DATA-COL2 = sy-datum.

APPEND IT_DATA.

IT_DATA-COL1 = 'No. of pieces = '.

IT_DATA-COL2 = L_NUM.

APPEND IT_DATA.

CLEAR IT_DATA.

APPEND IT_DATA.

CLEAR IT_DATA.

APPEND IT_DATA.

APPEND IT_DATA.

IT_DATA-COL1 = 'Number'.

IT_DATA-COL2 = 'VIp'.

IT_DATA-COL3 = 'Key '.

IT_DATA-COL4 = 'Brakery'.

IT_DATA-COL5 = 'No'.

IT_DATA-COL6 = 'State'.

IT_DATA-COL7 = 'Date'.

IT_DATA-COL8 = ' Date'.

APPEND IT_DATA.

CLEAR IT_DATA.

APPEND IT_DATA.

LOOP AT IT.

IT_DATA-COL1 = IT-PROP.

IT_DATA-COL2 = IT-VIp.

IT_DATA-COL3 = KEY.

IT_DATA-COL4 = IT-PLATE..

IT_DATA-COL5 = V_REQUESTOR.

IT_DATA-COL6 = V_STATUS .

IT_DATA-COL7= Date .

IT_DATA-COL8= DATE.

APPEND IT_DATA.

CLEAR IT_DATA.

ENDLOOP.

APPEND IT_DATA.

APPEND IT_DATA.

ENDIF.

i want to attach below data in excel and send mail.

IT_DATA-COL1 = 'Action'.

APPEND IT_DATA.

CLEAR IT_DATA.

IT_DATA-COL1 = V_DATE.

APPEND IT_DATA.

CLEAR IT_DATA.

IT_DATA-COL1 = 'Execution '.

IT_DATA-COL2 = sy-datum.

APPEND IT_DATA.

IT_DATA-COL1 = 'No. of pieces = '.

IT_DATA-COL2 = L_NUM.

APPEND IT_DATA.

CLEAR IT_DATA.

APPEND IT_DATA.

CLEAR IT_DATA.

APPEND IT_DATA.

APPEND IT_DATA.

IT_DATA-COL1 = 'Number'.

IT_DATA-COL2 = 'VIp'.

IT_DATA-COL3 = 'Key '.

IT_DATA-COL4 = 'Brakery'.

IT_DATA-COL5 = 'No'.

IT_DATA-COL6 = 'State'.

IT_DATA-COL7 = 'Date'.

IT_DATA-COL8 = ' Date'.

APPEND IT_DATA.

CLEAR IT_DATA.

APPEND IT_DATA.

LOOP AT IT.

IT_DATA-COL1 = IT-PROP.

IT_DATA-COL2 = IT-VIp.

IT_DATA-COL3 = KEY.

IT_DATA-COL4 = IT-PLATE..

IT_DATA-COL5 = V_REQUESTOR.

IT_DATA-COL6 = V_STATUS .

IT_DATA-COL7= Date .

IT_DATA-COL8= DATE.

APPEND IT_DATA.

CLEAR IT_DATA.

ENDLOOP.

APPEND IT_DATA.

APPEND IT_DATA.

ENDIF.

7 REPLIES 7
Read only

Former Member
0 Likes
2,164

Hi,

Go through the link to find how to send attachment as email.

Hope it helps you.

Regards,

Rajesh Kumar

Read only

Former Member
0 Likes
2,164

Hi,

Please go through the sample code below,



REPORT  ZBC_ITAB_TO_EXCEL_NEW                   .

CLASS: cl_abap_char_utilities DEFINITION LOAD.

DATA: docdata LIKE sodocchgi1,
      objpack LIKE sopcklsti1 OCCURS 1 WITH HEADER LINE,
      objtxt LIKE solisti1 OCCURS 10 WITH HEADER LINE,
      objbin1 LIKE solisti1 OCCURS 10 WITH HEADER LINE,
      objbin2 LIKE solisti1 OCCURS 10 WITH HEADER LINE,
      objbin_final LIKE solisti1 OCCURS 10 WITH HEADER LINE,
      reclist LIKE somlreci1 OCCURS 1 WITH HEADER LINE,
      tab_lines TYPE sy-tabix.

DATA: gd_sender_type LIKE soextreci1-adr_typ.

DATA: c_tab TYPE c VALUE cl_abap_char_utilities=>horizontal_tab,
      c_ret TYPE c VALUE cl_abap_char_utilities=>cr_lf.

DATA: c_dev TYPE sy-sysid.

DATA: BEGIN OF i_data OCCURS 0,
      a(20),
      b(20),
      END OF i_data.

DATA: BEGIN OF st,
      f1(2) TYPE c,
      f2(2) TYPE n,
      END OF st.

DATA: itab1 LIKE TABLE OF st WITH HEADER LINE,
      itab2 LIKE TABLE OF st WITH HEADER LINE.


DATA: n TYPE i.

PARAMETER: p_email1 LIKE somlreci1-receiver
                    DEFAULT 'email address',
           p_sender LIKE somlreci1-receiver.

START-OF-SELECTION.

  itab1-f1 = 'AA'. itab1-f2 = '01'. APPEND itab1.
  itab1-f1 = 'BB'. itab1-f2 = '02'. APPEND itab1.
  itab1-f1 = 'CC'. itab1-f2 = '03'. APPEND itab1.

  itab2-f1 = 'ZZ'. itab2-f2 = '26'. APPEND itab2.
  itab2-f1 = 'YY'. itab2-f2 = '25'. APPEND itab2.

  LOOP AT itab1.
    CONCATENATE itab1-f1 itab1-f2 INTO objbin1 separated BY c_tab.
    CONCATENATE c_ret objbin1 INTO objbin1.
    APPEND objbin1.
  ENDLOOP.

  LOOP AT itab2.
    CONCATENATE itab2-f1 itab2-f2 INTO objbin2 separated BY c_tab.
    CONCATENATE c_ret objbin2 INTO objbin2.
    APPEND objbin2.
  ENDLOOP.

  LOOP AT objbin1.
    MOVE objbin1-line TO objbin_final-line.
    APPEND objbin_final.
  ENDLOOP.

  LOOP AT objbin2.
    MOVE objbin2-line TO objbin_final-line.
    APPEND objbin_final.
  ENDLOOP.

  PERFORM process_email.
  c_dev = sy-sysid.

  IF sy-sysid = c_dev.
    wait up to 5 seconds.
    SUBMIT rsconn01 WITH mode = 'INT'
    WITH output = 'X'
    AND RETURN.
  ENDIF.

  IF sy-subrc = 0.
    WRITE: / 'Email succesfilly delivered'.
  ELSE.
    WRITE: / 'failure'.
  ENDIF.

FORM process_email.

  IF p_sender EQ space.
    gd_sender_type = space.
  ELSE.
    gd_sender_type = 'INT'.
  ENDIF.

*Body
  docdata-obj_name = 'Mail_Excel_File'.
  docdata-obj_descr = 'Excel file attachment'.
  objtxt = 'Attached is the sample Excel file'.
  APPEND objtxt.
  DESCRIBE TABLE objtxt LINES tab_lines.
  READ TABLE objtxt INDEX tab_lines.
  docdata-doc_size = ( tab_lines - 1 ) * 255 + strlen( objtxt ).

  CLEAR objpack-transf_bin.
  objpack-head_start = 1.
  objpack-head_num = 0.
  objpack-body_start = 1.
  objpack-body_num = tab_lines.
  objpack-doc_type = 'RAW'.
  APPEND objpack.

*Attachment
  n = 1.

  DESCRIBE TABLE objbin1 LINES tab_lines.
  objpack-doc_size = tab_lines * 255.
  objpack-transf_bin = 'X'.
  objpack-head_start = 1.
  objpack-head_num = 1.
  objpack-body_start = n.
  objpack-body_num = tab_lines.
  objpack-doc_type = 'XLS'.
  docdata-obj_name = 'Excel_File_Attachment1'.
  objpack-obj_descr = 'Excel File Attachment1'.
  APPEND objpack.

  n = n + tab_lines.

  DESCRIBE TABLE objbin2 LINES tab_lines.
  objpack-doc_size = tab_lines * 255.
  objpack-transf_bin = 'X'.
  objpack-head_start = 1.
  objpack-head_num = 1.
  objpack-body_start = n.
  objpack-body_num = tab_lines.
  objpack-doc_type = 'XLS'.
  docdata-obj_name = 'Excel_File_Attachment2'.
  objpack-obj_descr = 'Excel File Attachment2'.
  APPEND objpack.


*Create the list of recipients
  reclist-receiver = p_email1.
  reclist-rec_type = 'U'.
  reclist-express = 'X'.
  APPEND reclist.


*Send the e-mail
  CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
    EXPORTING
      document_data              = docdata
      put_in_outbox              = 'X'
      commit_work                = 'X'
    TABLES
      packing_list               = objpack
      contents_bin               = objbin_final
      contents_txt               = objtxt
      receivers                  = reclist
    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.

  COMMIT WORK.
ENDFORM.                    "process_email

Hope it helps

Regards

Mansi

Read only

Former Member
0 Likes
2,164

Hi Karunya ,

Here are some useful links.....

Regards.

Read only

Former Member
0 Likes
2,164

Hi, use the folowing code for mail attachment ..

CONSTANTS: lc_tab TYPE x VALUE '09', lc_ret TYPE x VALUE '0D'.

  • TRANSFER fieldnames to mail content

CONCATENATE text-004 text-005 text-006 text-007 INTO l_string SEPARATED BY lc_tab.

CONCATENATE l_string lc_ret INTO l_string.

MOVE l_string TO lx_objtxt-line.

APPEND lx_objtxt TO li_objtxt.

CLEAR: l_string, lx_objtxt.

  • TRANSFER output to mail content

LOOP AT p_i_final INTO x_final.

CONCATENATE x_final-bukrs x_final-vkorg x_final-vbeln x_final-matnr INTO l_string SEPARATED BY lc_tab.

CONCATENATE l_string lc_ret INTO l_string.

MOVE l_string TO lx_objtxt-line.

APPEND lx_objtxt TO li_objtxt.

ENDLOOP.

lx_objhead-line = 'MAIL.xls'.

APPEND lx_objhead TO li_objhead.

  • MAIL Subject and description

lx_docdata-obj_name = 'MAIL'. lx_docdata-obj_descr = 'Test mail'.

  • CREATE Message body

DESCRIBE TABLE li_objtxt LINES l_tablines.

READ TABLE li_objtxt INTO lx_objtxt INDEX l_tablines.

lx_docdata-doc_size = ( l_tablines - 1 ) * 255 + STRLEN( lx_objtxt-line ).

CLEAR lx_objpack-transf_bin.

lx_objpack-head_start = 1.

lx_objpack-head_num = 0.

lx_objpack-body_start = 1.

lx_objpack-body_num = l_tablines.

lx_objpack-doc_type = 'XLS'.

APPEND lx_objpack TO li_objpack.

  • FOR Attachment

li_objbin[] = li_objtxt[].

DESCRIBE TABLE li_objbin LINES l_tablines.

READ TABLE li_objbin INTO lx_objbin INDEX l_tablines.

lx_objpack-transf_bin = c_x.

lx_objpack-head_start = 1.

lx_objpack-head_num = 1.

lx_objpack-body_start = 1.

lx_objpack-body_num = l_tablines.

lx_objpack-doc_type = 'XLS'.

lx_objpack-obj_name = 'ATTACHMENT'.

lx_objpack-obj_descr = 'MAIL'.

lx_objpack-doc_size = ( 255 * ( l_tablines - 1 ) ) + STRLEN( lx_objbin ).

APPEND lx_objpack TO li_objpack.

CLEAR lx_objpack.

  • ADD List of recipients

LOOP AT p_addr INTO s_addr.

lx_reclist-receiver = s_addr-low.

lx_reclist-rec_type = 'U'.

lx_reclist-rec_date = sy-datum.

lx_reclist-express = c_x.

lx_reclist-com_type = 'INT'.

lx_reclist-notif_del = c_x.

lx_reclist-notif_read = c_x.

APPEND lx_reclist TO li_reclist.

ENDLOOP.

  • SEND E-Mail with attachmment

CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'

EXPORTING

document_data = lx_docdata

TABLES

packing_list = li_objpack

object_header = li_objhead

contents_bin = li_objbin

contents_txt = li_objtxt

receivers = li_reclist

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.

Read only

Former Member
0 Likes
2,164

Hello,

Until then it's fine, but you got to make up a simple program to send email..

REPORT  zahack_mail_simple.
*--------------------------------------------------------*
*  Mail related declarations
*--------------------------------------------------------*
"Variables
DATA :
     g_sent_to_all   TYPE sonv-flag,
     g_tab_lines     TYPE i.
"Types
TYPES:
     t_document_data  TYPE  sodocchgi1,
     t_packing_list   TYPE  sopcklsti1,
     t_attachment     TYPE  solisti1,
     t_body_msg       TYPE  solisti1,
     t_receivers      TYPE  somlreci1.
"Workareas
DATA :
     w_document_data  TYPE  t_document_data,
     w_packing_list   TYPE  t_packing_list,
     w_attachment     TYPE  t_attachment,
     w_body_msg       TYPE  t_body_msg,
     w_receivers      TYPE  t_receivers.
"Internal Tables
DATA :
     i_document_data  TYPE STANDARD TABLE OF t_document_data,
     i_packing_list   TYPE STANDARD TABLE OF t_packing_list,
     i_attachment     TYPE STANDARD TABLE OF t_attachment,
     i_body_msg       TYPE STANDARD TABLE OF t_body_msg,
     i_receivers      TYPE STANDARD TABLE OF t_receivers.

*--------------------------------------------------------*
"start-of-selection.
*--------------------------------------------------------*
START-OF-SELECTION.

  PERFORM send_mail.

*&---------------------------------------------------------------------*
  "Form  send_mail
  "---------------
  " PACKING LIST table requires information about how the data in the
  " tables OBJECT_HEADER, CONTENTS_BIN and CONTENTS_TXT are to be
  " distributed to the documents and its attachments. The first row is
  " for the document, the following rows are each for one attachment.
*&---------------------------------------------------------------------*
FORM send_mail .

  "Subject of the mail.
  w_document_data-obj_name  = 'MAIL_TO_HEAD'.
  w_document_data-obj_descr = 'Simple mail using SAP ABAP'.

  "Body of the mail
  PERFORM build_body_of_mail
    USING:space,
          'Hi,',
          'I am fine. How are you? How are you doing ? ',
          'This program has been created to send simple mail',
          'with Subject,Body with Address of the sender. ',
          'Thanks and Regards,',
          'Venkat.O,',
          'SAP HR Technical Consultant.'.

  "Write Packing List (Body)
  DESCRIBE TABLE i_body_msg LINES g_tab_lines.
  w_packing_list-head_start = 1.
  w_packing_list-head_num   = 0.
  w_packing_list-body_start = 1.
  w_packing_list-body_num   = g_tab_lines.
  w_packing_list-doc_type   = 'RAW'.
  APPEND w_packing_list TO i_packing_list.
  CLEAR  w_packing_list.

  "Fill the document data and get size of attachment
  READ TABLE i_body_msg INTO w_body_msg INDEX g_tab_lines.
  w_document_data-doc_size = ( g_tab_lines - 1 ) * 255 + STRLEN( w_body_msg ).

  "Receivers List.
  w_receivers-rec_type   = 'U'.  "Internet address
  w_receivers-receiver   = '<email id of receiver>'.
  w_receivers-com_type   = 'INT'.
  w_receivers-notif_del  = 'X'.
  w_receivers-notif_ndel = 'X'.
  APPEND w_receivers TO i_receivers .
  CLEAR:w_receivers.

  "Function module to send mail to Recipients
  CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
    EXPORTING
      document_data              = w_document_data
      put_in_outbox              = 'X'
      commit_work                = 'X'
    IMPORTING
      sent_to_all                = g_sent_to_all
    TABLES
      packing_list               = i_packing_list
      contents_txt               = i_body_msg
      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 .
    MESSAGE i303(me) WITH 'Mail has been Successfully Sent.'.
  ELSE.
    WAIT UP TO 2 SECONDS.
    SUBMIT rsconn01 WITH mode = 'INT'
                 WITH output = 'X'
                 AND RETURN.
  ENDIF.

ENDFORM.                    " send_mail
*&---------------------------------------------------------------------*
"      Form  build_body_of_mail
*&---------------------------------------------------------------------*
FORM build_body_of_mail  USING l_message.

  w_body_msg = l_message.
  APPEND w_body_msg TO i_body_msg.
  CLEAR  w_body_msg.

ENDFORM.                    " build_body_of_mail

Read only

Former Member
0 Likes
2,164

Hi Karunay,

1. To get your data from internal table to Excel file you a Function Module 'SAP_CONVERT_TO_XLS_FORMAT'

2. To attach this file as an attachment in a mail use FM ''SO_DOCUMENT_REPOSITORY_MANAGER' in this pass the file and other details of sender and receiver.

Revert back if you want the related code for SO_DOCUMENT_REPOSITORY_MANAGER.

Regards,

Sana.

Read only

Former Member
0 Likes
2,164

Hi,

By the following method you can send email even if the no. of charactes in a line exceeds 255.

Here I am converting the data to HTML and later sending it in Xcel. This worked for me.

form email_lgcytrans .

data: lv_document_data like sodocchgi1,

lv_tab_lines like sy-tabix,

li_body type standard table of solisti1,

lv_body type solisti1,

lv_html like line of i_html,

li_attach type standard table of solisti1,

lv_attach type solisti1.

----


  • CONSTANTS:

----


constants: lc_filetype(3) type c value 'XLS',

lc_emaildocname like sodocchgi1-obj_name

value 'Legacy Trans',

lc_subject like sodocchgi1-obj_descr value

'Discrepant Transactions',

lc_filename like solisti1

value 'HG-DTL DISCREPANT TRANS'.

if i_row_head[] is initial.

perform get_column_headers.

  • Get the HTML Formatted Data

call function 'WWW_ITAB_TO_HTML'

exporting

table_header = v_html_head

tables

html = i_html

fields = i_fields

row_header = i_row_head

itable = i_dscpren.

form get_column_headers.

data : lv_fieldcat type slis_fieldcat_alv,

lv_text type w3head-text,

lv_tabix type i.

loop at i_fieldcat into lv_fieldcat.

lv_tabix = lv_tabix + 1.

  • Get Fieldname

clear lv_text.

lv_text = lv_fieldcat-seltext_l.

  • Prepare Header Row

call function 'WWW_ITAB_TO_HTML_HEADERS'

exporting

field_nr = lv_tabix

text = lv_text

fgcolor = 'black'

bgcolor = 'cccccc'

font = 'Tahoma'

tables

header = i_row_head.

endloop.

endform. " GET_COLUMN_HEADERS

form send_email tables li_attach

ls_ifmail

li_body

using lc_filetype lc_subject lc_filename.

  • Local Data for E-mailing

data : lv_document type sodocchgi1,

li_pack type standard table of sopcklsti1,

lv_pack type sopcklsti1,

li_receiver type standard table of somlreci1,

lv_receiver type somlreci1,

  • li_body type standard table of solisti1,

  • lv_body type solisti1,

li_objhead type standard table of solisti1,

lv_objhead type solisti1,

lv_admin like line of s_ifmail .

data : lv_lines type i,

lv_sent type sonv-flag.

  • Fill Document Data

lv_document-obj_langu = sy-langu.

lv_document-obj_expdat = sy-datum.

lv_document-sensitivty = 'F'.

lv_document-no_change = 'X'.

lv_document-expiry_dat = sy-datum.

move lc_subject to lv_document-obj_descr.

describe table li_body lines lv_lines.

lv_document-doc_size = lv_lines * 255.

*-- Packing Data (for e-mail body)

lv_pack-transf_bin = space.

lv_pack-head_start = 1.

lv_pack-head_num = 0.

lv_pack-body_start = 1.

lv_pack-body_num = lv_lines.

lv_pack-doc_type = 'RAW'.

append lv_pack to li_pack.

describe table li_attach lines lv_lines.

*-- Packing list for email attachment (in XLS format)

clear lv_pack.

lv_pack-transf_bin = 'X'.

lv_pack-head_start = 1.

lv_pack-head_num = 1.

lv_pack-body_start = 1.

lv_pack-body_num = lv_lines.

lv_pack-doc_type = lc_filetype.

lv_pack-obj_name = 'MAIL'.

lv_pack-doc_size = lv_lines * 255.

move lc_filename to lv_pack-obj_descr.

append lv_pack to li_pack.

*-- Attachment Header

move space to lv_objhead-line.

append lv_objhead to li_objhead.

*-- Receiver list

lv_receiver-rec_type = 'U'.

lv_receiver-com_type = 'INT'.

lv_receiver-notif_del = 'X'.

lv_receiver-notif_ndel = 'X'.

loop at ls_ifmail into lv_admin.

lv_receiver-receiver = lv_admin-low.

append lv_receiver to li_receiver.

clear : lv_receiver-receiver,

lv_admin.

endloop.

*-- E-Mail Document

call function 'SO_NEW_DOCUMENT_ATT_SEND_API1'

exporting

document_data = lv_document

commit_work = 'X'

importing

sent_to_all = lv_sent

tables

packing_list = li_pack

object_header = li_objhead

contents_bin = li_attach

contents_txt = li_body

receivers = li_receiver

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.

message id sy-msgid type 'I' number sy-msgno

with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

endif.

if lv_sent = 'X'.

write : / text-t12.

loop at s_ifmail into lv_admin.

write : lv_admin-low.

endloop.

clear lv_sent.

endif.

endform. " SEND_EMAIL

  • Attach the HTML content

loop at i_html into lv_html.

lv_attach = lv_html.

append lv_attach to li_attach.

clear : lv_html, lv_attach.

endloop. "loop at i_html

  • Appending the text of boby

lv_body-line+0(44) = 'Please find attached Report of Discrepancies'.

lv_body-line+45(30) = 'in Transactions'.

append lv_body to li_body.

  • Send the content i_email_input to s_dsmail

perform send_email tables li_attach s_dsmail li_body

using lc_filetype lc_subject lc_filename.

endform. " EMAIL_LGCYTRANS