mail is pretty dumb and usually just lobs the message off to a
sendmail binary. Unfortunately there are multiple implementations of
mail so you'd ideally need to inspect the docs or code to see what sort of error handling (if any) is done for the exact version in use.
In addition to the mail service that
sendmail is part of—Mail Transport Agents (MTA) which on RHEL5 would most likely be Sendmail or Postfix—you will likely also need DNS as the MTA will need to perform a DNS lookup on
fooexample.blah to be able to send there. Possibly also a MTA queue runner if there's a temporary failure and the email ends up in a MTA queue directory somewhere (RHEL MTA include such queue runners by default). Oh, and a filesystem, if
/var or whatever is full the MTA will probably not be able to queue additional mails and therefore will likely not accept the message from
mail. (Especially if due to some other error the alert system fills up
/var and then more alerts are created and then when it unbreaks you have to call the pager company because they disabled your pager because it got sent a bazillion alerts and oh I so do not miss those days.)
... and a working network, depending on how broadly you want to define service ... and also there are optional but commonly used anti-spam services; what happens when Gmail or Exchange are rejecting or discarding your alerts as spam?
Also do note the lack of error checking on that
mail (or a
exec call) could fail, and then what? Slightly more sensible may be to log such failures somewhere:
openlog("homegrown-monitoring-101", "ndelay", "user");
system("echo ... root") == 0
or syslog(LOG_ERR, "non-zero exit code from mail command");
And then ideally have something like
sec.pl check for
homegrown-monitoring-101 logs and report on them (via a grouped summary, not the usual cron spam of one email per log message...).
mail or the MTA may also leave
dead.letter files somewhere (assuming it can write to where it wants to write those) which monitoring in theory could check for, but if there's a new
dead.letter and your alerting is via email, you probably need some other protocol to report that error.
On centos 7 systems I instead use the
perl-Email-Sender package over
mail to lob messages places:
use Email::Sender::Simple qw(sendmail);
my $message = ...;
my $email = Email::Simple->create(
header => [
To => $username . '@example.edu',
From => 'firstname.lastname@example.org',
Subject => "...",
body => $message,