Koozali.org: home of the SME Server

Command line mail

Offline basmistry

  • *
  • 19
  • +0/-0
Command line mail
« on: August 23, 2008, 12:54:58 AM »
How do I attach file using command line mail

I've used Suse Linux in before and it has a "-a" option to attach a file

eg.
echo Test Completed at `date` | mail -s "Test email" -a /home/test.log  me@mydomain.com

(date is set at top of the script)


Offline cactus

  • *
  • 4,880
  • +3/-0
    • http://www.snetram.nl
Re: Command line mail
« Reply #1 on: August 23, 2008, 10:13:17 AM »
How do I attach file using command line mail

I've used Suse Linux in before and it has a "-a" option to attach a file

eg.
echo Test Completed at `date` | mail -s "Test email" -a /home/test.log  me@mydomain.com

(date is set at top of the script)


mail is not installed, instead mutt is, there is a bash one-liner on how to send mail with that from the command line here: http://www.cyberciti.biz/tips/sending-mail-with-attachment.html
Be careful whose advice you buy, but be patient with those who supply it. Advice is a form of nostalgia, dispensing it is a way of fishing the past from the disposal, wiping it off, painting over the ugly parts and recycling it for more than its worth ~ Baz Luhrmann - Everybody's Free (To Wear Sunscreen)

Offline basmistry

  • *
  • 19
  • +0/-0
Re: Command line mail
« Reply #2 on: August 23, 2008, 12:07:01 PM »
Mail is installed

the does not support attachments ie. have a -a option

I can send an emal

echo Test Email from SME | mail me@mydomain.com -s "This is a test from SME"

Offline basmistry

  • *
  • 19
  • +0/-0
Re: Command line mail *** SORTED **
« Reply #3 on: August 23, 2008, 01:29:48 PM »

I've sorted it myself...

My file is a Text file so ...

echo Test Completed at `date` | mail -s "Test email" -a /home/test.log  me@mydomain.com < /home/mytextfile.txt


Offline CharlieBrady

  • *
  • 6,918
  • +3/-0
Re: Command line mail *** SORTED **
« Reply #4 on: August 23, 2008, 04:55:19 PM »
I've sorted it myself...

My file is a Text file so ...

echo Test Completed at `date` | mail -s "Test email" -a /home/test.log  me@mydomain.com < /home/mytextfile.txt



Mail only has one "standard input". You can't do pipe and also redirect from a file. If you want both text in your mail, you need to do:

Code: [Select]
( echo Test Completed at $(date); cat /home/mytextfile.txt ) | mail -s "Test email" me@mydomain.com

$(...) has various advantages over backticks and is recommended syntax these days.