Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sending attachment as email ( Format Error ) #9

Closed
ivanfemia opened this issue Dec 27, 2013 · 3 comments
Closed

Sending attachment as email ( Format Error ) #9

ivanfemia opened this issue Dec 27, 2013 · 3 comments
Milestone

Comments

@ivanfemia
Copy link
Collaborator

What steps will reproduce the problem?

 constants:
    gc_tab  type c value cl_bcs_convert=>gc_tab,
    gc_crlf type c value cl_bcs_convert=>gc_crlf.

  DATA:
    mailto type ad_smtpadr.

  CONACATENATE SY-UNAME '@GMAIL.COM' INTO MAILTO.

  data send_request   type ref to cl_bcs.
  data document       type ref to cl_document_bcs.
  data recipient      type ref to if_recipient_bcs.
  data bcs_exception  type ref to cx_bcs.

  data main_text      type bcsy_text.
  data binary_content type solix_tab.
  data binary_content1 type SOLIX.
  data size           type so_obj_len.
  data sent_to_all    type os_boolean.

  clear: binary_content[].

*     loop at lt_file_tab into lt_file_tab1.
*
*
*     binary_content1-line = lt_file_tab1-line.
*
*     append binary_content1 to binary_content.
*
*     endloop.

CALL FUNCTION 'SO_SOLITAB_TO_SOLIXTAB'
  EXPORTING
    IP_SOLITAB        = lt_file_tab
 IMPORTING
   EP_SOLIXTAB       = binary_content
          .

  try.

*     -------- create persistent send request ------------------------
      send_request = cl_bcs=>create_persistent( ).

*     -------- create and set document with attachment ---------------
*     create document object from internal table with text
      append 'Multiple worksheets!'
                to main_text.                               "#EC NOTEXT
      document = cl_document_bcs=>create_document(
        i_type    = 'RAW'
        i_text    = main_text
        i_subject = 'Mul_sheets.xlsx' ).                    "#EC NOTEXT

*     add the spread sheet as attachment to document object
      document->add_attachment(
        i_attachment_type    = 'XLS'                        "#EC NOTEXT
        i_attachment_subject = 'MulSpreadSheet.xlsx'        "#EC NOTEXT
        i_att_content_hex    = binary_content ).

*     add document object to send request
      send_request->set_document( document ).

*     --------- add recipient (e-mail address) -----------------------
*     create recipient object
      recipient = cl_cam_address_bcs=>create_internet_address( mailto ).

*     add recipient object to send request
      send_request->add_recipient( recipient ).

*     ---------- send document ---------------------------------------
      sent_to_all = send_request->send( i_with_error_screen = 'X' ).

      commit work.

      if sent_to_all is initial.
        message i500(sbcoms) with mailto.
      else.
        message s022(so).
      endif.

*   ------------ exception handling ----------------------------------
*   replace this rudimentary exception handling with your own one !!!
    catch cx_bcs into bcs_exception.
      message i865(so) with bcs_exception->error_type.
  endtry.
@ivanfemia
Copy link
Collaborator Author

Hi,

I've tried your coding in my system. The main problem is that you've not specified the i_attachment_size parameter when attaching the binary to the mail. Here a complete test report. Hope that solves your issue and we can close it.

REPORT zdemo_excel_send_mail.

CONSTANTS:
gc_tab TYPE c VALUE cl_bcs_convert=>gc_tab,
gc_crlf TYPE c VALUE cl_bcs_convert=>gc_crlf.

DATA:
mailto TYPE ad_smtpadr.

CONCATENATE sy-uname '@GMAIL.COM' INTO mailto.

DATA send_request TYPE REF TO cl_bcs.
DATA document TYPE REF TO cl_document_bcs.
DATA recipient TYPE REF TO if_recipient_bcs.
DATA bcs_exception TYPE REF TO cx_bcs.

DATA main_text TYPE bcsy_text.
DATA binary_content TYPE solix_tab.
DATA binary_content1 TYPE solix.
DATA size TYPE so_obj_len.
DATA sent_to_all TYPE os_boolean.

DATA: lo_excel TYPE REF TO zcl_excel,
lo_worksheet TYPE REF TO zcl_excel_worksheet.

DATA: lv_file TYPE xstring,
lv_bytecount TYPE i,
lv_filelen TYPE so_obj_len,
lt_file_tab TYPE solix_tab.

" Creates active sheet
CREATE OBJECT lo_excel.

" Get active sheet
lo_worksheet = lo_excel->get_active_worksheet( ).
lo_worksheet->title = 'Sheet1'.
lo_worksheet->set_cell( ip_column = 'B' ip_row = 2 ip_value = 'This is the first sheet' ).

lo_worksheet = lo_excel->add_new_worksheet( ).
lo_worksheet->title = 'Sheet2'.
lo_worksheet->set_cell( ip_column = 'B' ip_row = 2 ip_value = 'This is the second sheet' ).

lv_file = lo_excel->save_as( zcl_excel=>c_xlsx ).

" Convert to binary
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = lv_file
IMPORTING
output_length = lv_bytecount
TABLES
binary_tab = lt_file_tab.

TRY.

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

    send_request = cl_bcs=>create_persistent( ).

  • -------- create and set document with attachment ---------------
    
  • create document object from internal table with text
    

    APPEND 'Multiple worksheets!'
    TO main_text. "#EC NOTEXT
    document = cl_document_bcs=>create_document(
    i_type = 'RAW'
    i_text = main_text
    i_subject = 'Mul_sheets.xlsx' ). "#EC NOTEXT

  • add the spread sheet as attachment to document object
    

    lv_filelen = lv_bytecount.
    document->add_attachment(
    i_attachment_type = 'EXT' "#EC NOTEXT
    i_attachment_subject = 'MulSpreadSheet.xlsx' "#EC NOTEXT
    i_attachment_size = lv_filelen
    i_att_content_hex = lt_file_tab ).

  • add document object to send request
    

    send_request->set_document( document ).

  • --------- add recipient (e-mail address) -----------------------
    
  • create recipient object
    

    recipient = cl_cam_address_bcs=>create_internet_address( mailto ).

  • add recipient object to send request
    

    send_request->add_recipient( recipient ).

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

    sent_to_all = send_request->send( i_with_error_screen = 'X' ).

    COMMIT WORK.

    IF sent_to_all IS INITIAL.
    MESSAGE i500(sbcoms) WITH mailto.
    ELSE.
    MESSAGE s022(so).
    ENDIF.

  • ------------ exception handling ----------------------------------

  • replace this rudimentary exception handling with your own one !!!
    CATCH cx_bcs INTO bcs_exception.
    MESSAGE i865(so) WITH bcs_exception->error_type.
    ENDTRY.

@ivanfemia
Copy link
Collaborator Author

Yes It works, Great

@ivanfemia
Copy link
Collaborator Author

Hi Gregor,
With this change, the email file is saved with extension .EXT and not with .XLSX
The user will have to download this file, unzip it and save it as .XLSX format to view the contents.
Is it not possible to directly send it in .XLSX format so that the user will not have to change the document type or am I missing anything here?
Vinny

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant