Search code examples
bashhtml-emailmailx

How to sent Email as HTML from bash script by using mail or mailx command (Centos/Redhat)


I am trying to sent an email from bash script which should have HTML table in body , I am using mail command in Redhat . but its keep sending me as Text file .

difference=`expr $artu_removed - $artu_added`

mail  -s "Built notification" [email protected] << EOF


<html>

<head><title></title>
</head>
<body>

<table>  
 <tr>
  <Td> Before </td>  <td>after </td>  <td>differece </td>
 </tr>

<tr>
  <Td> $_before </td>  <td>$_after </td>  <td>$difference </td>
 </tr>

</table>

 Before:$_before
 After:$_after
 Difference:$difference
</body>
</html>
EOF

Can any one please let me know what shall I do, I am using Redhat, Not ubuntu

Thanks


Solution

  • Try it via mail (do not forget to add your variables):

    mail -a "Content-type: text/html" -s "HTML message" [email protected] << EOF
    <html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Title</title>
    </head>
    <body>
        <table>
            <tr>
                <td> Before </td>
                <td> After </td>
                <td> Differece </td>
            </tr><tr>
                <td> $_before </td>
                <td> $_after </td>
                <td> $difference </td>
            </tr>
        </table>
        Before: $_before
        After: $_after
        Difference: $difference
    </body>
    </html>
    EOF
    

    If you need send from CentOS/RedHat via mailx use it:

    mail -s "$(echo -e "HTML message\nContent-Type: text/html")" [email protected] << EOF
    

    Try it for outlook:

    mail -s "$(echo -e "HTML message\nContent-Transfer-Encoding: 7bit\nUser-Agent: Heirloom mailx 12.4 7/29/08\nMIME-Version: 1.0\nContent-Type: text/html; charset=us-ascii\nContent-Transfer-Encoding: 7bit")" [email protected] << EOF
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
    <html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
        <title>Title</title>
    ...