0

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";    
  echo;     
  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
2

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

s-nail have it !

email=foo@base

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
)

.
EOF

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
0

Using sendmail:

TO_ADDRESS="abc@example.com"
SUBJECT="Test Mail"

cat << --OEF--
Subject: $SUBJECT
TO: $TO_ADDRESS
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="MAIL_BOUNDARY"


--MAIL_BOUNDARY
Content-Type: multipart/alternative;
boundary="MAIL_BOUNDARY2"

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

$SUBJECT

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

--OEF--
cat MailBody.html 

cat << --OEF--

--MAIL_BOUNDARY2--

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

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

--MAIL_BOUNDARY--
--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.