07-15-2016 11:56 PM
Hi all,
I need generate a txt file, compress it into a zip file and send e-mail attachment, but gmail does not recognize the file extension, and if I download the zip file from hotmail, the txt file is not formatted in columns.
This is the correct column format
This is the incorrect column format
This is file in gmail and downloaded
This is program
CALL FUNCTION 'LIST_TO_ASCI'
TABLES
listasci = ascilist
listobject = listtab
EXCEPTIONS
empty_list = 1
list_index_invalid = 2
OTHERS = 3.
data lc_newline type c value cl_abap_char_utilities=>cr_lf.
clear lv_strmsg.
loop at ascilist. " into wa_text.
concatenate lv_strmsg ascilist-line lc_newline into lv_strmsg.
endloop.
clear: lv_xstrmsg, lv_encoding.
lv_encoding = '4130'. " I try with 1160, 4110, 4120, also without this parameter
CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
EXPORTING
TEXT = lv_strmsg
ENCODING = lv_encoding
IMPORTING
BUFFER = lv_xstrmsg.
TRY.
send_request = cl_bcs=>create_persistent( ).
lv_subject = 'subject'.
lv_name_str = 'name_file.txt'.
create object zipper.
zipper->add( name = lv_name_str
content = lv_xstrmsg ).
lv_zipfilehex = zipper->save( ).
refresh binary_content.
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
BUFFER = lv_zipfilehex
IMPORTING
OUTPUT_LENGTH = lv_size
TABLES
BINARY_TAB = binary_content.
PERFORM body_mail CHANGING main_text .
document = cl_document_bcs=>create_document(
i_type = 'HTM'
i_text = main_text
i_subject = lv_subject ).
size = lv_size.
document->add_attachment(
i_attachment_type = 'zip'
i_attachment_subject = lv_name
i_attachment_size = size
i_att_content_hex = binary_content ).
send_request->set_document( document ).
email = 'mail@gmail.com'.
recipient = cl_cam_address_bcs=>create_internet_address( email ).
send_request->add_recipient( recipient ).
CALL METHOD send_request->set_status_attributes
EXPORTING
i_requested_status = 'E'.
sent_to_all = send_request->send( i_with_error_screen = 'X' ).
COMMIT WORK.
IF sent_to_all IS INITIAL.A
size = lv_size.
document->add_attachment(
i_attachment_type = 'zip'
i_attachment_subject = lv_name
i_attachment_size = size
i_att_content_hex = binary_content ).
send_request->set_document( document ).
email = 'mail@gmail.com'.
recipient = cl_cam_address_bcs=>create_internet_address( email ).
send_request->add_recipient( recipient ).
CALL METHOD send_request->set_status_attributes
EXPORTING
i_requested_status = 'E'.
sent_to_all = send_request->send( i_with_error_screen = 'X' ).
COMMIT WORK.
GE s022(so).
CALL METHOD send_request->set_status_attributes
EXPORTING
i_requested_status = 'E'.
sent_to_all = send_request->sen
d( i_
sent_to_all = send_request->send( i_with_error_screen = 'X' ).
COMMIT WORK.
with_error_screen = 'X' ).
COMMIT WORK.
Best regards,
Robert
07-16-2016 12:05 AM
Sorry,
The code is not copied properly:
CALL FUNCTION 'LIST_TO_ASCI'
TABLES
listasci = ascilist
listobject = listtab
EXCEPTIONS
empty_list = 1
list_index_invalid = 2
OTHERS = 3.
clear lv_strmsg.
loop at ascilist.
concatenate lv_strmsg ascilist-line
into lv_strmsg separated by CL_ABAP_CHAR_UTILITIES=>NEWLINE.
endloop.
clear: lv_xstrmsg, lv_encoding.
lv_encoding = '4130'. " I try with 1160, 4110, 4120, also without this parameter
CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
EXPORTING
TEXT = lv_strmsg
ENCODING = lv_encoding
IMPORTING
BUFFER = lv_xstrmsg.
TRY.
send_request = cl_bcs=>create_persistent( ).
lv_subject = 'subject'.
lv_name_str = 'name_file.txt'.
create object zipper.
zipper->add( name = lv_name_str
content = lv_xstrmsg ).
lv_zipfilehex = zipper->save( ).
refresh binary_content.
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
BUFFER = lv_zipfilehex
IMPORTING
OUTPUT_LENGTH = lv_size
TABLES
BINARY_TAB = binary_content.
PERFORM body_mail CHANGING main_text .
document = cl_document_bcs=>create_document(
i_type = 'HTM'
i_text = main_text
i_subject = lv_subject ).
size = lv_size.
document->add_attachment(
i_attachment_type = 'zip'
i_attachment_subject = lv_name
i_attachment_size = size
i_att_content_hex = binary_content ).
send_request->set_document( document ).
email = 'mail@gmail.com'.
recipient = cl_cam_address_bcs=>create_internet_address( email ).
send_request->add_recipient( recipient ).
CALL METHOD send_request->set_status_attributes
EXPORTING
i_requested_status = 'E'.
sent_to_all = send_request->send( i_with_error_screen = 'X' ).
COMMIT WORK.
IF sent_to_all IS INITIAL.
MESSAGE i500(sbcoms) WITH email.
ELSE.
MESSAGE s022(so).
ENDIF.
CATCH cx_bcs INTO bcs_exception.
MESSAGE i865(so) WITH bcs_exception->error_type.
ENDTRY.
Best regards,
Robert
07-16-2016 5:26 AM
Dear Robert,
Just change your logic :
From :
CALL FUNCTION 'LIST_TO_ASCI'
TABLES
listasci = ascilist
listobject = listtab
EXCEPTIONS
empty_list = 1
list_index_invalid = 2
OTHERS = 3.
clear lv_strmsg.
loop at ascilist.
concatenate lv_strmsg ascilist-line
into lv_strmsg separated by CL_ABAP_CHAR_UTILITIES=>NEWLINE.
endloop.
To like this :
LOOP AT IT_MARA INTO WA_MARA.
CONCATENATE WA_MARA-MATNR
WA_MARA-MTART
WA_MARA-MEINS
WA_MARA-MBRSH
WA_MARA-MATKL
INTO LV_STRING
SEPARATED BY CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB.
CONCATENATE LV_DATA_STRING LV_STRING
INTO LV_DATA_STRING
SEPARATED BY CL_ABAP_CHAR_UTILITIES=>NEWLINE.
ENDLOOP.
Please refer the below link :
https://www.sapnuts.com/tutorials/Send-email-with-zip-attachment/7.html
Regards,
Yance
07-16-2016 11:36 AM
Your general logic seems good. I'm not sure about the code page, as you use a special UTF-8 code page (4130). Instead, you should use the official UTF-8 code page (4110).
Your issue is about the "new line" character. It may not be interpreted correctly by a few softwares (like notepad for instance). Try with cl_abap_char_utilities=>CRLF (official Windows character equivalent to newline).
Are you sure "lv_name" contains the zip file name In the statement "document->add_attachment( i_attachment_type = 'zip' i_attachment_subject = lv_name ..." ?
07-17-2016 11:59 PM
Hi Yance,
The internat table ascilist already contains the column delimiter "|" for that reason I do not use CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB,
The following picture is after execute LIST_TO_ASCI function.
Best regards,
Robert
07-18-2016 12:44 AM
Hi Sandra,
Thank you, lv_name no contains the zip file name, I already insert the name and the zip file is recognized in gmail, but the txt file not have format columns, I already use cl_abap_char_utilities=>CRLF but it did not work,
I already sent only a txt file using cl_abap_char_utilities not zip => CRLF, the SCMS_TEXT_TO_BINARY function and then the method add_attachment and it worked correctly but the file is very big and I need zip.
Best regards,
Robert
07-18-2016 9:06 AM
You said: "lv_name no contains the zip file name".
What do you mean? Which value do you have in lv_name?
It's not clear what your issues are, because you also said "but gmail does not recognize the file extension". What do you mean, is there an issue here, what is it exactly?
You said "I already sent ... but the file is very big and I need zip". I never said that you shouldn't zip, or is there Something I did not understand?
07-18-2016 4:27 PM
Hi Sandra,
Sorry, maybe I did not explain properly, concerning the name of the zip file, there is a code that delete the contents of the lv_name variable, this code I was not posted, I already corrected the code and now gmail recognize the zip file extension.
The initial version of this program send a txt file, for that reason I told you I already sent a txt file with the correct format columns, but now due to file size require send a zip file.
Now only I have the format columns problem.
Best regards,
Robert
07-18-2016 4:55 PM
If it was okay without zip, but KO when you zip, as there's no change in the zipped document (if you unzip it, it will look exactly the same), so I think the error is only because of the software you use to read the zipped document.
Did you open the TXT file directly from the zip viewer software, or did you unzip it and view it with the same software as initially? (at the time your program didn't zip).