Today I’ll help you get started with sending mails from the Linux command line and in shell scripts.We will begin with the “mail” command.
First we run a quick test to make sure the “sendmail” application is installed and working correctly on server. Execute the following command, Make sure to replace “firstname.lastname@example.org” with your e-mail address.
mail “Hello world” email@example.com
2) How to use mail command to send mail from terminal ?
You can use the mail command with below format and send the mail from terminal. After typed your subject press Ctrl+D, The command prompt will ask you if you want to mark a copy of the mail to any other address, if needed type it or else, hit enter. Check your mailbox. This command will send out a mail to the email id mentioned with the subject, “Hello world”.
Now, i’m going to check my gmail inbox whether the mail are received or not. And found that its received.
3) How to use mail command to send mail from terminal with echo ?
Use the mail & echo command with below format and send the mail from terminal.
# echo "This is test mail by seemab" | mail -s Test firstname.lastname@example.org
4) Sending main from a shell script
Now you can send mails from your shell scripts. Here’s a simple shell script that gives you a reading of the usage of space on your partitions and mails the data to you.
#!/bin/bash df -h > /tmp/mail_report.log free -m >> /tmp/mail_report.log mail -s “disk and RAM report” email@example.com < /tmp/mail_report.log
Save these lines in a file on your Linux server and run it. You will receive a mail containing the results of the command.