send mail from command line with attached file

I had to send a mail using only command line. I was surprised it isn’t straightforward at all. I didn’t had pine nor mutt or anything like that. Just mail and mailx.

What Internet say (via google) is

I tried it. And it works almost each times. But for my file, it didn’t worked. I compressed it to .gz, .bz2 and .zip. Using .bz2 format it worked nicely, but not with other formats. Instead of having an attached file I saw this in my email.

begin 664 fic.jpg
M(R$O=7-R+V)I;B]E;[email protected]>G-H"GAL

Not really readable.
After some research I found the solution.
Use MIME instead of `uuencode`.

Finally I made it manually using `sendmail`.
I didn" t dare to use `telnet`. The command to use is: ~~~~~~ {.zsh} sendmail -t -oi < mailcontent.txt ~~~~~~ Of course you need to create the `mailcontent.txt` file. It should contains: 
From: [email protected]
To: [email protected]
Subject: View the attached file
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary="-"

This is a MIME encoded message. Decode it with "Decoder"
or any other MIME reading software. Decoder is available
at .
Content-Type: image/jpeg; name="fic.jpg"
Content-Transfer-Encoding: base64
Content-Disposition: inline; filename="fic.jpg"


And to obtain the “encoded” file in base64 I used:

uuencode -m fic.jpg fic.jpg ~~~~~~

That is all. Sometimes technology is so easy to use. If I need it another time I should consider to make a shell script to automatize this.

Published on 2010-08-31
Done with Vim spacemacs & nanoc Hakyll

ADA: DdzFFzCqrhtAvdkmATx5Fm8NPJViDy85ZBw13p4XcNzVzvQg8e3vWLXq23JQWFxPEXK6Kvhaxxe7oJt4VMYHxpA2vtCFiP8fziohN6Yp