10

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

Yuval A
  • 257
  • 1
  • 3
  • 11

9 Answers9

11

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.

RainyRat
  • 3,700
  • 1
  • 23
  • 29
  • How about if I want the text file as an attachment, and not in the message body? – Yuval A Jun 02 '09 at 09: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 02 '09 at 09:44
  • Pine is good! Can I use it via command line with using the textual GUI? – Yuval A Jun 02 '09 at 09:47
  • I think that's do-able. Have a look at http://staff.washington.edu/chappa/pine/info/outgoing.html for more. – RainyRat Jun 02 '09 at 10:01
11

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

chicks
  • 3,639
  • 10
  • 26
  • 36
Gavin McTaggart
  • 1,826
  • 16
  • 14
  • mutt is not available on my systems, thanks anyways – Yuval A Jun 02 '09 at 09: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 02 '09 at 09:52
  • 2
    I had to add a `--` before the email address to make these commands work. – Translunar Mar 19 '15 at 15:36
  • Dashes are in fact needed to separate the email address from filenames " -- user@example.com", @DoctorMohawk is correct. Attempting to add them above to the body of the answer. – Jeff Clayton Nov 01 '17 at 15:29
2

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

Clinton Blackmore
  • 3,510
  • 6
  • 35
  • 61
  • +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
1
echo "Email body text" | mail -s "Subject of email" -a file.to.attach.txt my@email.com
jscott
  • 24,204
  • 8
  • 77
  • 99
Neobyte
  • 3,177
  • 25
  • 29
1

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.
Dan Carley
  • 25,189
  • 5
  • 52
  • 70
  • +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 02 '09 at 09:39
1

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.

Maximus Minimus
  • 8,937
  • 1
  • 22
  • 36
1

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.

hark
  • 560
  • 2
  • 6
0

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.

mouviciel
  • 476
  • 4
  • 6
0

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

slovon
  • 957
  • 5
  • 12