使用awk命令以html表格式发送邮件

问题描述 投票:0回答:3

我正在尝试使用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 [email protected]

我的文件(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命令是否正确?或者我错过了什么?

bash shell awk
3个回答
5
投票

您需要添加Content-type标头:

(
    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 [email protected]

0
投票

它已经是表格格式,我们必须给表格一个样式。这对我有用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 [email protected]


0
投票

您需要指定用于解析html的内容类型。 U也可以为awk使用选项-F,它指定分隔符(默认为空格)。

(
    echo "Subject: $subject"
    echo "Content-type: text/html"
    echo "To:" $emailAddress
    awk 'BEGIN{print "<table border=1 cellspacing=2 cellpadding=2> {print "<tr>";for(i=1;i<=NF;i++)print "<td>" $i"</td>";print  "</tr>"} END{print "</table>"}' $fileName
) | sendmail -t
© www.soinside.com 2019 - 2024. All rights reserved.