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`" [email protected]
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.
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):I use the following:
Which I found in the GNU mail documentation under the nullbody section.
One-liner version of SvenW's answer (the creds should go to him, not me)
For some implementations of
mail
the command line switch-E
is equivalent to--exec
which let's you execute a command. In this case daharon's answer works quite well. You can even shortened it to:If you want to test this behavior use the
echo
command with the-n
command line switch to suppress the trailing newline.This command sends an email:
But this command doesn't send an email:
example:
echo -n "" | mail -E -s "Log Message" [email protected]
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