Page 1 of 1

command output in mail subject.

Posted: Sat Dec 01, 2012 3:26 am
by sim_tcr
Hello,

How do I send an email with its subject as output of a command?

For eg. I want to send wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//' output as Subject of the email and send to [email protected].

Thanks,
Simon Mandy

Re: command output in mail subject.

Posted: Sat Dec 01, 2012 4:42 am
by Dweeber
sim_tcr wrote:Hello,

How do I send an email with its subject as output of a command?

For eg. I want to send wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//' output as Subject of the email and send to [email protected].

Thanks,
Simon Mandy
You don't say if you have a mail server setup....

Code: Select all

Result=`wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'`
echo $Result | mailx -s $Result [email protected]
There are only 2 lines, the first one is long....

First line places the output into a variable called $Result
Second line cats echos the $Result (so it is in the body of the message) and includes it in the Subject line as well sending the message to [email protected]

The second line assume you have a typical mail server that has mail or mailx program.... there are some scripts that can mimic that without actually loading a mail server that simply client to a mail server like Gmail etc... but it would be similar.

Re: command output in mail subject.

Posted: Sat Dec 01, 2012 5:18 am
by sim_tcr
Yes. I have the mail server setup done. And I put your two lines in to a shell script and executed it and it worked well.
Now, same way I setup another script,

Resultping=`ping -c 1 <hostname> | grep "64 bytes from <hostname>"`
echo $Resultping | mailx -s $Resultping [email protected]


The mail command fails with below message,


[email protected] ~ $ echo $Resultping | mailx -s $Resultping [email protected]
: contains invalid character ':'
[email protected] ~ $ send-mail: recipient address bytes not accepted by the server
send-mail: server message: 553-5.1.2 We weren't able to find the recipient domain. Please check for any
send-mail: server message: 553-5.1.2 spelling errors, and make sure you didn't enter any spaces, periods,
send-mail: server message: 553 5.1.2 or other punctuation after the recipient's email address. iu8sm4169517pbc.71
send-mail: could not send mail (account default from /home/pi/.msmtprc)

Re: command output in mail subject.

Posted: Sat Dec 01, 2012 9:49 am
by jojopi
You should always put double-quotes around back-quotes: "`command`", and around variable expansions: "$variable". Otherwise, bad things happen when any spaces are embedded in the substitution results.

Re: command output in mail subject.

Posted: Sat Dec 01, 2012 11:59 am
by sim_tcr
jojopi wrote:You should always put double-quotes around back-quotes: "`command`", and around variable expansions: "$variable". Otherwise, bad things happen when any spaces are embedded in the substitution results.
This fixed it

Result=$(ping -c 1 google.com | grep "PING google.com")
echo $Result | mailx -s "$Result" [email protected]

Re: command output in mail subject.

Posted: Sat Dec 01, 2012 12:20 pm
by Dweeber
sim_tcr wrote:Yes. I have the mail server setup done. And I put your two lines in to a shell script and executed it and it worked well.
Now, same way I setup another script,

Resultping=`ping -c 1 <hostname> | grep "64 bytes from <hostname>"`
echo $Resultping | mailx -s $Resultping [email protected]


The mail command fails with below message,


[email protected] ~ $ echo $Resultping | mailx -s $Resultping [email protected]
: contains invalid character ':'
...
You changed the command you said you were going to use.

The difference is that the original command only returned an IP address (no spaces in it).. the new command has a lot more info in it including spaces something like:

64 bytes from hostname (xxx.xxx.xxx.xxx): icmp_req=1 ttl=55 time=43.4 ms

The main difference is now, instead of just an IP... you have a string of text with spaces in it. The command line to send the email sees the spaces as meaning more input and treats only the first word segment (in this case 64) as the subject and the rest as email addresses... hence the complaint that an email address you provided contained an invalid character : It was seeing "(xxx.xxx.xxx.xxx):" as an email address.

To use the string for a subject on the command line, you have to escape the variable in quotes like:

Code: Select all

echo $Resultping | mailx -s "$Resultping" [email protected]
Now the whole string which is in the $Resultping variable is considered the subject.. A long subject line...

I would tend to put just the IP address in the subject and the full string in the body of the message if you wanted that info like:

Code: Select all

Resultping=`ping -c 1 <hostname> | grep "64 bytes from <hostname>"`
$ResultIP=`echo $Resultping | cut -f2 -d'('| cut -f1 -d')'`
echo $Resultping | mailx -s "$ResultIP" [email protected]
This would generate an email like:

Code: Select all

From: ....
Subject: XX.XXX.XXX.XXX

64 bytes from hostname (xxx.xxx.xxx.xxx): icmp_req=1 ttl=55 time=43.4 ms
There are a lot of different ways to extract just the IP... awk, sed, (I used cut), Perl ....

part of the fun of learning is experimenting with different types of tools to learn how they work. Google is your friend there.... there are tons of examples a couple clicks away...