You need to add the image as a MIME part to the mail and then reference it using the
Content-ID (CID) already answered here
The people behind Courier MTA have made the utility makemime. It will be installed as part of Courier MTA but on some systems it is an independent package or as part of the maildrop utility.
makemime makes it easier to MIME encode your mail messages from the commandline. The example from their page is:
# Take two files containing the text and the html version of a message, and
# add MIME headers to them.
makemime -c "text/plain; charset=iso-8859-1" -o tmp1.txt msg.txt
makemime -c "text/html; charset=iso-8859-1" -o tmp1.html msg.html
# Combine the result into a multipart/alternative collection
makemime -m "multipart/alternative" -a "Content-Disposition: inline" \
-o tmp.ma1 tmp1.txt
makemime -j tmp.ma1 -o tmp.ma2 tmp1.html
# Add MIME headers to an image attachment.
makemime -c "image/gif" -a "Content-Disposition: attachment" \
-o tmp2.gif attachment.gif
# Create the final multipart/mixed collection
makemime -m "multipart/mixed" -a "Mime-Version: 1.0" \
-o tmp.mm1 tmp.ma2
makemime -j tmp.mm1 -o output.msg tmp2.gif
output.msg now contains the complete MIME collection. Just add the Subject:, From:, and To: headers (can also be done by additional -a options, of course), and send it on its way.
This is what you are asking - and it can be done. Using a tool like
makemime makes the process fairly safe.
You should however read the Sendgrid article on "How to Embed Images in Your Emails: The Facts". Because of poor handling in many clients I usually just end up referencing the image on a web server. It is the easiest method and even though it often requires the user to "click to view" it seems to give the most consistent results across clients.
Another option is to inline embed the logo in your HTML source.
<img alt="Logo" src="data:image/jpeg;base64,">
<img alt="Logo" src="data:image/png;base64,"
Add the base64 encoded data of the image file right after
base64, and you are good to go. But again many clients will not like it.
You can encode the file online with base64encode.org. I would highly recommend to compress the image first using something like TinyPNG