Ubuntu – Default `mail` program used by cron

cronemail

In reading this link it says:

The crontab command is used to maintain crontab files for individual users. By default the output of a command or a script (if any produced), will be email to your local email account

When I type mail it says that no package called mail is currently installed.

My question is threefold:

  • How does Ubuntu send mail to the "local email account"?
  • Do I need to install a mail client to send and receive mail (if so which one)?
  • How do I know what my local email address is?

I see messages all over the place referring to "messaging" other users, but I have never done this, nor checked "system mail" and I would be appreciative of any insight here.

Best Answer

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 dont 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:

  1. 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

  1. 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

  1. 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

Related Question