Multiple html files in Email body as separate table blocks

emailhtml

I am trying to generate and send html files by attaching to email body. I tried using awk for generating and sending one file. eg. the input file MARTINI has these records:

1554894,2015-04-16,00:21:52,processes.martini_gsicorptradeeventoutput.instancecount,0,1,UP
1554793,2015-04-15,22:03:52,processes.martini_gsicorptradeeventoutput.instancecount,2,0,DOWN 

and I have this awk in a file named HTML:

awk 'BEGIN {
    FS=","
    print  "MIME-Version: 1.0"
    print  "To:lijo@abc.com"
    print  "From:lijo@abc.com"
    print  "Subject: Health check"
    print  "Content-Type: text/html"
    print  "Content-Disposition: inline"
    print  "<HTML>""<TABLE border="1"><TH>Ref_id</TH><TH>EOD</TH><TH>Time</TH><TH>Process</TH><TH>Desc</TH><TH>Instance</TH><TH>Status</TH>"
    }
    {
    printf "`<TR>`"
    for(i=1;i<=NF;i++)
    printf "`<TD>%s</TD>`", $i
    print "`</TR>`"
    }
END {
    print "`</TABLE></BODY></HTML>`"
} ' /home/martini > /home/martini_html

Later I send this file through email cat MARTINI_HTML | /usr/sbin/sendmail -t . This works until here. But now I have 2 new tasks:

How to convert multiple files Say MARTINI1, MARTINI2 … etc into html files and how to attach them in email body as separate table block and not as a single table?
Some mail utilities such as mailx is not available.

Best Answer

You'll need to set the content-type to multipart/mixed and define a boundary (separator string), and add an instance of the string between each file. I provided a few examples of this a while back in this post: Using just Bash and Sendmail to send multiple input files and / or a pipe as attachments in an email

A bit of the code:

# ========================
# Attach file to msg
# ========================
attach_file() {

cat >> $TMP_FL << EOF

--$BOUNDARY
Content-Type: $MIMETYPE; name="$MAIL_FL"
Content-Transfer-Encoding: 8bit
Content-Disposition: attachment; filename="$MAIL_FL"

`cat $ATTACHMENT`
EOF
}

# ========================
# ========================
create_msg() {

  cat > $TMP_FL <<EOF
From: $FROM
`echo -e $MAILTO`
Reply-To: $REPLY_TO
Subject: $SUBJECT_LINE
Content-Type: multipart/mixed; boundary="$BOUNDARY"

This is a MIME formatted message.  If you see this text it means that your
email software does not support MIME formatted messages, but as plain text
encoded you should be ok, with a plain text file.

--$BOUNDARY

EOF

...
for attach in "xxxxx yyyyyy"
do 
  ATTACHMENT=$attach
  attach_file >> $TMP_FL
done
...


  echo -e "\n--$BOUNDARY--\n" >> $TMP_FL
}
Related Question