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.

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:

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