Sending HTML Email

1) Can you please provide me with a best example of how to send an HTML email via mutt or mail or send email commands using bash shell.

2) I tried below but i am not able to get the variable values in the HTML email output.

(cat <<-EOT
<html>
<head><title></title> </head>
<body>
<p>Hello, world!</p>
<table border="1">
<tr>
        <th>HASH_TOTALS</th>
        <th>HASH_TOTALS_COMP</th>
        <th>DIFFERENCE</th>
</tr>
</tr>
        <td>`printf "%s" "$TOT_LOAN_ROW_COUNT"`</td>
        <td>$COMP_LOAN_ROW_COUNT</td>
        <td>$COMP_LOAN_ROW_COUNT</td>
</tr>
</table>
</body>
</html>
EOT
) | mutt -e "my_hdr Content-Type: text/html" "xyz@123456.com" -s "html test"

Output:

It is just printing headers but not expanding the variables.

thank you.

Try simply echoing:

{
        echo "
        <html>
        <head><title></title> </head>
        <body>
                <p>Hello, world!</p>
                <table border="1">
                <tr>
                        <th>HASH_TOTALS</th>
                        <th>HASH_TOTALS_COMP</th>
                        <th>DIFFERENCE</th>
                </tr>
                <tr>
                        <td>${TOT_LOAN_ROW_COUNT}</td>
                        <td>${COMP_LOAN_ROW_COUNT}</td>
                        <td>${COMP_LOAN_ROW_COUNT}</td>
                </tr>
                </table>
        </body>
        </html>
        "
} | mutt -e "my_hdr Content-Type: text/html" "xyz@123456.com" -s "html test"
1 Like

What shell are you using? in my bash, it works perfectly, expanding the variables as desired. Try it without the pipe to mutt first.

how do i put thousand seperator & () for negative numbers inside the HTML table like

519,090,514.68 

&

(0.32)

for -.32.

I am using bc for finding the difference from which i got negative numbers example:

`echo ${TOT_BOOK_VALUE_DOLLARS}-${COMP_BOOK_VALUE_DOLLARS} | bc`

Yes, you can use bc for floating point comparison and sed to put thousand separator.

Here is an example checking the value of variable: TOT_LOAN_ROW_COUNT

if [ $( echo "$TOT_LOAN_ROW_COUNT < 0" | bc ) -eq 1 ]
then
        TOT_LOAN_ROW_COUNT="(${TOT_LOAN_ROW_COUNT})"
else
        TOT_LOAN_ROW_COUNT=$( echo "$TOT_LOAN_ROW_COUNT" | sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta' )
fi

With some implementations of the printf utility (not all, because it is not required to support either the apostrophe flag or floating point), you can easily format the value to contain a thousands separator and then add the parenthesis for negative values with sed:

printf "%'.2f\n" "$amount" | sed 's/^\(-.*\)/(&)/'

Regards,
Alister

I executed the below statement i still see the hyphen symbol "-" prepended, is there a way to remove that as well.

printf "%'.2f\n" "-501.12" | sed 's/^\(-.*\)/(&)/'

Output:

(-501.12)

thanks much, appreciate it.

Try

sed 's/^\(-.*\)/(&)/; s/-//'

If your printf supports the apostrophe flag for the thousands separator, and if you are confident that you won't have to run the script on a different operating system which might not support that flag, and if the only issue is the negative sign, you can modify the sed to strip it as it adds the parenthesis.

printf "%'.2f\n" "-501.12" | sed 's/^-\(.*\)/(\1)/'

Regards,
Alister

Trying to understand what you have provided.

sed 's/^-\(.*\)/(\1)/' is similar to sed 's/^-.*/(\1)'

^ start searching for - then if found replace it with parentheses () along with that replace the first character - of the standard output piped in as input. I am I correct please help.