Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

What is the easiest - and preferably most portable - command I can use to email a single file as an attachment a *nix shell?

share|improve this question

9 Answers 9

up vote 10 down vote accepted

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.

share|improve this answer
    
How about if I want the text file as an attachment, and not in the message body? –  Yuval A Jun 2 '09 at 9:39
    
It depends - I don't think plain GNU mail can do that, so you'll need to use an actual mail client; pine, mutt, or something similar. Which of these are available to you depends on which ones your systems have installed. Using mutt, Gavin's answer (below) will work just fine. –  RainyRat Jun 2 '09 at 9:44
    
Pine is good! Can I use it via command line with using the textual GUI? –  Yuval A Jun 2 '09 at 9:47
    
I think that's do-able. Have a look at staff.washington.edu/chappa/pine/info/outgoing.html for more. –  RainyRat Jun 2 '09 at 10:01

Using mutt, you can:

mutt -z -a <file> -s <subject> user@example.com

Or, if you don't want to type a body:

mutt -z -a <file> -s <subject> user@example.com < /dev/null

share|improve this answer
    
mutt is not available on my systems, thanks anyways –  Yuval A Jun 2 '09 at 9:40
    
I think you are going to find that this is going to be a toss-up between what is easy and what is portable. mailx is portable, but not necessarily easy. As RainyRat mentions, I think you are going to have to go with a full MUA, and run it from the command-line. –  Gavin McTaggart Jun 2 '09 at 9:52

"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.

share|improve this answer
    
+1 - This was perfect for what I need as it's easy and still actively being maintained! If you're on a Debian system, you can just aptitude install sendemail. Note that the command gets installed as sendEmail with a capital 'E'. –  Topher Fangio Dec 21 '09 at 17:47
    
Actually, the tarball contains a perl script and a couple of documents. You can just download it, extract it, and run it (provided your system has perl). Glad you like it, though. –  Clinton Blackmore Dec 21 '09 at 17:57

I can't add a comment, but..

  • The answers to this are going to depend very much upon which, if any, derivative of mailx you have available.
  • Although the file should be displayed without any problems by the receiving MUA, piping from uuencode won't technically produce an email with an attachment. Take a look at the source of the email you receive to see why.
share|improve this answer
    
+1 for truth. I didn't even realise there was a version of mailx that could send attachments (mine certainly doesn't). You are also correct about the manual uudecoding required with the uuencode -> mail pipeline. –  Gavin McTaggart Jun 2 '09 at 9:39

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.

share|improve this answer
3  
You could also write your own SMTP server. –  hark Jun 2 '09 at 13:02

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".

  • metamail -f file-to-attach -m mime-type

  • mpack -c mime-type file-to-attach

  • mutt -a file-to-attach

  • Elm -A file-to-attach

Additionally, it has links to a couple of shell scripts and a Perl script to craft and send the message, which will probably be more to your liking.

share|improve this answer

Some years ago, I wrote a shell script which did just that. It was called binmail.sh. It used a base64 encoder (many source codes can be found with google) and built attachments according to RFC1521.

share|improve this answer

Or if you have python available, it'll take only few lines, such as listed here: Sending attachments in python

share|improve this answer
echo "Email body text" | mail -s "Subject of email" -a file.to.attach.txt my@email.com
share|improve this answer
    
mail: illegal option -- a –  Yuval A Jun 2 '09 at 9:40
1  
note that this is a mailx feature, make sure that particular package is installed –  niXar Jun 2 '09 at 9:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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