Skip to content
Advertisement

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

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.

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

Advertisement

Answer

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

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.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement