What is the easiest - and preferably most portable - command I can use to email a single file as an attachment a *nix shell?
Assuming it's a binary attachment:
uuencode [filename] [filename] | mail -s [subject] [recipient address]
You don't need to bother with the UUencoding if it's just a text file, eg:
mail -s [subject] [recipient address] < [filename]
Most *NIXes have mail and uuencode, so this should work pretty much anywhere.
Using mutt, you can:
mutt -z -a <file> -s <subject> -- email@example.com
Or, if you don't want to type a body:
mutt -z -a <file> -s <subject> -- firstname.lastname@example.org < /dev/null
"sendEmail is a lightweight, command line SMTP email client. If you have the need to send email from a command line, this free program is perfect: simple to use and feature rich. It was designed to be used in bash scripts, batch files, Perl programs and web sites, but is quite adaptable and will likely meet your requirements. SendEmail is written in Perl and is unique in that it requires NO MODULES. It has an intuitive and flexible set of command-line options, making it very easy to learn and use. [Supported Platforms: Linux, BSD, OS X, Windows 98, Windows NT, Windows 2000, & Windows XP]"
I've used it before and really liked it. You can attach files with the -a option.
I can't add a comment, but..
- The answers to this are going to depend very much upon which, if any, derivative of
mailxyou have available.
- Although the file should be displayed without any problems by the receiving MUA, piping from
uuencodewon't technically produce an email with an attachment. Take a look at the source of the email you receive to see why.
If you want absolute portability you can telnet into your mail server on port 25 and issue SMTP commands directly. They're not too hard, and it should be very scriptable.
A quick Google turned up this page, which describes a variety of ways to attach files using a variety of applications. A couple of the more prevalent ones --
uuenc8de to make an inline "attachment".
-f file-to-attach -m mime-type
-c mime-type file-to-attach
echo "Email body text" | mail -s "Subject of email" -a file.to.attach.txt email@example.com
Or if you have python available, it'll take only few lines, such as listed here: Sending attachments in python