0

I am trying to send a mail with below command. I am sending this command from sqr to the command line, which is working fine. $FilePath_mail have To,From and other information along with mail body which is in HTML format. I want to have an image (logo) in the body, so I wanted to send it as an attachment.

/usr/sbin/sendmail -t < $FilePath_mail

I need to change the above command to add the attachment(basically an image) to the mail?

  • Pure sendmail doesn't support attachments, use mutt instead. – Ipor Sircer Dec 13 '18 at 12:58
  • 1
    sendmail does not care what you send. But if you want attachments you need to handle the MIME encoding yourself. If we can choose not to use sendmail then mail or mailx would be more lightweight than mutt. – Claus Andersen Dec 13 '18 at 13:32
0

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.

The makemime makes it easier to MIME encode your mail messages from the commandline. The example from their page is:

For example:

# 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,">

or

 <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

0

In case you opt just for sending the image, you can always send an image via email with mpack

The syntax as you need it, is roughly:

mpack -s "This is my image" $FilePath_mail bill@domain.com

From man mpack

NAME

   mpack - pack a file in MIME format

DESCRIPTION The mpack program encodes the the named file in one or more MIME messages. The resulting messages are mailed to one or more recipients, written to a named file or set of files, or posted to a set of newsgroups.

In Debian, you install it as in:

sudo apt-get install mpack

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.