Need help in sending html mail with attachment

Hi Everyone,
I am facing problems in sending html mail with attachemnt.
I will able to send mail with attachment (plain text ) using mailx -s and uuencode command and
also html mail without attachment using sendmail option.
However I am not able to send html mail along with attachment.Either one of it is working
(html mail or attachment)
Below are the different ways I have tried. Could you please help me in resolving the same.
1) Failed because of illegal option base64

#!/usr/bin/ksh
export MAILTO="abc@abc.com"
export SUBJECT="Mail Subject"
export BODY="card_summary_mail.html"
export ATTACH="query5_result.csv"
(
 echo "To: $MAILTO"
 echo "Subject: $SUBJECT"
 echo "MIME-Version: 1.0"
 echo 'Content-Type: multipart/mixed; boundary="-q1w2e3r4t5"'
 echo
 echo '---q1w2e3r4t5'
 echo "Content-Type: text/html"
 echo "Content-Disposition: inline"
 cat $BODY
 echo '---q1w2e3r4t5'
 echo 'Content-Type: application; name="'$(basename $ATTACH)'"'
 echo "Content-Transfer-Encoding: base64"
 echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
 uuencode --base64 $ATTACH $(basename $ATTACH)
 echo '---q1w2e3r4t5--'
) | /usr/lib/sendmail $MAILTO

------------------------------------------------------------
2)

cib-sokay2{u384283}323:cat test_html2.sh
{
  uuencode query5_result.csv query5_result.csv > attachment.txt
  cat mail.html attachment.txt > attachment2.html
} | /usr/lib/sendmail -t abc@abc.com

-----------------------------------------------
3)

cib-sokay2{u384283}324:cat test_html3.sh
export MAILTO="abc@abc.com"
export CONTENT="mail.html"
export CONTENT_F="attachment.txt"
export SUBJECT="TEST EMAIL: TESTING HTML"

BOUNDARY='=== This is the boundary between parts of the message. ==='
{
print -  "From: Someone <$MAILFROM>"
print -  "To: Someone <${MAILTO}>"
print -  'Subject:' $SUBJECT
print -  'MIME-Version: 1.0'
print -  'Content-Type: MULTIPART/MIXED; '
print -  '    BOUNDARY='\"$BOUNDARY\"
print -
print -  '        This message is in MIME format.  But if you can see this,'
print -  "        you aren't using a MIME aware mail program.  You shouldn't "
print -  '        have too many problems because this message is entirely in'
print -  '        ASCII and is designed to be somewhat readable with old '
print -  '        mail software.'
print -
print -  "--${BOUNDARY}"
print -  'Content-Type: TEXT/PLAIN; charset=US-ASCII'
print -
cat $CONTENT
print -
print -
print -  "--${BOUNDARY}"
print -  'Content-Type: TEXT/PLAIN; charset=US-ASCII; name='${CONTENT}
print -  'Content-Disposition: attachment;   filename='${CONTENT_F}
print -
cat ${CONTENT}
print -
print -  "--${BOUNDARY}--"
} | /usr/lib/sendmail ${MAILTO}

------------------------------------------------------------

cib-sokay2{u384283}326:cat test_html4.sh
#!/usr/bin/ksh
export MAILTO="abc@abc.com"
export CONTENT="mail.html"
export SUBJECT="subject of email"
(
echo "Subject: $SUBJECT"
# This appears in the mail body
cat $CONTENT
# The next line creates the attachment with a suitable extension to read
# with Windows notepad
unix2dos "attachment.txt" | uuencode myattach.txt
echo "."
) | /usr/lib/sendmail $MAILTO

-------------------------------------

Although it hasn't been updated in awhile, I have had good luck sending emails with Perl and the MIME::Lite package.

MIME::Lite - search.cpan.org

It is capable of sending UTF8 text, and should more than handle sending html. In the past I have used mpack and uuencode to send attachments through Unix. But the Perl libraries work much better and produce code that is easier to read. There should be a number of scripts available that you can download and run, mostly as is.

If you have static HTML that you need to include you can generate that though the qq{} structure. It allows multiple lines in a single scalar.

my $email_body = 
qq{ From: Someone <$MAILFROM>"
To: Someone <${MAILTO}>"
Subject:' $SUBJECT
MIME-Version: 1.0'
Content-Type: MULTIPART/MIXED; '
 BOUNDARY='\"$BOUNDARY\"

 This message is in MIME format. But if you can see this,'
 you aren't using a MIME aware mail program. You shouldn't "
 have too many problems because this message is entirely in'
 ASCII and is designed to be somewhat readable with old '
 mail software.'

--${BOUNDARY}"
Content-Type: TEXT/PLAIN; charset=US-ASCII'

cat $CONTENT


--${BOUNDARY}"
Content-Type: TEXT/PLAIN; charset=US-ASCII; name='${CONTENT}
Content-Disposition: attachment; filename='${CONTENT_F}

cat ${CONTENT}

--${BOUNDARY}--"
};

Here is example script used only ksh + base64 + sendmail

# sendmail inline text+html and attachments

get_mimetype()
{
  Xfname="$1"
  [ "$Xfname" = "" ] && echo "usage:$0 fname" >&2 && return 1
  [ ! -f "$Xfname" ] && echo "no file $Xfname" >&2 && return 2
  mtype=$(file --mime-type "$Xfname" )
  mtype=${mtype##* } # take last value from the answer, space is delim.
  echo "$mtype"
}

####
# parse file, expand variables and commands, dangerous if source file has not controlled
# but nice method to use templates to make dynamic output
parse_file()
{
 [ "$*" = "" ] && return
 [ ! -f "$1" ] && return
 eval echo  "\"$(cat $1 | sed 's+\"+\\"+g'   )\""
}

#################################################
# MAIN
#################################################
from="some@example.xx"
to="some@some.us"

epoc=$(printf "%(%#)T" now)  # other sh users can use date something ...
subject="Message $epoc"
procid=$$
boundary="My_/some1234.$epoc.$procid.0"
boundarybody="My_/some1234.$epoc.$procid.1"
body="This is my message body $epoc"
# of course you can make body using file as html block has done

# make mail and pipe for sendmail
# mail body text + html
{
echo "$(<<EOT
From: $from
To: $to
Subject: $subject
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="$boundary"

--$boundary
Content-Type: multipart/alternative; boundary="$boundarybody"

--$boundarybody

Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

$body

$(date), $(uptime)

--$boundarybody
Content-Type: text/html; charset=ISO-8859-1
Content-Disposition: inline

$(parse_file example.html)

--$boundarybody--
EOT
)"

# attachments = check the mime-type + encoded base64
# loop my all tst.* files ...
for file in tst.*
do

  [ ! -f "$file" ] && echo "Warning: $file not found, skip" >&2 && continue

  mimetype=$(get_mimetype "$file")

echo "$(<<EOT
--$boundary
Content-Type: $mimetype
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="$file"

  $(base64 "$file")

EOT
)"

done

# last boundary
echo "--${boundary}--"

} | tee mailcopy.tmp | /usr/lib/sendmail -t -oi
# mail has saved to file mailcopy.tmp = debug output

And example html body file (example.html):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title>some</title>
</head>
<body>

<h3>Header $epoc</h3>
<p>Hello, world!
$(date), $(uptime)
</p>

</body>
</html>