Friday, January 28, 2011

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

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.

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

    Yuval A : mutt is not available on my systems, thanks anyways
    Gavin McTaggart : 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.
  • echo "Email body text" | mail -s "Subject of email" -a file.to.attach.txt my@email.com

    Yuval A : mail: illegal option -- a
    niXar : note that this is a mailx feature, make sure that particular package is installed
    From Neobyte
  • 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.
    Gavin McTaggart : +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.
    From Dan Carley
  • 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.

    From mouviciel
  • 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.

    hark : You could also write your own SMTP server.
    From mh
  • 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.

    From hark
  • "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.

    Topher Fangio : +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 : 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.
  • Or if you have python available, it'll take only few lines, such as listed here: Sending attachments in python

    From slovon

0 comments:

Post a Comment