Shell脚本使用sendmail嵌入多个图像

时间:2015-08-26 11:36:39

标签: linux shell unix sendmail mime

我正在使用以下脚本使用sendmail功能在邮件上嵌入多个图像。

sendmail -t <<EOT
TO: example_to@xyz.com
FROM: example_from@xyz.com
Cc: example_cc@xyz.com
SUBJECT: Phobos Report 
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=""><br/>
<img src="cid:part2.06090408.01060107" alt=""><br/>
<img src="cid:part3.06090408.01060107" alt="">
</body>
</html>

--XYZ
Content-Type: image/jpeg;name="rag1.jpg"
Content-Transfer-Encoding: base64
Content-ID: <part1.06090408.01060107>
Content-Disposition: inline; filename="rag1.jpg"

$(base64 rag1.jpg)

--XYZ

Content-Type: image/jpeg;name="rag2.jpg"
Content-Transfer-Encoding: base64
Content-ID: <part2.06090408.01060107>
Content-Disposition: inline; filename="rag2.jpg"

$(base64 rag2.jpg)

--XYZ

Content-Type: image/jpeg;name="rag3.jpg"
Content-Transfer-Encoding: base64
Content-ID: <part3.06090408.01060107>
Content-Disposition: inline; filename="rag3.jpg"

$(base64 rag3.jpg)
--XYZ--
EOT

此处仅嵌入第一张图片。所有其他人都没有被添加。这两个图像被添加为基于文本的附件。如何在此脚本上添加多个图像?

1 个答案:

答案 0 :(得分:3)

这是一个老问题,但我认为无论如何都值得回答。

您的代码存在的问题是,您在MIME边界和最后两个图像的MIME标头之间放置了空行,如下所示:

--XYZ

Content-Type: image/jpeg;name="rag2.jpg"
Content-Transfer-Encoding: base64
Content-ID: <part2.06090408.01060107>
Content-Disposition: inline; filename="rag2.jpg"

允许。您应该更正代码并在两个位置删除这些空行,如下所示:

--XYZ
Content-Type: image/jpeg;name="rag2.jpg"
Content-Transfer-Encoding: base64
Content-ID: <part2.06090408.01060107>
Content-Disposition: inline; filename="rag2.jpg"

在这里:

--XYZ
Content-Type: image/jpeg;name="rag3.jpg"
Content-Transfer-Encoding: base64
Content-ID: <part3.06090408.01060107>
Content-Disposition: inline; filename="rag3.jpg"

在边界线之前允许空行,这意味着它们将成为前一个主体的一部分,它们通常无害(但我可以组成上下文)他们不会在哪里。但不是你的情况。)

最好不要在MIME边界的任何一侧放置额外的空行。当然,这不是关于第一个MIME边界之前所需的空行,它将MIME标题分开。