Unix sendmail - html嵌入图像不起作用

Sat*_*thy 10 html unix embed base64

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

这是我的片段

{
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
Run Code Online (Sandbox Code Playgroud)

我收到的电子邮件包含以下内容而非显示图片,

--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--
Run Code Online (Sandbox Code Playgroud)

你能帮助我解决我所遗漏的问题

mat*_*ata 20

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

而且,你的边界是错误的.如果你定义boundary=--XYZ,那么每个消息部分都需要从----XYZ(你需要添加两个破折号)开始,否则你的边界应该只是XYZ.并且mime部分的标题必须通过空行与主体分开.

如果你真的需要从shell脚本生成邮件,那么我的建议就是摆脱所有的回声并使用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
Run Code Online (Sandbox Code Playgroud)