bash 使用 awk 命令以 html 表格格式发送邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19927710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending mail with html table format using awk command
提问by kattashri
I am trying to send mail in html table format using awk command as below:
我正在尝试使用 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]
And my file(file.tmp) contains as below:
我的文件(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
But I am not getting the mail in html tabular format but instead with html code itself.
但我不是以 html 表格格式接收邮件,而是使用 html 代码本身。
Is the AWK command correct? Or am I missing something ?
AWK 命令是否正确?或者我错过了什么?
回答by pobrelkey
You need to add a Content-type
header:
您需要添加一个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]
回答by Sathish
It is already in table format only thing is we have to give a style to the table.
This worked for me 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]
它已经是表格格式,唯一的事情是我们必须给表格一个样式。这对我有用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]
回答by singh
You need to specify Content type for parsing html. U can also use option -F for awk which specifies the delimiter( default is space ).
您需要指定用于解析 html 的内容类型。你也可以对 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