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

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

我正在尝试从 bash 脚本发送电子邮件,正文中应包含 HTML table, 我在 Redhat 中使用邮件命令。但它一直以文本文件的形式发送给我。

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

谢谢

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

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

试试 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")" 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>
...