sendmail with attachment not working

dear all

I have below function which send an email, but if I need to add an attachement it won't work instead it write some jibberish to body of the email

begin 644 SIGN_OFF_AP_20120626.csv
M4TE'3E]/1D9?4TA%150L351-7T1!5$4L0D]/2RQ35$%455,L5D%25%E012Q#
%3U5.5`H`
`
end
function send_mail_html
{
SUBJECT="$1"
FROM="test@linux.site"
FILENAME=$4

(
        echo "Subject: $SUBJECT"
        echo "From: $FROM"
        echo "MIME-Version: 1.1"
        echo "Content-Type: text/html"
        echo "Content-Transfer-Encoding: 8bit"
        echo "Content-Disposition: inline"
        echo "<html>"
        echo "<body>"
        echo "<FONT FACE="Courier New">"
        echo "<PRE>"
        uuencode ${FILENAME} $(basename ${FILENAME})
        cat $3
        echo "</PRE>"
        echo "</FONT>"
        echo "</body>"
        echo "</html>"
) | /usr/sbin/sendmail $2
}

and calling above function as below with proper placeholder

send_mail_html "${mailsubject}" "${mailto}" "/scripts/signoff/sos_template_ntd.mail" "${spool_to}"

can anyone suggestion, where and what exactly ggone wrong with the function?

The "gibberish" is the output from uuencode.
Just for interest, it decodes to:

SIGN_OFF_SHEET,MTM_DATE,BOOK,STATUS,VARTYPE,COUNT

The section headers you are giving to sendmail look a bit unlikely.

What Operating System and version is this?

If you search the forums for "sendmail mime attachment" there are several hits.
This thread contains an example relevant to you (even if it didn't match the original requirement).
http://www.unix.com/shell-programming-scripting/191073-sending-csv-attachment-html-text-together.html

wow!! decode message are totally correct
I'm using SUSE Linux and ksh

when you said "The section headers you are giving to sendmail look a bit unlikely" does it eman for the attachment ?

anyway I'm looking those links..thanks for that.
But would you mind, letting me know, how to check which uuencode I'm using??

---------- Post updated at 07:50 AM ---------- Previous update was at 07:21 AM ----------

hi Methyle,

I have gone thru your suggested link, and I changed my function a bit
good thing is I'm getting CSV as an attachment but without any content in that attachment..could you assist.....

(
        echo "Subject: Test"
        echo "From: test@linux.site"
        echo "MIME-Version: 1.1"
	echo 'Content-Type: multipart/mixed; boundary="-q1w2e3r4t5"'
	echo
 	echo '---q1w2e3r4t5'
        echo "Content-Type: text/html"
        echo "Content-Transfer-Encoding: 8bit"
        echo "Content-Disposition: inline"
        echo "<html>"
        echo "<body>"
        echo "<FONT FACE="Courier New">"
        echo "<PRE>"
	cat /var/tmp/test
	echo '---q1w2e3r4t5'
	echo "Content-type: Application/Octet-stream; name=/var/tmp/SIGN_OFF_AP-Summary20120626.csv; type=Binary" 
	echo "Content-Transfer-Encoding: X-UUencode"
	echo 'Content-Disposition: attachment; filename="'/var/tmp/SIGN_OFF_AP-Summary20120626.csv'"'
        uuencode "/var/tmp/SIGN_OFF_AP-Summary20120626.csv" "SIGN_OFF_AP-Summary20120626.csv"
	echo '---q1w2e3r4t5--'
        echo "</PRE>"
        echo "</FONT>"
        echo "</body>"
        echo "</html>"
) | /usr/sbin/sendmail manas@axe.com

Maybe look again at the example, particularly the boundary lines and significant blank lines at the end of the mail body. The HTML bit is alien to me and it may be easier to test without it to start with.

Why everyone allways try to reinvent the wheel :slight_smile:

Why dont you use something like this:

http://caspian.dotconf.net/menu/Software/SendEmail/

Hope it helps you :wink:

hi Methyl,

I removed the HTML tag from the function as well as I changed the boundary, but still no content in the attachment.

(
        echo "Subject: Test"
        echo "From: test@linux.site"
        echo "MIME-Version: 1.0"
	echo "Content-Type: multipart/mixed; boundary=Message-Boundary-"$$
 	echo "--Message-Boundary-"$$
        echo "Content-Type: text/html; charset=US-ASCII"
        echo "Content-Disposition: inline"
        echo ""
        echo ""
	cat /var/tmp/test
	echo ""
	echo "--Message-Boundary-"$$
	echo "Content-type: Application/Octet-stream; name=SIGN_OFF_AP-Summary20120626.csv; type=Binary" 
	echo "Content-Transfer-Encoding: X-UUencode"
	echo "Content-Disposition: attachment; filename=SIGN_OFF_AP-Summary20120626.csv"
        uuencode "/var/tmp/SIGN_OFF_AP-Summary20120626.csv" "SIGN_OFF_AP-Summary20120626.csv"
) | /usr/sbin/sendmail manas@axe.com

has it to do with FILENAME ??

---------- Post updated at 08:57 AM ---------- Previous update was at 08:37 AM ----------

Thanks for your suggestion, but couldn't add new package in my already built system and a loads of procedure involved which might take longer than the solution from the existing one. you know , the process of corporate world....:rolleyes:

Possibly because the lines preceding your uuencode are in a different order from the sample and contain subtle differences.
Also worth trying .txt as the file extension in case your browser has something unusual associated with .csv .
If the original file is in unix text format you may need to convert it to MSDOS format (using ux2dos or unix2dos program) before the uuencode.

I wish I could try your script but my sendmail generates the MIME headers automatically.