[英]Shell Script : Send html formatted email while inside an array
I am trying to send html formatted email via shell script while reading a tab separated text file in array and need some help. 我正在尝试通过shell脚本发送html格式的电子邮件,同时读取数组中的制表符分隔的文本文件,并且需要一些帮助。 I can see there were multiple similar question on forum and I also have multiple working scripts which can send html emails but I am not able to fit that code when I am inside an array.
我可以在论坛上看到多个类似的问题,并且我还有多个可以发送html电子邮件的工作脚本,但是当我位于数组中时,我无法容纳该代码。
apart form this I also need to use a variable and later use it but cant fit it inside my code, for example i have below code to extract first name and make the first letter uppercase but not sure how to merge it in my existing code. 除此以外,我还需要使用一个变量,然后再使用它,但不能将其适合我的代码,例如,我有下面的代码来提取名字并将首字母大写,但是不确定如何在现有代码中合并它。
SplitName=$(echo firstname.lastname@domainname.com| cut -d'.' -f 1)
Firstname=`echo -e $SplitName | sed -r 's/\<./\U&/g'`
below is my main code and data file and is working fine and creates a html formatted email but the problem is when it reads "print substr(a[user], 2) | cmd" it looses all formatting so the email I receive do have html formatting at start but not when it show me the records I need. 下面是我的主要代码和数据文件,可以正常工作并创建html格式的电子邮件,但问题是,当它读取“ print substr(a [user],2)| cmd”时,它将失去所有格式,因此我收到的电子邮件确实有开始时为html格式,但向我显示我需要的记录时则不会。
Any help will be appreciated . 任何帮助将不胜感激 。
Here is my data file 这是我的数据文件
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
Here is my code 这是我的代码
awk -F '\t' '{ a[$4] = a[$4] ORS $0 }
END {
for (user in a) {
cmd = "/usr/sbin/sendmail -v " $4
print "From: static.name@domain.com" | cmd
print "To: " $4 | cmd
print "Cc: static.name@domain.com" | cmd
print "Subject: Some text here " $2 " 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"
Below code is to generate html table 下面的代码是生成html表
<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>
please refer below link for more details. 请参阅下面的链接以获取更多详细信息。 Shell script -How to group test file records based on column value and send email to corresponding receipents.?
Shell脚本-如何根据列值对测试文件记录进行分组并将电子邮件发送到相应的收货人?
You need an empty line between the email headers and the body; 您需要在电子邮件标题和正文之间留空行; and you need to add HTML formatting to the table in order for it to render correctly.
并且您需要在表格中添加HTML格式,以便表格能够正确呈现。 Something like this, perhaps:
大概是这样的:
# -F ',' -- sample is comma-separated, not tab-separated
awk -F ',' '{ a[$4] = a[$4] ORS "<tr><td>" $1 "</td><td>" $2 "</td><td>" $3 "</td><td>" $4 "</td></tr" }
# ^^ Notice the addition of HTML
END {
for (user in a) {
cmd = "/usr/sbin/sendmail -v " $4
print "From: static.name@domain.com" | cmd
# Send to user, not to $4
print "To: " user | cmd
print "Cc: static.name@domain.com" | cmd
# What should $2 expand to here?
# Maybe collect that in a separate array
print "Subject: Some text here " $2 " 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
I kept this simple to highlight the general structure. 我保持简单以突出总体结构。 You can add more embellished HTML formatting to your heart's content once you understand how this works.
一旦了解了它的工作原理,就可以在您的内容中添加更多修饰的HTML格式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.