How to include table in Email body

Hi Folks,

I have a requirement where I want to print the .csv file output in the form of the table in the mail body. The challenge here for me is that I cannot place the table inside the existing body. The body of the mail would be in the below format.

Hi All,

The below table gives an idea about the percentage of the consecutive days,

Day                  Date            Percentage
Day1       04/26/2020               2.3
Day 2       04/27/2020              3.4

Please do not reply to this mail.

I am able to print the strings in the mail but I am struggling to print the table output.

Thanks in Advance

Hello,
You may use Content-Type: text/html in mailx or sendmail tool and format the content of your email using HTML tags. Give it a try and let us know. Thanks :slight_smile:

HTML table example:

<table>
<tr>  
  <th>Day</th>
  <th>Date</th>
  <th>Percentage</th>
</tr>
</tr>
  <td>Day1</td>
  <td>04/26/2020</td>
  <td>2.3</td>
</tr>
</table>
1 Like

Thanks for Replying.

I have created an html file. But I am not able to incorporate the table along the rest of the message. Basically after the salutation and string after that. Can you let me know how to incorporate within a message.

Hello amiteshsahu,
Could you please share what you've tried (masking sensitive details), which mail tool are you using, OS/Shell details..
Thanks.

awk -F "," 'NR>1 {print "Day" $7",",$6",",$8}' ${filename} > ${SPLIT_FILENAME}
	awk 'BEGIN{
	FS=","
	print  "MIME-Version: 1.0"
	print  "Content-Type: text/html"
	print  "Content-Disposition: inline"
	print  "<HTML>""<TABLE border="1"><TH>Day</TH><TH>Date</TH><TH>Percentage</TH>"
	}
	{
	printf "<TR>"
	for(i=1;i<=NF;i++)
	printf "<TD>%s</TD>", $i
	print "</TR>"
	}
	END{
	print "</TABLE></BODY></HTML>"
	}
	' "${filename%.*}""_Split"".csv" > ${HTML_FILENAME}
EMAIL_SUBJECT="Alert on Date<>"
EMAIL_BODY="Hi All,\n\nThe below table gives an idea about the percentage of the consecutive days,\n\n`cat ${HTML_FILENAME}`\n\nPlease do not reply to this mail.
EMAIL_ATTACHMENT=${filename}
echo -e ${EMAIL_BODY}  | mutt -e -n "set Content-Type:text/html" -s "${EMAIL_SUBJECT}" -a ${EMAIL_ATTACHMENT} -- $EMAIL_LIST

Above is the actual code which creates the HTML file and sends email.

Please adapt to suit your needs.

user@host~>cat split.csv
Day,Date,Percentage
Day1,04/26/2020,2.3
Day2,04/27/2020,3.4

user@host~>awk -F "," '
    BEGIN {
        print "Hi All,\n\nThe below table gives an idea about the percentage of the consecutive days:\n\n"
    }
    NR==1 {
        print "<table>\n<tr><th>Day</th><th>Date</th><th>Percentage</th><tr>"
    }
    NR>1 {
        printf "<tr>"
        for(i=1;i<=NF;i++) {
            printf "<td>%s</td>", $i
        }
        printf "</tr>\n"
    }
    END {
        print "</table>\n\nPlease do not reply to this mail."
    }
' split.csv >! test.html

user@host~>mutt -e "set content_type=text/html" user@domain.com -s "subject" < test.html
user@host~>
1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.