【问题标题】:Unix sendmail - html embed image not workingUnix sendmail - html 嵌入图像不起作用
【发布时间】:2013-08-01 03:18:15
【问题描述】:

在 SO.com 中的先前帖子中,我尝试构建脚本以将电子邮件发送到我的 Outlook 帐户,并在电子邮件正文中嵌入图像。但是 html 内容正在显示在 html 中,而不是显示图像。请帮忙。

这是我的sn-p

{
echo "TO: XXX@YYY.com"
echo "FROM: TEST_IMAGE@YYY.com>"
echo "SUBJECT: Embed image test"
echo "MIME-Version: 1.0"
echo "Content-Type: multipart/related;boundary="--XYZ""

echo "--XYZ"
echo "Content-Type: text/html; charset=ISO-8859-15"
echo "Content-Transfer-Encoding: 7bit"
echo "<html>"
echo "<head>"
echo "<meta http-equiv="content-type" content="text/html; charset=ISO-8859-15">"
echo "</head>"
echo "<body bgcolor="#ffffff" text="#000000">"
echo "<img src="cid:part1.06090408.01060107" alt="">"
echo "</body>"
echo "</html>"


echo "--XYZ"
echo "Content-Type: image/jpeg;name="sathy.jpg""
echo "Content-Transfer-Encoding: base64"
echo "Content-ID: <part1.06090408.01060107>"
echo "Content-Disposition: inline; filename="sathy.jpg""
echo $(base64 sathy.jpg)
echo "' />"
echo "--XYZ--"
}| /usr/lib/sendmail -t

我收到的电子邮件包含以下内容,而不是显示图像,

--XYZ
Content-Type: text/html; charset=ISO-8859-15
Content-Transfer-Encoding: 7bit
<html>
<head>
<meta http-equiv=content-type content=text/html
</head>
<body bgcolor=#ffffff text=#000000>
<img src=cid:part1.06090408.01060107 alt=>
</body>
</html>
--XYZ
Content-Type: image/jpeg;name=sathy.jpg
Content-Transfer-Encoding: base64
Content-ID: <part1.06090408.01060107>
Content-Disposition: inline; filename=sathy.jpg
/9j/4QAYRXhpZgAASUkqAAgAAAAAAAAAAAAAAP/sABFEdWNreQABAAQAAAAoAAD/4QNxaHR0cDov
....base64 values.....
/>
--XYZ--
----XYZ--

你能帮我解决我缺少什么

【问题讨论】:

    标签: html unix base64 embed


    【解决方案1】:

    您使用echo 打印邮件标头的方式会使用所有双引号 - 您需要使用反斜杠 (\") 将它们转义以使其正常工作。

    另外,你的边界是错误的。如果你定义boundary=--XYZ,那么每个消息部分需要以----XYZ开头(你需要添加两个破折号),否则你的边界应该只有XYZ。并且 mime 部分的标题必须与正文用空行分隔。

    如果你真的需要从 shell 脚本生成邮件,那么我的建议是去掉所有的 echo 并改用 heredoc:

    sendmail -t <<EOT
    TO: XXX@YYY.com
    FROM: <TEST_IMAGE@YYY.com>
    SUBJECT: Embed image test
    MIME-Version: 1.0
    Content-Type: multipart/related;boundary="XYZ"
    
    --XYZ
    Content-Type: text/html; charset=ISO-8859-15
    Content-Transfer-Encoding: 7bit
    
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-15">
    </head>
    <body bgcolor="#ffffff" text="#000000">
    <img src="cid:part1.06090408.01060107" alt="">
    </body>
    </html>
    
    --XYZ
    Content-Type: image/jpeg;name="sathy.jpg"
    Content-Transfer-Encoding: base64
    Content-ID: <part1.06090408.01060107>
    Content-Disposition: inline; filename="sathy.jpg"
    
    $(base64 sathy.jpg)
    --XYZ--
    EOT
    

    【讨论】:

    • 1/0 谢谢你。如此清晰和详细的解释。你摇滚!!
    • 很棒的反应!这对我来说也适用于常规的mail,使用mail -r "&lt;test_image@YYY.com&gt;" -s "Embed image test" -a "MIME-Version: 1.0" -a "Content-Type: multipart/related;boundary=\"XYZ\"" XXX@YYY.com &lt;&lt;EOT,然后使用--XYZ 和随后的所有内容启动heredoc。
    • 如何扩展此功能以使用多张图片?
    • @sugunan。您在 和适当的 img 定义部分添加另一个
    猜你喜欢
    • 2017-07-10
    • 2015-11-20
    • 2021-11-26
    • 2016-02-15
    • 1970-01-01
    • 1970-01-01
    • 2020-06-06
    • 2012-04-28
    • 2015-01-03
    相关资源
    最近更新 更多