Shell 脚本:在数组中发送 html 格式的电子邮件
Shell Script : Send html formatted email while inside an array
我正在尝试通过 shell 脚本发送 html 格式的电子邮件,同时读取数组中的制表符分隔文本文件,需要一些帮助。我可以看到论坛上有多个类似的问题,我也有多个可以发送 html 电子邮件的工作脚本,但是当我在数组中时我无法适应该代码。
除此之外,我还需要使用一个变量,稍后再使用它,但无法将其放入我的代码中,例如,我有以下代码来提取名字并将首字母大写,但不确定如何合并它在我现有的代码中。
SplitName=$(echo firstname.lastname@domainname.com| cut -d'.' -f 1)
Firstname=`echo -e $SplitName | sed -r 's/\<./\U&/g'`
下面是我的主要代码和数据文件,工作正常并创建了一个 html 格式的电子邮件,但问题是当它读取 "print substr(a[user], 2) | cmd" 时它丢失了所有格式所以我收到的电子邮件确实有html 在开始时格式化,但在显示我需要的记录时不格式化。
任何帮助将不胜感激。
这是我的数据文件
10011,5-Jan,Sam,Sam@companydomain.com
10023,8-Jan,Mutthu,Mutthu@companydomain.com
10010,8-Jan,Mutthu,Mutthu@companydomain.com
10026,15-Jan,Sam,Sam@companydomain.com
10050,10-Jan,Jordan,Jordan@companydomain.com
10021,12-Jan,Andrew,Andrew@companydomain.com
这是我的代码
awk -F '\t' '{ a[] = a[] ORS [=14=] }
END {
for (user in a) {
cmd = "/usr/sbin/sendmail -v "
print "From: static.name@domain.com" | cmd
print "To: " | cmd
print "Cc: static.name@domain.com" | cmd
print "Subject: Some text here " " Some more text"| cmd
print "MIME-Version: 1.0" | cmd
print "Content-Type: text/html" | cmd
print "Content-Disposition: inline" | cmd
print "<font face=Calibri><font size=2>Some text here<br><br>"| cmd
print substr(a[user], 2) | cmd
close(cmd) } }' myfile | grep Sent >>"$HOME/maillog.txt"
下面的代码是生成htmltable
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
</style>
</head>
<body>
<table style="width:100%">
<tr>
<th>Number</th>
<th>Date</th>
<th>Name</th>
</tr>
<tr>
<td>10011</td>
<td>5-Jan</td>
<td>Mutthu</td>
</tr>
</table>
</body>
</html>
请参阅下文 link 了解更多详情。
您需要在电子邮件 headers 和 body 之间留一个空行;并且您需要将 HTML 格式添加到 table 以使其正确呈现。可能是这样的:
# -F ',' -- sample is comma-separated, not tab-separated
awk -F ',' '{ a[] = a[] ORS "<tr><td>" "</td><td>" "</td><td>" "</td><td>" "</td></tr" }
# ^^ Notice the addition of HTML
END {
for (user in a) {
cmd = "/usr/sbin/sendmail -v "
print "From: static.name@domain.com" | cmd
# Send to user, not to
print "To: " user | cmd
print "Cc: static.name@domain.com" | cmd
# What should expand to here?
# Maybe collect that in a separate array
print "Subject: Some text here " " Some more text"| cmd
print "MIME-Version: 1.0" | cmd
print "Content-Type: text/html" | cmd
# Not really useful, but why not
print "Content-Disposition: inline" | cmd
# Add an empty line
print "" | cmd
print "<font face=Calibri><font size=2>Some text here<br><br>"| cmd
print "<table><tbody>" | cmd
print substr(a[user], 2) | cmd
print "</tbody></table>" | cmd
close(cmd) } }' myfile
我保持这个简单是为了突出一般结构。了解其工作原理后,您可以添加更多修饰的 HTML 格式。
我正在尝试通过 shell 脚本发送 html 格式的电子邮件,同时读取数组中的制表符分隔文本文件,需要一些帮助。我可以看到论坛上有多个类似的问题,我也有多个可以发送 html 电子邮件的工作脚本,但是当我在数组中时我无法适应该代码。
除此之外,我还需要使用一个变量,稍后再使用它,但无法将其放入我的代码中,例如,我有以下代码来提取名字并将首字母大写,但不确定如何合并它在我现有的代码中。
SplitName=$(echo firstname.lastname@domainname.com| cut -d'.' -f 1)
Firstname=`echo -e $SplitName | sed -r 's/\<./\U&/g'`
下面是我的主要代码和数据文件,工作正常并创建了一个 html 格式的电子邮件,但问题是当它读取 "print substr(a[user], 2) | cmd" 时它丢失了所有格式所以我收到的电子邮件确实有html 在开始时格式化,但在显示我需要的记录时不格式化。
任何帮助将不胜感激。
这是我的数据文件
10011,5-Jan,Sam,Sam@companydomain.com
10023,8-Jan,Mutthu,Mutthu@companydomain.com
10010,8-Jan,Mutthu,Mutthu@companydomain.com
10026,15-Jan,Sam,Sam@companydomain.com
10050,10-Jan,Jordan,Jordan@companydomain.com
10021,12-Jan,Andrew,Andrew@companydomain.com
这是我的代码
awk -F '\t' '{ a[] = a[] ORS [=14=] }
END {
for (user in a) {
cmd = "/usr/sbin/sendmail -v "
print "From: static.name@domain.com" | cmd
print "To: " | cmd
print "Cc: static.name@domain.com" | cmd
print "Subject: Some text here " " Some more text"| cmd
print "MIME-Version: 1.0" | cmd
print "Content-Type: text/html" | cmd
print "Content-Disposition: inline" | cmd
print "<font face=Calibri><font size=2>Some text here<br><br>"| cmd
print substr(a[user], 2) | cmd
close(cmd) } }' myfile | grep Sent >>"$HOME/maillog.txt"
下面的代码是生成htmltable
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
</style>
</head>
<body>
<table style="width:100%">
<tr>
<th>Number</th>
<th>Date</th>
<th>Name</th>
</tr>
<tr>
<td>10011</td>
<td>5-Jan</td>
<td>Mutthu</td>
</tr>
</table>
</body>
</html>
请参阅下文 link 了解更多详情。
您需要在电子邮件 headers 和 body 之间留一个空行;并且您需要将 HTML 格式添加到 table 以使其正确呈现。可能是这样的:
# -F ',' -- sample is comma-separated, not tab-separated
awk -F ',' '{ a[] = a[] ORS "<tr><td>" "</td><td>" "</td><td>" "</td><td>" "</td></tr" }
# ^^ Notice the addition of HTML
END {
for (user in a) {
cmd = "/usr/sbin/sendmail -v "
print "From: static.name@domain.com" | cmd
# Send to user, not to
print "To: " user | cmd
print "Cc: static.name@domain.com" | cmd
# What should expand to here?
# Maybe collect that in a separate array
print "Subject: Some text here " " Some more text"| cmd
print "MIME-Version: 1.0" | cmd
print "Content-Type: text/html" | cmd
# Not really useful, but why not
print "Content-Disposition: inline" | cmd
# Add an empty line
print "" | cmd
print "<font face=Calibri><font size=2>Some text here<br><br>"| cmd
print "<table><tbody>" | cmd
print substr(a[user], 2) | cmd
print "</tbody></table>" | cmd
close(cmd) } }' myfile
我保持这个简单是为了突出一般结构。了解其工作原理后,您可以添加更多修饰的 HTML 格式。