Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 9 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
2  
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
    
Ta! ... Cleanest one so far .. Especially when using cron every hour ;) – GerardJP Apr 12 '15 at 8:19

example:

echo -n "" | mail -E -s "Log Message" me@mydomain.tld

explain:

an empty string has endline character so we have to add -n to exclude in this testing echo \n (cr). But if your body message is not existing hence mail will exit/stop sending.

sources:

Why does mail -E let me send empty messages? http://heirloom.sourceforge.net/mailx/mailx.1.html

share|improve this answer
    
You're explanation falls a bit short, mainly because you're not actually answering the original question of how to suppress blank emails. - In my humble opinion you're missing something at the start of your answer along the lines of: "Many implementations of mail support the -E command line switch which does <explanation of -E> and an example of how you would improve the original command line and only then your explanation of how even -E is not quite foolproof.... – HBruijn Sep 13 '15 at 17:51

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.