Unix sendmail - html embed image not working

2019-03-20 00:39发布

With the prior posts in SO.com I have tried building my script to send email to my Outlook account with the Image inline in the email body. But the html contents are getting displayed in the html rather displaying the image. Please help.

Here is my snippet

{
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

Email I received contains the below rather displaying the image,

--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--

Can you please assist me in what am i missing

1条回答
Fickle 薄情
2楼-- · 2019-03-20 01:39

The way you use echo to print the message headers it eats all double quotes - you need to escape them with a backslash (\") to make it work.

Also, your boundary is wrong. If you define boundary=--XYZ, then each message part needs to start with ----XYZ (you need to add two dashes), otherwise your boundary should be only XYZ. And the headers of the mime parts must be separated from the bodies by an empty line.

If you really need to generate a mail from a shell script, then my advise would be to get rid of all the echo and use a heredoc instead:

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
查看更多
登录 后发表回答