8

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.

1
  • I am looking for an option within mail command. I do know how to check this using script.
    – shantanuo
    Mar 7, 2011 at 13:36

5 Answers 5

15
#!/bin/sh  

string=`some command`
len=${#string} 
if [ "$len" -gt "0" ]
   then echo $string | mail -s "tables altered on `hostname`" [email protected]
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`" [email protected]
5
  • One liner please?
    – shantanuo
    Mar 7, 2011 at 13:37
  • 2
    See my edit. And read the man page.
    – Sven
    Mar 7, 2011 at 13:52
  • mail: invalid option -- E # I get this error message.
    – shantanuo
    Mar 8, 2011 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, 2011 at 9:36
  • for Ubuntu: some command | mail -e -s "tables altered on `hostname` [email protected]
    – user219901
    Mar 31, 2017 at 11:01
9

I use the following:

$ mail --exec 'set nonullbody' -s Test [email protected]

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

1
  • Ta! ... Cleanest one so far .. Especially when using cron every hour ;)
    – GerardJP
    Apr 12, 2015 at 8:19
3

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:

$ mail -E 'set nonullbody' -s Test [email protected]

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:

$ echo -n "something" | mail -E 'set nonullbody' -s Test [email protected]

But this command doesn't send an email:

$ echo -n "" | mail -E 'set nonullbody' -s Test [email protected]
2

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`" [email protected] )
1
  • It seems to be missing the assignment of $len Jan 12, 2017 at 8:51
1

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

1
  • 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, 2015 at 17:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .