cancel
Showing results for 
Search instead for 
Did you mean: 

Email CSV Files from ABAP in 4.6c

Former Member
0 Kudos

Hi all,

I've been trying to work out how to send an email from an ABAP program with a body of email text and a CSV file attachment. We are running 4.6c and have SAP connect configured. I can send plain text emails but can't seem to attach a CSV file successfully. In my program I have set up an internal table which represents the CSV contents.

Any of the threads I follow on the SDN seem to point towards binary attachments which I have tried but do not seem to work as I want them to.. there are formatting issues or the file ends up having an extension which is not suitable.

I want to be able to name the attachment appropriately ... e.g. filename.csv so that the receiver can open it directly in Excel or in other text file applications.

Can anyone point me in the right direction for this ?

Cheers,

Gordon

Accepted Solutions (1)

Accepted Solutions (1)

thomas_jung
Developer Advocate
Developer Advocate
0 Kudos

You can have more than one text object in the Email send Function module. The trick is still just using the packing list table correctly. Just now when you add the entry to the Packing list table don't mark the field transf_bin. Put all the content of your document body and the csv attachment together in the contents_txt table. Then create two entries in the packing_list table that tells the function module how many lines in the content_txt belong to each object. I assume since you stated that you have already read other posting, that you don't need links to the details. However if it helps, let me know. I have a few - including some weblogs.

Former Member
0 Kudos

Hi Thomas, thanks for that.. I'm starting to get the hang of the packing table. But I'm still stuggline with the format of the file when it arrives in the email.

If I set the DOC_TYPE field to 'TXT' for the attachment, the email does arrive now with my data in a .txt file attachment however the CSV data which I had in my internal table is not presented in the file as it was in the table. In my table I_EXTRACT, each row is a CSV record I want to go into the file.. but when it arrives the txt file has incorrect line breaks, so more than one record per line etc. It's as all the data has been spewed out as long string into the text file.

Is there an extra step or escape key I need to add to my data to get SAP to add the line breaks correctly ?

My code for adding the attachment to the email data is as follows:

LOOP AT i_extract.

APPEND i_extract TO objtxt.

ENDLOOP.

  • Create Message Attachment

  • Write Packing List (Attachment)

att_type = 'TXT'.

DESCRIBE TABLE objtxt LINES tab_lines.

READ TABLE objtxt INDEX tab_lines.

objpack-doc_size = ( tab_lines - 2 ) * 255 + STRLEN( objtxt ).

clear objpack-transf_bin.

objpack-head_start = 2.

objpack-head_num = 0.

objpack-body_start = 2.

objpack-body_num = tab_lines.

objpack-doc_type = att_type.

objpack-obj_name = 'ATTACHMENT'.

objpack-obj_descr = 'myfile'.

APPEND objpack.

thomas_jung
Developer Advocate
Developer Advocate
0 Kudos

Yes you are going to want to add the Hex Value for Line Feed. In 610 and higher we have the class cl_abap_character_utilities. However in 46C you will probably just want to create a variable in your code.


data : begin of enter,
         x(2) type x value '0D0A',
       end of enter.

Have a look at the SAP function module GUI_DOWNLOAD for a sample of how SAP did this.

Former Member
0 Kudos

Hi Thomas,

That's fixed it. I did have an issue with white spaces on the various lines as I hadn't realised that after adding the line feed the next line would begin with the remaining blank space from my internal table record. I've sorted this out now and the file is working nicely.

Many thanks for your help.

Gordon

Former Member
0 Kudos

Hi Gordon,

Can you please tell me how could you delete the extra spaces which were leading before each line after the new line character??

Regards,

Pallavi.

Sharadha1
Active Contributor
0 Kudos

hi Gordon,

I am facing the problem of leading spaces from the second line in the csv,Can you please tell me how u got rid of this.

thanks,

Sharadha

cristiano_santos
Participant
0 Kudos

Hey Former Member dont just ask for help, but post your solutions too. More than one person is having this problem with trailing spaces in csv attached file.

cristiano_santos
Participant
0 Kudos

Hi people, the solution for leading blanks in csv file is OSS note 1408083

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi Gordon,

Can you please suggest how did you solve the issue of leading blank spaces .

Thanks,

Netal