Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mail: piping command output to mail with subject

Status
Not open for further replies.

cmayo

MIS
Apr 23, 2001
159
0
0
US
Hi all,

Hoping someone might know the answer to this... I'm looking for a simple way to send the output of a shell script via email (on Solaris), and I'd like to include From: and Subject: headers in those mails. Google hasn't been much help -- seems most of the mail command resources involve reading mail.

In a simple form, what I'm doing is this:

ls -l | mail myname@myhost.com

Mail arrives from root@myhost with no subject line. I'd like to specify a subject, as well as a From: address

Thanks in advance,
Chuck
 
what about:

ls -l | mail -s "Subject" myname@myhost.com



Regards,
Chuck
 
Good thought, but didn't work. The mail command seems to look for headers in the body of the mail. For example, this works just fine:

bash-2.05# mail myname@myhost.com
Subject: My Subject
From: myrealname@myotherhost.com

Hello there
^D

The mail man pages don't seem to cite any switches for specifying haders.

When piping command output to mail, there's no opportunity to prepend text to the output. Or is there? I suppose if I could figure out a way to include my command output in a cat statement, I could do something like:

cat 'Subject:My Subject\nFrom:myrealname@myotherhost.com\n' MYCOMMANDOUTPUT | mail myname@myhost.com

I'm probably making this way too hard...
 
what about using mailx instead of mail like this:

ls -l | mailx -s "Subject" myname@myhost.com

Regards,
Chuck
 
And what about something like this ?
(echo "Subject:My Subject\nFrom:myrealname@myotherhost.com\n"; mycommand) | mail myname@myhost.com

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Chuck, thanks much. mailx will definitely work. I don't see a way to specify a FROM address but can specify a subject with -s, and that'll get me the basic functionality I need.

PH, turns out echo command is executed as a separate command from the command piped to mail and just outputs to the shell before the command is run.

Thanks again,
Chuck
 

No problem.

As far as the FROM address issue, are you talking wanting it to show something like "Super User Account" instead of root@myhost.com? If so, then you will need to fill in the comment field in /etc/passwd on the server which is what it will use for that.



Regards,
Chuck
 
echo command is executed as a separate command
It shouldn't as it's inside parens ...
 
Nah, just wanted the reply-to address to point to someone else's mailbox. Turns out mailx accepts a reply-to switch:

mailx -r "anotheremail@server.com"

I'm golden!

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top