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:


and I have this awk in a file named HTML:

awk 'BEGIN {
    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>`"
    printf "`<TD>%s</TD>`", $i
    print "`</TR>`"
    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.


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

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


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

  cat > $TMP_FL <<EOF
From: $FROM
`echo -e $MAILTO`
Reply-To: $REPLY_TO
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.



for attach in "xxxxx yyyyyy"
  attach_file >> $TMP_FL

  echo -e "\n--$BOUNDARY--\n" >> $TMP_FL
  • Thanks for the input but couldn't read the article completely as its being premium :( – LIJO May 31 '15 at 14:46

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.