There might arise situations, where a monitoring script is in dire need to send an e-mail. But how do you send mail when for some reason the mail system itself is dysfunctional? Bash to the rescue. Building upon my
previous post on bash networking, I put together a bash_mail function, which is capable of connecting directly to an SMTP server (without authentication or SSL) and send email message. The function respects best practices and tries not to trigger anti-spam mechanisms by dutifully awaiting SUCCESS response codes from server and aborting on server fail code. The function returns 0 when all went OK and 1 or 2 depending on the state it dropped off.
UPDATE: Taken into account ideas from a coworker, I tweaked the code to (1) run in subshell to release filedescriptor requirements, (2) use loop for DRY and (3) drop cat usage to be as much as possible self-sustained.
UPDATE 2: Script adjusted to comply to SMTP standards regarding instead of simple as pointed out by Zoot in the comments.
The function can be used as a function in other scripts. Just source it in, where needed. You can also define the arguments, without rewriting the function:
source bash_mail.sh # just send the mail with hard-coded parameters bash_mail # define important parameters bash_mail "email@example.com" "Still Panic"