Insert an image in HTML mail sent from Shell script

Hi Shell Experts
I am trying to insert an image into HTML email through shell script send mail, I have the email text file which included body and the images included in it in html format.
Through my Shell script, I am calling the text file and send it through email so it it sends the email with image as I required..
The mail body looks perfectly file but images are not showing in the email..

See my code below

 
#!/bin/bash
outputFile="/home/u35731/dev/mail.txt"
(
echo "From: donotreply@xyz.com"
echo "To: anji009@xyz.com"
echo "MIME-Version: 1.0"
echo "Subject: Test"
echo "Content-Type: text/html; charset=ISO-8859-15"
echo "Content-Transfer-Encoding: 7bit"
cat $outputFile
) | sendmail -t

Can you please suggest where am i doing wrong here ?

my mail.txt files is too big to paste here, but the image and mail body related information looks like below in html format

</o:shapelayout></xml><![endif]--></head><body lang=EN-SG link=blue vlink=purple><div class=WordSection1><p class=MsoNormal><o:p>�</o:p></p><p class=MsoNormal><o:p>�</o:p></p><div align=center><table class=MsoNormalTable border=0 cellspacing=0 cellpadding=0 width="100%" style='width:100.0%;background:white'><tr><td style='padding:0cm 0cm 0cm 0cm'><div align=center><table class=MsoNormalTable border=1 cellspacing=0 cellpadding=0 width=640 style='width:480.0pt;border:solid #999999 1.0pt'><tr><td colspan=3 style='border:none;padding:0cm 0cm 0cm 0cm'><div align=center><table class=MsoNormalTable border=0 cellspacing=0 cellpadding=0 width="100%" style='width:100.0%'><tr><td valign=top style='padding:0cm 0cm 24.0pt 0cm'><p class=MsoNormal><span style='font-family:"Arial","sans-serif";mso-fareast-language:EN-SG'><img width=640 height=147 id="Picture_x0020_6" src="cid:image004.jpg@01D01559.A83395D0" alt="cid:image001.jpg@01D01530.C57FB980"></span><span style='font-size:12.0pt;font-family:"Arial","sans-serif"'><o:p></o:p></span></p></td></tr></table></div></td></tr><tr><td width=15 style='width:11.25pt;border:none;padding:0cm 0cm 0cm 0cm;-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%'><p class=MsoNormal><span style='font-family:"Arial","sans-serif"'>�</span><span style='font-size:12.0pt;font-family:"Arial","sans-serif"'><o:p></o:p></span></p></td><td width=610 style='width:457.5pt;border:none;padding:0cm 0cm 0cm 0cm;-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%'><table class=MsoNormalTable border=0 cellspacing=0 cellpadding=0 width="100%" style='width:100.0%;-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%'><tr><td style='padding:0cm 0cm 0cm 0cm;-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%'><div align=center><table class=MsoNormalTable border=0 cellspacing=0 cellpadding=0 width=610 style='width:457.5pt;-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%'><tr><td valign=top style='padding:0cm 0cm 6.0pt 0cm;-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%'><p class=MsoNormal align=center style='text-align:center'><span style='font-family:"Arial","sans-serif";mso-fareast-language:EN-SG'><img width=130 height=30 id="Picture_x0020_4" src="cid:image002.png@01D00E3D.30A814A0" alt="Julius Baer"></span><span style='font-size:12.0pt;font-family:"Arial","sans-serif"'><o:p></o:p></span></p></td></tr></table></div></td></tr><tr><td style='padding:0cm 0cm 0cm 0cm;-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%'><table class=MsoNormalTable border=0 cellspacing=0 cellpadding=0 style='-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%'><tr><td width=15 style='width:11.25pt;padding:0cm 0cm 0cm 0cm;-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%'><p class=MsoNormal><span style='font-family:"Arial","sans-serif"'>�</span><span style='font-size:12.0pt;font-family:"Arial","sans-serif"'><o:p></o:p></span></p></td><td style='padding:0cm 0cm 0cm 0cm;-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%'><table class=MsoNormalTable border=0 cellspacing=0 cellpadding=0 style='-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%'><tr><td style='padding:0cm 0cm 0cm 0cm;-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%'><table class=MsoNormalTable border=0 cellspacing=0 cellpadding=0 width="100%" style='width:100.0%;-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%'><tr><td width="50%" style='width:50.0%;padding:0cm 0cm 0cm 0cm;-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%'></td><td width="50%" style='width:50.0%;padding:0cm 0cm 0cm 0cm;-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%'></td></tr></table></td></tr><tr style='height:1.5pt'><td style='padding:0cm 0cm 18.75pt 0cm;height:1.5pt;-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%'><div style='border:none;border-bottom:solid #EDC8A3 1.5pt;padding:0cm 0cm 0cm 0cm'><p class=MsoNormal><span style='font-size:1.5pt;font-family:"Arial","sans-serif"'>�<o:p></o:p></span></p></div></td></tr><tr><td width=580 style='width:435.0pt;padding:0cm 0cm 0cm 0cm;-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%'><p class=MsoNormal align=center style='text-align:center'><b><span style='font-size:18.0pt;font-family:"Arial","sans-serif";color:#001489;text-transform:uppercase'>IPO Bond Kit<o:p></o:p></span></b></p></td></tr><tr><td width=580 style='width:435.0pt;padding:0cm 0cm 18.75pt 0cm;-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%'><p class=MsoNormal align=center style='text-align:center'><span style='font-size:18.0pt;font-family:"Arial","sans-serif";color:#001489'>Your Account has been activated<o:p></o:p></span></p></td></tr><tr><td style='padding:0cm 0cm 0cm 0cm;-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%'><table class=MsoNormalTable border=0 cellspacing=0 cellpadding=0 width="100%" style='width:100.0%;-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%'><tr><td width="39%" style='width:39.0%;padding:0cm 0cm 0cm 0cm;-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%'><p class=MsoNormal><span style='font-family:"Arial","sans-serif"'>�</span><span style='font-size:12.0pt;font-family:"Arial","sans-serif"'><o:p></o:p></span></p></td><td width="22%" style='width:22.0%;padding:0cm 0cm 30.0pt 0cm;-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%'><div style='border:none;border-bottom:solid #EDC8A3 1.5pt;padding:0cm 0cm 0cm 0cm'><p class=MsoNormal><span style='font-size:1.5pt;font-family:"Arial","sans-serif"'>�<o:p></o:p></span></p></div></td><td width="39%" style='width:39.0%;padding:0cm 0cm 0cm 0cm;-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%'><p class=MsoNormal><span style='font-family:"Arial","sans-serif"'>�</span><span style='font-size:12.0pt;font-family:"Arial","sans-serif"'><o:p></o:p></span></p></td></tr></table></td></tr><tr><td style='padding:0cm 0cm 11.25pt 0cm;-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%'><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'>Dear user<br><br>Your account to the IPO Bond Kit has been activated. Please click on the link below or type the following URL into your browser:<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'><o:p>�</o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Arial","sans-serif";color:#1F497D'><a href="https://singapore.xyz.com/"><span style='color:#1F497D'>https://ipokit.sin.xyz.com/</span></a></span><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'><o:p></o:p></span></p></td></tr><tr><td style='padding:0cm 0cm 6.75pt 0cm;-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%'><p class=MsoNormal><b><span style='font-size:10.0pt;font-family:"Arial","sans-serif";color:#001489;text-transform:uppercase'>Quick User Reference<o:p></o:p></span></b></p></td></tr><tr><td style='padding:0cm 0cm 11.25pt 0cm;-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%'><p class=MsoNormal><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'>A quick user reference guide is attached. Please feel free to contact us if anything is unclear.<o:p></o:p></span></p></td></tr><tr><td style='padding:0cm 0cm 11.25pt 0cm;-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%'><table class=MsoNormalTable border=0 cellpadding=0 style='-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%'><tr style='height:21.0pt'><td width=44 style='width:33.15pt;padding:0cm 0cm 0cm 0cm;height:21.0pt;-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%'><p class=MsoNormal><a href="https://singapore.xyz.com/"><span style='font-size:10.0pt;font-family:"Arial","sans-serif";color:#001489;border:none windowtext 1.0pt;padding:0cm;mso-fareast-language:EN-SG;text-decoration:none'><img border=0 width=28 height=28 id="Picture_x0020_3" src="cid:image003.png@01D00E3D.30A814A0" alt="Link Icon"></span></a><span style='font-size:12.0pt;font-family:"Arial","sans-serif"'><o:p></o:p></span></p></td><td width=5 style='width:3.7pt;padding:0cm 0cm 0cm 0cm;height:21.0pt;-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%'><p class=MsoNormal><span style='font-family:"Arial","sans-serif";color:#1F497D'>�</span><span style='font-size:12.0pt;font-family:"Arial","sans-serif";color:#1F497D'><o:p></o:p></span></p></td><td width=137 style='width:102.55pt;padding:0cm 0cm 0cm 0cm;height:21.0pt;-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%'><p style='-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%'><span style='font-size:10.0pt;font-family:"Arial","sans-serif";color:#1F497D;border:none windowtext 1.0pt;padding:0cm;mso-fareast-language:EN-US'><a href="https://singapore.xyz.com/" target="_blank"><span style='color:#1F497D'>IPO Bond Kit</span></a></span><span style='font-size:10.0pt;font-family:"Arial","sans-serif";color:#1F497D;mso-fareast-language:EN-US'><o:p></o:p></span></p></td></tr></table></td></tr><tr><td style='padding:0cm 0cm 11.25pt 0cm;-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%'><p style='-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%'><span style='font-size:10.0pt;font-family:"Arial","sans-serif";mso-fareast-language:EN-US'><br></span><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'><br>Kind regards <br><br><br>Markets Applications Singapore (<span style='color:#1F497D'><a href="mailto:anji@xyz.com"><span style='color:#1F497D'>marketsappsupport.sg@xyz.com</span></a></span>)<o:p></o:p></span></p><p><span style='font-size:10.0pt;font-family:"Arial","sans-serif"'>Fixed Income Trading Desk Singapore (<span style='color:#1F497D'><a href="mailto:anji@xyz.com"><span style='color:#1F497D'>securities.execution@xyz.com</span></a></span>)<o:p></o:p></span></p></td></tr></table></td><td width=15 style='width:11.25pt;padding:0cm 0cm 0cm 0cm;-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%'><p class=MsoNormal><span style='font-family:"Arial","sans-serif"'>�</span><span style='font-size:12.0pt;font-family:"Arial","sans-serif"'><o:p></o:p></span></p></td></tr></table></td></tr></table></td><td width=15 style='width:11.25pt;border:none;padding:0cm 0cm 0cm 0cm;-webkit-text-size-adjust: 100%;-ms-text-size-adjust: 100%'><p class=MsoNormal><span style='font-family:"Arial","sans-serif"'>�</span><span style='font-size:12.0pt;font-family:"Arial","sans-serif"'><o:p></o:p></span></p></td></tr></table></div></td></tr></table></div><p class=MsoNormal><span lang=EN-AU style='color:#1F497D'>Regards<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-AU style='color:#1F497D'>Anji<o:p></o:p></span></p><p class=MsoNormal><span lang=EN-AU style='color:#1F497D'><o:p>�</o:p></span></p></div></body></html>

Thanks in advance
Anji

Could you please upload mail.txt ? It help us to reproduce the issue at our end.

1 Like

Hi Pravin
Please see the attached email.txt file

Let me know if you require any other infoirmation

Thanks for the help
--Anji

Hi Anji,

You need to provide full path of src image file. For test purpose I have added below src image to your mail.tx and it works fine.
replace below code

 src="cid:image002.png@01D00E3D.30A814A0"

with

src=http://i.imgur.com/4pGDdQM.gif

Thanks,
Pravin

1 Like

Hi Parvin
Thanks for the reply

I tried giving the Linux path where this image is residing, but its not taking...

src="cid:image002.png@01D00E3D.30A814A0"

with

src="/home/u35731/1.jpg"
or
src="cid:/home/u35731/1.jpg"

The mail is still showing as blank

Should i require to give only windows path ?

Thanks
Anji

I am not sure about this.

Could you please try with below code ? just to verify image appear in the email or not (working below code fine at my end)

src=http://i.imgur.com/4pGDdQM.gif
1 Like

Yes Pravin, Image is appearing in the email with your code.. its just that, I will have to explicitly right click on the email and say "download Image"

I copied the image png files in Linux folder and tried to give the path of Linux server, but its not working as specified in previous response

Thanks mate
Anji

---------- Post updated at 10:36 PM ---------- Previous update was at 01:19 AM ----------

Hi Pravin and Experts
Can some one help me out here please

I copied the image jpg files in my Linux home folder and tried to give the path of Linux server, but its not working, Image not showing in the email...

I tried multiple options (See below), but no luck :mad:
I might be missing something but couldn't find what it is...

<img width=640 height=147 src="/home/u35731/dev/1.jpg">
and
<img width=640 height=147 src="1.jpg">

Thanks in advance
Anji

You can include the image data in the mail using HTML's embedded img url eg.

<img width=640 height=147 src="data:image/jpeg;base64,$(base64 /home/u35731/dev/1.jpg)">
1 Like

Hi Skrynesaver
I tried the below after you suggested but still i dont see the image in mail :mad:

<img width=640 height=147 src="data:image/jpeg;base64,$(base64 /home/u35731/dev/1.jpg)">

In email, I'm getting just a cross image saying "the linked image cannot be displayed"

Thanks and advanced happy new year
Anji

It's probably the "+" characters in the base64 encode they may need to be URI encoded try:

<img width=640 height=147 src="data:image/jpeg;base64,$(base64 /home/u35731/dev/1.jpg | sed 's/+/%2b/g')">

Hi Chubler
Unfortunately still no luck :frowning:

Please suggest if i am missing something here

Code which is not working

<img width=640 height=147 src="data:image/jpeg;base64,$(base64 /home/u35731/dev/1.jpg | sed 's/+/%2b/g')">

Code which is working

<img width=130 height=30 id="Picture_x0020_4" src=http://i.imgur.com/4pGDdQM.gif >

Thank you

Do you have base64 installed and in your path?

Try running the encode from the command line and see if you get output eg:

$ base64 /home/u35731/dev/1.jpg
8I+xMods90y4ayTcU8uKPKpTJTaOY6A9eofuLRNTi7wfIR8Rp5CPVCQcXWEFOdKd6p0HEvYdi/YK
v3dfllPLGVezEMm1PFW8C7qEPEHaeNM6PwHZ4+QnU27m4PESHUT0CAj+kc6u3q9W42WRpgcx7yFm
...
fP35+vP15+vP/9yf/z90FwlRAFggAA==
1 Like

Wish you a happy new year to all the experts

Hi Chubler
Yes base64 is installed in my path, see the result from the commandline

$ base64 /home/u35731/dev/1.jpg
/9j/4RdARXhpZgAATU0AKgAAAAgABwESAAMAAAABAAEAAAEaAAUAAAABAAAAYgEbAAUAAAABAAAA
agEoAAMAAAABAAIAAAExAAIAAAAeAAAAcgEyAAIAAAAUAAAAkIdpAAQAAAABAAAApAAAANAAD0JA
AAAnEAAPQkAAACcQQWRvYmUgUGhvdG9zaG9wIENTNiAoV2luZG93cykAMjAxNDowNzoyOCAxMDox
MjowNgAAA6ABAAMAAAAB//8AAKACAAQAAAABAAADBKADAAQAAAABAAAAsQAAAAAAAAAGAQMAAwAA
----

anF1ZXJ5LmZpbGVEb3dubG9hZE9yZGVyLmpzPzE0MDQ5MDYwMDQiIHR5cGU9InRleHQvamF2YXNj
cmlwdCI+PC9zY3JpcHQ+CjxzY3JpcHQgc3JjPSJ0eXBvM2NvbmYvZXh0L2p1bGl1c2JhZXJfaW1h
Z2VkYi9SZXNvdXJjZXMvUHVibGljL0phdmFTY3JpcHQvc29sci5qcz8xNDA0OTA2MDA0IiB0eXBl
PSJ0ZXh0L2phdmFzY3JpcHQiPjwvc2NyaXB0PgoKCgo8L2JvZHk+CjwvaHRtbD4=

Good Morning Experts
I triped multiple ways but still unable to find the solution...
Can some one help me out here please

Thanks In Advance
Anji

Hi Anji,

What appears in the source of the mail when you run the script with the $(base64 $file) call?

1 Like

Hi Skrynesaver
I get the below when i run the

$(base64 $file) 
NTM1IGYNCjAwMDAwMDAwMDAgNjU1MzUgZg0KMDAwMDAwMDAwMCA2NTUzNSBmDQowMDAwMDAwMDAw
IDY1NTM1IGYNCjAwMDAwMDAwMDAgNjU1MzUgZg0KMDAwMDAwMDAwMCA2NTUzNSBmDQowMDAwMDAw
MDAwIDY1NTM1IGYNCjAwMDAwMDAwMDAgNjU1MzUgZg0KMDAwMDAwMDAwMCA2NTUzNSBmDQowMDAw
MDAwMDAwIDY1NTM1IGYNCjAwMDAwMDAwMDAgNjU1MzUgZg0KdHJhaWxlcg0KPDwvU2l6ZSAyMDMv
SURbPERDOThEOTcyNDA2ODE3NEE5QzlCREQ1RUFBNEE1Nzc4PjwzNzAyRjU5QzI0MEJCNjQ2OURD
MEFENDdDMUQ2RTNBND5dPj4NCnN0YXJ0eHJlZg0KMTE2DQolJUVPRg0K

Thanks
Anji

Strange, when I try to view the resultant tag in a browser the image seems to be invalid...

<img src="data:image/jpeg;base64,NTM1IGYNCjAwMDAwMDAwMDAgNjU1MzUgZg0KMDAwMDAwMDAwMCA2NTUzNSBmDQowMDAwMDAwMDAwIDY1NTM1IGYNCjAwMDAwMDAwMDAgNjU1MzUgZg0KMDAwMDAwMDAwMCA2NTUzNSBmDQowMDAwMDAwMDAwIDY1NTM1IGYNCjAwMDAwMDAwMDAgNjU1MzUgZg0KMDAwMDAwMDAwMCA2NTUzNSBmDQowMDAwMDAwMDAwIDY1NTM1IGYNCjAwMDAwMDAwMDAgNjU1MzUgZg0KdHJhaWxlcg0KPDwvU2l6ZSAyMDMvSURbPERDOThEOTcyNDA2ODE3NEE5QzlCREQ1RUFBNEE1Nzc4PjwzNzAyRjU5QzI0MEJCNjQ2OURDMEFENDdDMUQ2RTNBND5dPj4NCnN0YXJ0eHJlZg0KMTE2DQolJUVPRg0K"

---------- Post updated at 11:42 ---------- Previous update was at 10:10 ----------

Just to be sure I tested on my own host...

 echo \<img src=\"data:image/jpeg\;base64,$(base64  avatar.jpg )\"\> > test.html

produced a test.html file that worked in my browser...

1 Like

Hello Skrynesaver
My bad, the resultant tag was too big so I just copied part of it, I attached it in txt format now..

Also, I tried the exactly how you tested at your end and the image was showing in browser to me aswell

Picture showing in browser with the below command
DEV_u35731:~/dev> echo \<img src=\"data:image/jpeg\;base64,$(base64  /home/u35731/dev/1.jpg)\"\> > test.html

But, when i try to use that code in my html file and call it in my shell script its not working :frowning: , Image not coming up in my email

Not working the code from mail.html
...
<img width=640 height=147 src="data:image/jpeg;base64,$(base64  /home/u35731/dev/1.jpg)">
...

Thank you
Anji

Hi Anji,

You would have to script creation of the /home/u35731/dev/mail.txt file rather than just using cat to apend it to the mail without interpolation.

#!/bin/bash
outputFile="/home/u35731/dev/mail.txt"
/home/u35731/bin/gen_mail_txt.sh $outputFile # where gen_mail_txt.sh generates the file including the required image tags.
(
echo "From: donotreply@xyz.com"
echo "To: anji009@xyz.com"
echo "MIME-Version: 1.0"
echo "Subject: Test"
echo "Content-Type: text/html; charset=ISO-8859-15"
echo "Content-Transfer-Encoding: 7bit"
cat $outputFile
) | sendmail -t
1 Like

Thanks Skrynesaver
I tried but didn't work too :frowning:
I have a query here.. As you said

,
If thats the case then how come the image is displaying in email when i just cat the output mail.txt file in script which has the image tags where as src as url ?

Working code

<img width=130 height=30 id="Picture_x0020_4" src=http://i.imgur.com/4pGDdQM.gif >

Is this suggestion is for only those images who have the img src as local linux drives ?

Thanks
Anji