Sending attachments using email through shell script

Hi all,

I have written a shell script which sends emails with attachments to our clients. All our attachments are simple flat files (.txt format).

The script is working fine and sending the attachments to the mail-ids except that, when i am sending the attachments to non-outlook users (Like yahoo, gmail etc), i am seeing a junk character at the end of each line and the next line of file is appended to the previous line. In short all the data in the file is coming in one long line.

Here also there is a small twist. In gmail, there will be 2 options 2 download the file, View and Download. I am getting this problem only when i am downloading to the file to my local folder and viewing it.

Also when i am opening the same attachments in outlook, i can see that the data is appearing line by line as we entered it.

Kindly advise me, why this problem is occuring only when i am sending the mail to yahoo and gmail accounts. I am pasting the relavent code from my script.
*********************************************************
(
cat <<!
From: $FROM
Subject: $SUBJ
To: $TO
!

[ "$CC" ] && echo "Cc: $CC"

## Generate the MIME header #####

cat <<!
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="DMW.Boundary.605592468"

This is a Mime message, which your mail reader may not understand. Parts
of the message will appear as text. If the remainder appears as random
characters in the message body, instead of as attachments, then you'll
have to extract these parts and decode them manually.

--DMW.Boundary.605592468
Content-Type: text/plain; name="message.txt"; charset=US-ASCII
Content-Disposition: inline; filename="message.txt"
Content-Transfer-Encoding: 7bit

!

# Read the standard input as the main text of the message ...
# -------------------------------------------------------

cat -

# Now process the File attachments ...
# -----------------------------------------

if [ "$FILES" ]
then
for fl in $FILES
do

   BASE=\`basename $fl\`
          echo --DMW.Boundary.605592468
   echo Content-Type: application/octet-stream\\; name=\\"$BASE\\"
   echo Content-Disposition: attachment\\; filename=\\"$BASE\\"
   echo Content-Transfer-Encoding: 7bit
   echo

   cat $fl

done

fi

# append the final boundary line ...

echo --DMW.Boundary.605592468--

) | /usr/lib/sendmail -t
*********************************************************

Thanks & Regards
Vamsi.

Hi Vamsi,

run the command unix2dos for the attachment file and attach the new file of dos format...

Unix new line character is different from windows notepad's new line character and everything appears in the same line when we open it in notepad or similar like other web emails... but the same opens correctly in wordpad or ms word...

Please try the above stuff.. not sure whether it is because of something else as well.

Hi Mahendra,

thanks for the information. You almost resolved my problem. I installed UNIX2DOS on my PC and converted the attachments which i sent through my shell script to DOS mode and they are fine now.

But we are sending these attachments to our clients. So we cannot ask them to install UNIX2DOS, copy the attachments on their PC and run the command to correct the format.

Is there any way in which i run the UNIX2DOS (or similar) command within the script and then send the attachment to users?? I am working on HP-UX and there is no UNIX2DOS inbuilt function on my OS. You know any similar function in HP-UX which does the work of UNIX2DOS?

Thanks & Regards
Vamsi Sudhakar.

HP has ux2dos and dos2ux instead of unix2dos and dos2unix.

Hi,

I used the ux2dos command to convert the file to dos mode. But strangely still the problem persists. I mean when I saved the attachment from my GMAIL and open it, still the whole data is coming in a single line.

Where as I if i run UNIX2DOS (on my windows as mentioned in my previous thread) after extracting the attachment from my GMAIL, then i am getting the data in correct format.

I am really confused why it is not working when i do the same from my UNIX. Following is the way i am running my commands:
**********************************************************
$ /usr/bin/ux2dos TEST_Alert_File_01232006.txt > xyz.txt
$
$ sh send_mail -t vamsibehara@gmail.com -s "ALRT FILES" -a xyz.txt
Enter body of main message ( end with Ctrl-D ) ...
PLS FIND ATTCHED
VAMSI
Ctrl-D
$
**********************************************************
Kindly note that the problem is coming only if i am copying the attachment to my local folder from the mail and open it. If i am directly opening the file or use the 'VIEW' option of the mail, then i am not getting this problem.

Any thoughts on why this is happening like this?

Thanks & Regards
Vamsi Sudhakar.

It looks strange...

I have another solution for this, if you are facing the problem only when you save and open from you disk, then I would advice you to name the attachment file as test.rtf instead of test.txt, so that it would open in wordpad or msword directly, and they both support unix new line character...

Thanks Mahindra and RTM.
The .rtf has resolved the problem.

I just have to get back to the client and see whether he is OK with the rtf format rather than txt format.

Thanks & Regards
Vamsi Sudhakar.