Easiest way to email file via command line in *nix?

17,735

Solution 1

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.

Solution 2

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

Solution 3

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

Solution 4

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.

Solution 5

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:
17,735
Yuval A
Author by

Yuval A

λf.(λx.f(x x)) (λx.f(x x))

Updated on September 17, 2022

Comments

  • Yuval A
    Yuval A over 1 year

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

  • Gavin McTaggart
    Gavin McTaggart almost 15 years
    +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.
  • Yuval A
    Yuval A almost 15 years
    How about if I want the text file as an attachment, and not in the message body?
  • Yuval A
    Yuval A almost 15 years
    mutt is not available on my systems, thanks anyways
  • Yuval A
    Yuval A almost 15 years
    mail: illegal option -- a
  • niXar
    niXar almost 15 years
    note that this is a mailx feature, make sure that particular package is installed
  • RainyRat
    RainyRat almost 15 years
    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.
  • Yuval A
    Yuval A almost 15 years
    Pine is good! Can I use it via command line with using the textual GUI?
  • Gavin McTaggart
    Gavin McTaggart almost 15 years
    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.
  • RainyRat
    RainyRat almost 15 years
    I think that's do-able. Have a look at staff.washington.edu/chappa/pine/info/outgoing.html for more.
  • hark
    hark almost 15 years
    You could also write your own SMTP server.
  • Topher Fangio
    Topher Fangio over 14 years
    +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'.
  • Clinton Blackmore
    Clinton Blackmore over 14 years
    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.
  • Translunar
    Translunar about 9 years
    I had to add a -- before the email address to make these commands work.
  • Jeff Clayton
    Jeff Clayton over 6 years
    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.