我正在尝试使用awk命令以html表格式发送邮件,如下所示:
(
echo "From: "
echo "Subject: testing of html table using awk"
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
我的文件(file.tmp)包含如下:
AAA 1 1 1 1 0 0
SAP 1 1 1 1 0 0
RTTC 1 1 1 1 0 0
PGW 1 1 1 1 0 0
但我没有以html表格格式获取邮件,而是使用html代码本身 .
AWK命令是否正确?或者我错过了什么?
2 回答
您需要添加
Content-type
标头:它已经是表格格式,我们必须给表格一个样式 . 这对我有用
awk ' BEGIN{ print "<!DOCTYPE html>\n<html>\n<head>\n<style>\ntable,th,td\n{\nborder:1px solid black ; \nborder-collapse:collapse;\n}\n</style>\n</head>\n<Body>\n<table>" } {print "<tr>" for(i=1;i<=NF;i++) print "<td>" $i"</td>" print "</tr>" } END{ print "\n</table>\n</Body>\n</html>\n" }' a.txt >> email.html; cat email.html | sendmail -t ananoymous@exampl.com