Tuesday, August 3, 2010

Sending mail from command line or shell script

How to send mail from the Linux Command line (or shell script) using the mail command.

echo "This is the body of the email" | mail -s "Email Subject" recipient@domain.com -- -f senderaddress@domain.com


The -- -f section is used to plug in a sender address, otherwise the mail will be sent from your root or user account from the actual physical server name - e.g root@server1.domain.com


This comes in handy for notification of script execution, for example, jobs in the crontab. Placing this code immediately after the last command in a shell script will ensure relevant person(s) are notified when the script is called.

-n