I need to send a mail with an attachment and body of mail in table format.I have used the below code to send mail. but unable to attache file.

 (echo "From: ";    
  echo "Subject: testing of table using awk";  
  echo "Content-type: text/html";    
  awk 'BEGIN{print "<table>"} {print "<tr><tr>";for(i=1;i<=NF;i++)print "<td><td>" $i"</td></td>";print  "</tr></tr>"} END{print "</table>"}' input1.txt;
 ) | sendmail xxx@yyy.com

Try to use a version of mail command supporting -a (attachment) switch . It's the more reliable solution.

s-nail have it !


mail -v -s subject foo@bar -a file.txt -q - <<EOF 
$(awk '
    BEGIN{print "<table>"}
        print "<tr><tr>";
        for(i=1;i<=NF;i++) {
            print "<td><td>" $i"</td></td>";
            print  "</tr></tr>";
    END{print "</table>"}' input1.txt


Check man mail if you need to add headers and such

  • i need body of mail with table format as well... – msp9011 Mar 7 '18 at 17:44
  • mail command is not converting to table format. it is printing with html tags.. – msp9011 Mar 7 '18 at 17:56
  • Sure, you need to read the man mail to add headers as already said – Gilles Quenot Mar 7 '18 at 18:26

Using sendmail:

SUBJECT="Test Mail"

cat << --OEF--
Subject: $SUBJECT
MIME-Version: 1.0
Content-Type: multipart/mixed;

Content-Type: multipart/alternative;

Content-Type: text/plain; charset=utf-8


Content-Type: text/html; charset=utf-8

cat MailBody.html 

cat << --OEF--


Content-Type: application/zip; name=file.zip
Content-Disposition: attachment; filename=file.zip
Content-Transfer-Encoding: base64

base64  /opt/file.zip
cat << --OEF--

) |  /usr/sbin/sendmail $TO_ADDRESS 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.