如何通过使用mail或mailx命令(Centos / Redhat)从bash脚本以HTML格式发送电子邮件

时间:2019-04-12 17:58:02

标签: bash html-email mailx

我正在尝试从bash脚本发送一封电子邮件,该脚本的正文中应该有HTML表格, 我在Redhat中使用mail命令。但是它一直以文本文件的形式发送给我。

difference=`expr $artu_removed - $artu_added`

mail  -s "Built notification" test@gmail.com << 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

任何人都可以让我知道该怎么办吗,我正在使用Redhat,而不是ubuntu

谢谢

1 个答案:

答案 0 :(得分:0)

通过邮件尝试(不要忘记添加变量):

mail -a "Content-type: text/html" -s "HTML message" test@gmail.com << 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

如果您需要通过mailx从CentOS / RedHat发送,请使用它:

mail -s "$(echo -e "HTML message\nContent-Type: text/html")" test@gmail.com << EOF

试试看吧

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")" test@outlook.com << 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>
...