0

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.

1

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

  • Great answer. Just a quick follow up - how does the local user check his or her email or where is his email address configured. – Startec Aug 12 '16 at 7:25
  • Hi @Startec I have updated the answer. – Nisheet Sep 15 '16 at 11:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.