The description reads:
Being able to send emails from command-line from a server is quite
useful when you need to generate emails programatically from shell
scripts or web applications for example.
How the mail command works
For those who are curious about how exactly the mail command delivers the mails to the recipients, here is a little quick explanation.
The mail command invokes the standard sendmail binary (/usr/sbin/sendmail) which in turns connects to the local MTA to send the mail to its destination. The local MTA is a locally running smtp server that accepts mails on port 25.
mail command -> /usr/sbin/sendmail -> local MTA (smtp server) -> recipient MTA (and Inbox)
This means that an smtp server like Postfix should be running on the machine where you intend to use the mail command. If none is running you get the error message "send-mail: Cannot open mail:25".
Install Sendmail
Open terminal & type following command in terminal.
sudo apt-get install mailutils
sudo apt-get install sendmail
Configure Sendmail
After installing sendmail , you should configure sendmail. Its little hard. But don
t worry after that we can spoof email to anyone.
Type following command on terminal
sudo gedit /etc/mail/sendmail.mc
It will open sendmail.mc file.
For example your last two lines are as follows:
MAILER(`local')dnl
MAILER(`smtp')dnl
Put this code before those two lines.
MAILER_DEFINITIONS
define('SMART_HOST',`smtp.gmail.com')
Now close that file
Now we will generate configure file from .mc file so type following command in terminal.
sudo bash -c 'cd/etc/mail/ && m4 sendmail.mc >sendmai.cf'
Now everything is complete, try to send mail using terminal
Some examples from the link that I provided:
Use the mail command
Run the command below, to send an email to someone@example.com. The
s option specifies the subject of the mail followed by the recipient
email address.
$ mail -s "Hello World" someone@example.com
- Send mail to a local system user
To send mail to a local system user just use the username in place of the recipient address
$ mail -s "Hello World" username
- Specify the FROM name and address
The "-a" option allows to specify additional header information to attach with the message. It can be used to provide the "FROM" name and address. Here is a quick example
# echo "This is the message body" | mail -s "This is the subject" mail@example.com -aFrom:sender@example.com
The a option basically adds additional headers. To specify the from name, use the following syntax.
$ echo "This is the body" | mail -s "Subject" -aFrom:Harry\<harry@gmail.com\> someone@example.com
Note that we have to escape the less/great arrows since they have special meaning for the shell prompt. When you are issuing the command from within some script, you would omit that.
Sources:
What is mail?
mail command examples
Install and Configure mail