uuencode problem

Hi,
I am making a shell script on HP-UX that will do some Oracle db query and fetch data. Then after formatting the output files (there are two files) in a csv format, I want to send them as email attachments. Unfortunately the email contains garbage. I could not find solution to the problem on forums. Can anyone help me ? Part of the code is below.

1_OUT="${WORKDIR}/1.`date +%Y%m%d`.csv"
2_OUT="${WORKDIR}/2.`date +%Y%m%d`.csv"
( uuencode ${1_OUT} ${1_OUT};uuencode ${2_OUT} ${2_OUT} ) | mailx -s "Attachments are in standard format" <emailid>

I am receiving the email im mS Outlook but it contains junk data in mailbody with no attachment.
Mail bode contents (first two lines) are:

begin 640 /home/asutoshch/1.20110707.csv
M1DQ47TY"4BQ&3%1?3$-,7T]224=?1$%412Q/4DE'7T%24%0L1$535%]!4E!4

Thank you.

You are just dumping the two uuencoded files into the body with no indication that they are attachments, so I would expect that the mail you receive at the destination is just the uuencoded files.

I did a couple of rounds of quick tests here (SuSE Linux) and this seems to do what you want:

echo "There should be attachments" | mail  -s "subject" -a file1.csv -a file2.csv user@system.com

The -a option attaches file 1 and file 2 rather than putting them into the body. Unless your destination knows how to deal with a uuencoded file, you will still have issues if you attach the uuencoded files. If you were uuencoding to "get them through the mail," then the attachment option should do any necessary encoding (quoted printable or base64 if the data is binary) and the mail reader at the other end should know how to deal with either of those two encoding types. Thus, I think the files that you need to supply on the command line are the names of the original .csv files.

Note that I used mail and not mailx; mailx on my system didn't support -a. You might want to read the man page for both mail and mailx.

Hope this gets you further along.

Thank you, but mail is not working for attachment on HP-UX. I could not get from man page.

On HP-UX, neither mail nor mailx handle the creation of attachments. The best you can do is to uuencode the files and embed them in the body of the message.

Hi Murphy,
Thanks for your response. but I have seen several excel reports coming as email attachment daily from shell program on HP-UX box. So I was thinking if I can implement that.

HP_UX does not natively support Excel. Some application is programatically writing files in Excel format and mailing them as attachments. That is perfectly do-able. For example, there are Perl modules that will convert data to Excel format and mail files as attachments.

Keeping it all simple, however...you might want to note that even Excel doesn't need things to be all that smart unless it's expecting specific formatting in the attachment. The Perl Excel modules are tremendous, but if you're only expecting to send text dumps, and not formatted content, Excel can take a tab-delimited file with a .xls (or similar, registry-based) extension.

On Windows, Excel is invoked according to the file extension in use...and tab-delimited is the default, so it just works. (Pardon the pun...)

Use uuencode to embed the attachments into your mail, and you've achieved the same results as your system's other shell script.

probably the issue is the file doesn't show as an attachment. it lacks MIME section maybe.

Try these last two posts:
http://www.unix.com/hp-ux/41306-sending-attachments-through-mailx.html

Curleb, Your explanation is great. I tried that way but the mailx is making the trouble. In fact I downloaded the generated .xls file (simply tab delimited fields) and ms excel could open fine. But mailx is making it a mess. The email body gets the junk dump
begin 640 /home/asutoshch/test.20110715.xls
M1DQ47TY"4@E&3%1?3$-,7T]224=?1$%410E/4DE'7T%24%0)1$535%]!4E!4
M"5-#2$5$7TQ#3%]$15!?1$%410E30TA%1%],0TQ?1$507U1)344)5$%)3%].
M0E()4U1!4E])3D0)24Y"3U5.1%]&3%1?3D)2"4E.0D]53D1?1DQ47T%24E]$
M051%"4E.0D]53D1?1DQ47T%24E]424U%"C,S-`DQ-"U*54PM,C`Q,0E/34$)

Assuming that these ".csv" files are in standard unix text file format and contain only printable ASCII characters.

Needs a bit of work to change the variable names (avoid variable names starting with numbers), remove the absolute path from the filenames, convert the file to MSDOS format and keep the target filenames valid in MSDOS. Also add the "-m" switch to mailx (a weird HP-UX feature).

I realise that changing the filenames may ripple back up the script but we must bear in mind that this is SNMP and we should keep it simple. Also many virus scanners will eat a filename containing a double file extention.

OUT_1="`date +%Y%m%d`_1.csv"
OUT_2="`date +%Y%m%d`_2.csv"
cd "${WORKDIR}"
( ux2dos ${OUT_1} | uuencode ${OUT_1} ; ux2dos ${OUT_2} | uuencode ${OUT_2} ) | mailx -m -s "Attachments are in standard format" <emailid>

When working the two attachments should appear as proper attachments.

I've assumed you are using a standard HP-UX Posix Shell or Korn Shell. Variable names like ${1_OUT} are not valid in this context and should be giving you runtine errors.

I hope that works for you (it's working in HP-UX)

File=/home/dir1/dir2/file.csv (whole path of the file)

mailx -s "name of the file"  destination@domain.com << EOF

Hi,

here the attached file :

~< ! uuencode ${File} `basename ${File}`




created by "Your NAME"
~.
EOF

Here's the basic logic we use to send csv formatted files to Outlook (ksh on Solaris) maybe it will work for you.

#!/bin/ksh

MAILBODY="This is the body of the e-mail"

# set_example.ksh is a unix shell script that gets sent as an attachment.
unix2dos -437 -ascii set_example.ksh attach.out
if (( $? != 0 ))
  then print "$0: unix2dos failed"
       exit 1
fi

(print $MAILBODY; uuencode attach.out set_example.txt) | mailx -s "mail test9" your_id@your_company.com
if (( $? != 0 ))
  then print "$0: uuencode or mailx failed"
       exit 2
fi

exit 0

On hpux mailx needs the -m option for attached stuff...as shown by methyl

Thank you. I am able to do it by html programming from shell script. The excel sheet can be opened sucessfully every time sent. Thank you all for your guidance.