Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

How do I suppress blank emails? FOr e.g. in the following example, I will like to

some command | mail -s "tables altered on `hostname`" me@company.com

I get the following message "after" sending the message:

Null message body; hope that's ok

This is not ok. I do not want to send mail if there is no text.

share|improve this question
    
I am looking for an option within mail command. I do know how to check this using script. –  shantanuo Mar 7 '11 at 13:36

3 Answers 3

up vote 8 down vote accepted
#!/bin/sh  

string=`some command`
len=${#string} 
if [ "$len" -gt "0" ]
   then echo $string | mail -s "tables altered on `hostname`" me@company.com
fi  

This will send the mail only if the output of the command is at least 1 character long, but this may include blanks etc.

(The solution above works, but is unnecessary. man mail reveals the -E option):

some command | mail -E -s "tables altered on `hostname`" me@company.com
share|improve this answer
    
One liner please? –  shantanuo Mar 7 '11 at 13:37
1  
See my edit. And read the man page. –  Sven Mar 7 '11 at 13:52
    
mail: invalid option -- E # I get this error message. –  shantanuo Mar 8 '11 at 5:07
    
What OS are you on? There might be many versions of mail(x) available, Linux usually uses the BSD version. Did you check the man page to see if there are other options with that purpose? If there are not, you would need to either use my script of Jeff's reduced version of it. –  Sven Mar 8 '11 at 9:36

One-liner version of SvenW's answer (the creds should go to him, not me)

string=`some command`; [ "$len" -gt "0" ] && ( echo $string | mail -s "tables altered on `hostname`" me@company.com )
share|improve this answer

I use the following:

$ mail --exec 'set nonullbody' -s Test test@example.com

Which I found in the GNU mail documentation under the nullbody section.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.