This is a quick tute for folk who want to send email from the commandline under the various Unices, especially those who also need to include attachments. This has been tested on Debian GNU/Linux with Icedove/Thunderbird.
We start by simply sending an email from the commandline. I shall use the mail command, but your system may use mailx or sendmail on your UNIX. I use the -s option to specify the subect.
echo "body text" | mail firstname.lastname@example.org
Now I want to send an attachment. I’ll need to break out uuencode to do that. Now, uuencode (for UNIX to UNIX encoding, geddit?) rewrites STDIN in an encoded form to STDOUT, or you can specify a couple of filenames and have it read from one of those files and encode as if the second were the title. By default it uses a 7-bit encoding. Most email clients are cool wi' dat. Anyhow, you'll want to install sharutils if you haven't got it already.
$ sudo aptitude install sharutils
I have a file called eg.txt. Lets have a look at what uuencode spits out.
uuencode eg.txt eg.txt
$ uuencode eg.txt eg.txt
begin 644 eg.txt
That should look familiar-ish if you’ve seen the plain text of an email with 7 bit attachments. You can also spit out base64 encoded mail using the -m option.
Now we can use the uuencoded text as our email body to create an attachment.
uuencode eg.txt eg.txt | mail -s "another test" email@example.com
So, the final step is if you want to include some body text. You could write your email in vim and cat it. Here I simply use echo and say "hello world" as my body text. Note that we run the commands together using the semicolon and wrap the lot in parentheses so that mail will run them together like a proper email.
$ (echo hello world; uuencode eg.txt eg.txt) | mail -s "another test" firstname.lastname@example.org
Well that's it for today UNIX geeks. Happy attachment sending!