Embed image to the html script

hi,

trying to embed an image to the html file to send out as an email.

img src="data:image/jpeg;base64,$(base64 /home/test/abc.jpg 

but getting error as file not found after it aplies base64 on the file.

It is hard to tell what is the issue without seeing your code.

Anyway here is an example to embed image in your mail body:-

{
        echo "From: from@domain.com"
        echo "To: to@domain.com"
        echo "Subject: Testing Image"
        echo "MIME-Version: 1.0"
        echo "Content-Type: multipart/mixed; boundary=FILEBOUNDARY"
        echo "--FILEBOUNDARY"
        echo "Content-Type: multipart/alternative; boundary=MMSGBOUNDARY"
        echo ""
        echo "--MMSGBOUNDARY"
        echo "Content-Type: text/html charset=iso-8859-1"
        echo "Content-Disposition: inline"
        echo ""
        echo "<html>"
        echo "<body>"
        echo "<img src=\"cid:myimg\" />"
        echo "</body>"
        echo "</html>"
        echo "--MMSGBOUNDARY--"
        echo "--FILEBOUNDARY"
        echo "Content-Type: image/jpeg"
        echo "Content-Transfer-Encoding: base64"
        echo "Content-Disposition: inline; filename=image.jpg"
        echo "Content-Id: <myimg>"
        echo ""
        base64 /absolute_path_to_your_image/image.jpg
        echo "--FILEBOUNDARY--"
} | /usr/sbin/sendmail -t

What email client are you using? Microsoft Outlook blocks "data" style embeds as they don't trust their own parser.

function email_tt()
{
echo "To:"abc@dom.com>$email_file

echo "Subject: Test " >>$email_file

echo "Content-type: text/html">>$email_file

echo "<head>
    <meta http-equiv=""Content-Type"" content=""text/html; charset=utf-8"">
        <style type=""text/css"" media=""screen"">

        table{
              border-collapse: collapse;
              font-family:Calibri;
             }
        table,td,th{
                   border: 1px solid black;
                   padding: 3px;
                  }
        td{
          font-size:10pt;
          padding: 5px;
          text-align: center;
          }
        th {
            background-color: #3397ff;
            padding: 5px;
            font-weight: bold;
            font-size:10pt;
                       }
           </style>
</head>
<body style=""background:#FFFFFF"">">>$email_file
echo "<img src="data:image/jpeg;base64,$(base64 /home/test/abc.jpg) "/>" >>$email_file

echo "<p style=""font-family:Calibri"" > Team,</p>" >>$email_file
echo "<p style=""font-family:Calibri""> test</p>" >>$email_file
echo "<br>" >>$email_file
}

email_tt
cat $email_file | sendmail -t

when i run this, i get the error as file not found.
line 35: base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcG: No such file or directory

running using unix.

echo "<img src='data:image/jpeg;base64,$(base64 -w 0 /home/test/abc.jpg)' />" >>$email_file
1 Like

At a guess, maybe it can't find /home/test/abc.jpg, or doesn't have permissions to access it. Or maybe it can't find base64.

after changing the double quote(") to single it did work out.
But the image is not getting displayed and only X is coming. i am using outlook to view the email.

echo "<img src='data:image/jpeg;base64,$(base64 -w 0 /home/test/abc.jpg)' />" >>$email_file