10

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

9 Answers 9

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.

4
  • How about if I want the text file as an attachment, and not in the message body?
    – Yuval A
    Jun 2, 2009 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, 2009 at 9:44
  • Pine is good! Can I use it via command line with using the textual GUI?
    – Yuval A
    Jun 2, 2009 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, 2009 at 10:01
11

Using mutt, you can:

mutt -z -a <file> -s <subject> -- [email protected]

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

mutt -z -a <file> -s <subject> -- [email protected] < /dev/null

4
  • mutt is not available on my systems, thanks anyways
    – Yuval A
    Jun 2, 2009 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. Jun 2, 2009 at 9:52
  • 2
    I had to add a -- before the email address to make these commands work.
    – Translunar
    Mar 19, 2015 at 15:36
  • Dashes are in fact needed to separate the email address from filenames " -- [email protected]", @DoctorMohawk is correct. Attempting to add them above to the body of the answer. Nov 1, 2017 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.

2
  • +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'. Dec 21, 2009 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. Dec 21, 2009 at 17:57
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.
1
  • +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. Jun 2, 2009 at 9: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.

1
  • 3
    You could also write your own SMTP server.
    – hark
    Jun 2, 2009 at 13:02
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.

1
echo "Email body text" | mail -s "Subject of email" -a file.to.attach.txt [email protected]
2
  • mail: illegal option -- a
    – Yuval A
    Jun 2, 2009 at 9:40
  • 1
    note that this is a mailx feature, make sure that particular package is installed
    – niXar
    Jun 2, 2009 at 9:44
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.

0

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .