How to Mail Script Output in Table Format

how to mail script output in table format

#!/bin/bash

input="/path/to/your/file.txt"
tmpfile="/path/to/tmpfile.html"

echo 'Content-Type: text/html; charset="us-ascii" ' > "$tmpfile"
awk 'BEGIN{print "<html><body><table border=1>"} {print "<tr>";for(i=1;i<=NF;i++)print "<td>" $i"</td>";print "</tr>"} END{print "</table></body></html>"}' "$input" >> "$tmpfile"
mail -s "test" abc@xyz.com < "$tmpfile"

Source: http://www.unix.com/302556864-post5.html

Unable to get output in table format through shell

The problem is likely your mail program which will encode your piped input as:

Content-Type: text/plain; charset=us-ascii

(given your mail is likely snail, Berkeley mailx, or heirloom_mailx -- all derived from various iterations of Berkeley mail with the latest, currently maintained version s-nail 14.9.11-1 (Homepage))

If your mail is one of the derivations that provides the -a (attach) option, that is probably the way to go. While the body of your e-mail will still be Content-Type: text/plain, your attachment will come through as Content-Type: text/html, which most mail readers will display within the e-mail itself (which appears to be your goal).

With just a few tweaks to your routine preparing the output from df as a table, what you have works quite well. The only tweaks made (optional, but help), were to use a quick sed substitution to replace "Mounted on" with "Mounted_on" to provide a consistent number of fields for awk to work with, and then to set the table "width=60%" to prevent the table from rendering scrunched (technical term).

For example, including the tweaks, you could do:

tmp=/home/david/tmp/df.html
echo '<html><body><table border=1 width=60%>' > "$tmp"
df | sed 's/Mounted\son/Mounted_on/' |
awk '{print "<tr>";for(i=1;i<=NF;i++)print "<td>" $i"</td>";print "</tr>"}
END
{print "</table></body></html>"}' >> "$tmp"
echo "df attached" | mailx -s "wizard df" -a "$tmp" david@nirvana

(obvious note: change the path assigned to tmp as required for your system as well as the e-mails)

Aside from the tweaks, the only change as mentioned above is to "attach" the html file to the message rather than dumping it into the message. Most variations of mail (but not all) provide the -a (attach) option. This provides a simple mechanism for sending files as a properly formed and encoded attachment. To provide the body of the mail message, I just included a simple "df attached" and used echo to pipe the text to the mail command. As shown above, the mail command was:

echo "df attached" | mailx -s "wizard df" -a "$tmp" david@nirvana

Then checking in an older version of Thunderbird, you find your tabular output of the df information, e.g.

e-mail results of html by attachment

Look things over and let me know if this is what you were attempting and whether you were able to find the -a (attach) option in your version of the command line mailer (if not, I'd recommend checking whether your distro provides s-nail or heirloom-mailx. For a tiny package, those two implementations provide robust command line mail capabilities)

Edit Per-Request to Encode as "text/html" in Body of Message

Here is where much will depend on which mail/mailx/s-nail package you have. With s-nail (which is generally aliased as mailx as well), the -M option allows you to specify the Content-type for text received on stdin for the body of the e-mail. In this case, all you need to is redirect "$tmp" to your mail command while specifying -M "text/html" as an option, e.g. change the last line above to:

 mailx -M "text/html" -s "wizard df redir -M" david@nirvana < "$tmp"

With s-nail/mailx that results in:

s-nail -M "text/html"

(note: I had to send from another machine, e.g. Archlinux packages s-nail as mailx while OpenSuSE packages heirloom-mailx)

With other packages like heirloom-mailx it appears the attachment is the only way to have mailx set Content-type based on extension. It does so by reading a MIME types file with syntax of the form:

 type/subtype      extension [extension . . .]

So in this case, it looks like you would have to either do a mail header re-write, or manually create the body of the mail message including the Content-type for the table part of the message similar to:

Content-Type: text/html; charset=us-ascii

(or whatever your charset should be)

That's why I suggested, if you are not using the mail/mailx based on s-nail, then go see if your distro offers the s-nail package, or it would even be worth building from source and dropping in /usr/local/bin.

Shell script to mail script output in table format

$ wc file1 file2 file3 file4 |
awk 'BEGIN{print "Filename Destname rowcount bytesize"}
$NF=="total"{exit}
{print $NF, "default", $1, $3}' file |
column -t |
mail -s "table" you@host.tld

Output:

Filename  Destname  rowcount  bytesize
file1 default 1488 2248
file2 default 123 657
file3 default 123 456
file4 default 567 124

Sending mail with html table format using awk command

You need to add a Content-type header:

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


Related Topics



Leave a reply



Submit