![电子邮件正文中的多个 html 文件作为单独的表块](https://linux22.com/image/66037/%E7%94%B5%E5%AD%90%E9%82%AE%E4%BB%B6%E6%AD%A3%E6%96%87%E4%B8%AD%E7%9A%84%E5%A4%9A%E4%B8%AA%20html%20%E6%96%87%E4%BB%B6%E4%BD%9C%E4%B8%BA%E5%8D%95%E7%8B%AC%E7%9A%84%E8%A1%A8%E5%9D%97.png)
我正在尝试通过附加到电子邮件正文来生成和发送 html 文件。我尝试使用 awk 生成和发送一个文件。例如。输入文件 MARTINI 具有以下记录:
1554894,2015-04-16,00:21:52,processes.martini_gsicorptradeeventoutput.instancecount,0,1,UP
1554793,2015-04-15,22:03:52,processes.martini_gsicorptradeeventoutput.instancecount,2,0,DOWN
我在名为 HTML 的文件中有这个 awk:
awk 'BEGIN {
FS=","
print "MIME-Version: 1.0"
print "To:[email protected]"
print "From:[email protected]"
print "Subject: Health check"
print "Content-Type: text/html"
print "Content-Disposition: inline"
print "<HTML>""<TABLE border="1"><TH>Ref_id</TH><TH>EOD</TH><TH>Time</TH><TH>Process</TH><TH>Desc</TH><TH>Instance</TH><TH>Status</TH>"
}
{
printf "`<TR>`"
for(i=1;i<=NF;i++)
printf "`<TD>%s</TD>`", $i
print "`</TR>`"
}
END {
print "`</TABLE></BODY></HTML>`"
} ' /home/martini > /home/martini_html
后来我通过电子邮件发送这个文件cat MARTINI_HTML | /usr/sbin/sendmail -t
。这一直有效到这里。但现在我有两个新任务:
如何将多个文件(比如 MARTINI1、MARTINI2 ... 等)转换为 html 文件,以及如何将它们作为单独的表块而不是单个表附加到电子邮件正文中?某些邮件实用程序(例如)mailx
不可用。
答案1
您需要设置内容类型到多部分/混合并定义边界(分隔符字符串),并在每个文件之间添加该字符串的实例。我不久前在这篇文章中提供了一些例子:仅使用 Bash 和 Sendmail 将多个输入文件和/或管道作为电子邮件中的附件发送
一些代码:
# ========================
# Attach file to msg
# ========================
attach_file() {
cat >> $TMP_FL << EOF
--$BOUNDARY
Content-Type: $MIMETYPE; name="$MAIL_FL"
Content-Transfer-Encoding: 8bit
Content-Disposition: attachment; filename="$MAIL_FL"
`cat $ATTACHMENT`
EOF
}
# ========================
# ========================
create_msg() {
cat > $TMP_FL <<EOF
From: $FROM
`echo -e $MAILTO`
Reply-To: $REPLY_TO
Subject: $SUBJECT_LINE
Content-Type: multipart/mixed; boundary="$BOUNDARY"
This is a MIME formatted message. If you see this text it means that your
email software does not support MIME formatted messages, but as plain text
encoded you should be ok, with a plain text file.
--$BOUNDARY
EOF
...
for attach in "xxxxx yyyyyy"
do
ATTACHMENT=$attach
attach_file >> $TMP_FL
done
...
echo -e "\n--$BOUNDARY--\n" >> $TMP_FL
}