Shell脚本:在数组内时发送html格式的电子邮件 [英] Shell Script : Send html formatted email while inside an array

查看:96
本文介绍了Shell脚本:在数组内时发送html格式的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过Shell脚本发送html格式的电子邮件,同时读取数组中的制表符分隔的文本文件,并且需要一些帮助。我可以在论坛上看到多个类似的问题,并且我也有多个可以发送html电子邮件的工作脚本,但是当我在数组中时,我无法容纳该代码。

I am trying to send html formatted email via shell script while reading a tab separated text file in array and need some help. 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.

除此以外,我还需要使用一个变量,然后再使用它,但不能将其放入我的代码中,例如,我有下面的代码来提取名字并将首字母大写,但是不确定如何在我的代码中合并它

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'`

是我的主要代码和数据文件,可以正常工作并创建html格式的电子邮件,但问题是当它读取 print substr(a [user],2)| cmd时,它会松开所有格式,因此我收到的电子邮件确实包含html

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.

任何帮助将不胜感激。

这是我的数据文件

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[$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"

下面的代码是生成html表

Below code is to generate html table

<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>

请参考以下链接以获取更多详细信息。
Shell脚本-如何基于列值对测试文件记录进行分组,以及如何将电子邮件发送到相应的接收者。?

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.?

推荐答案

您需要在电子邮件标题和正文之间留空行;并且您需要在表格中添加HTML格式,以便表格能够正确呈现。可能是这样的:

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. 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

我简单地强调了一般结构体。了解其工作原理后,您可以在自己的内容中添加更多修饰的HTML格式。

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.

这篇关于Shell脚本:在数组内时发送html格式的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆