title: Using sendmail for HTML body and binary attachment
body: <p>Objective: To send mail (using sendmail) with HTML body and binary attachment.</p>
  
  <p>Followed the guidelines specified in the following links</p>
  
  <p><a href="http://www.unix.com/shell-programming-scripting/159522-sendmail-html-body-attachment-2.html">http://www.unix.com/shell-programming-scripting/159522-sendmail-html-body-attachment-2.html</a></p>
  
  <p><a href="http://www.unix.com/shell-programming-scripting/58448-sendmail-attachment.html">http://www.unix.com/shell-programming-scripting/58448-sendmail-attachment.html</a></p>
  
  <p>It is working to the extent that, either HTML body or the binary attachment with uuencode, but not both.</p>
  
  <p>Given below is a snippet of the shell script to sendmail.  With this, the HTML body is coming fine, but the attachment is getting encoded/decoded wrongly and unable to view the same.</p>
  
  <p>Please advise.</p>
  
  <pre><code>#!/usr/bin/ksh
  
  export MAILFROM="noreply@site.dom"
  export MAILTO="somebody@somesite.com"
  export SUBJECT="Test PDF for Email"
  export BODY="email_body.htm"
  export ATTACH="file.pdf"
  export MAILPART=`uuidgen` ## Generates Unique ID
  (
   echo "From: $MAILFROM"
   echo "To: $MAILTO"
   echo "Subject: $SUBJECT"
   echo "MIME-Version: 1.0"
   echo "Content-Type: multipart/mixed; boundary=\"-$MAILPART\""
   echo "---$MAILPART"
   echo "Content-Type: text/html"
   echo "Content-Disposition: inline"
   cat $BODY
   echo "---$MAILPART"
   echo 'Content-Type: application/pdf; name="'$(basename $ATTACH)'"'
   echo "Content-Transfer-Encoding: base64"
   echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
   uuencode -m $ATTACH $(basename $ATTACH)
   echo "---$MAILPART--"
  ) | /usr/sbin/sendmail $MAILTO
  </code></pre>
  
  <p>I am using HP-UX ia64.
  Have searched through the forum and web and found references mostly to PHP, Python, etc.</p>