Checking Mail with mail on Linux

Checking Mail with mail on Linux

After logging in to a Linux system, you may often see:

you have mail.

This means there is local mail for the current user in the system mailbox. Many Linux distributions store local mail in /var/spool/mail/username or /var/mail/username. You can view it directly with the mail or mailx command.

Viewing Mail

Run this in the terminal:

mail

mail lists the messages in the current user's mailbox and displays them in chronological order. After entering the interactive interface, the common operations are:

Enter       View the next message
p           Display the current message again
d           Delete the current message
s filename  Save the current message to the specified file
q           Quit mail
h           Display the message list again
?           View help

If you only want to confirm whether the local mailbox file exists, you can also check:

ls -l /var/spool/mail/$USER /var/mail/$USER 2>/dev/null

The path may vary between systems, but usually one of the two paths above will exist.

Sending Yourself a Test Email

You can first send yourself an email and then read it with mail, which makes it easier to understand the workflow:

mail frank

Then enter the subject and body:

Subject: test
This is a mail test

After finishing the body, press Ctrl-D to end input. The terminal will usually display:

EOT

Then check the mail:

mail

You may see output similar to this:

"/var/spool/mail/frank": 1 message 1 new
>N  1 frank@xteam.xteamlinux.com  Thu Mar 25 11:00  13/403  "test"
&

Enter the message number, or simply press Enter, to view the content:

Message 1:
From frank Thu Mar 25 11:00:25 1999
To: frank@xteam.xteamlinux.com
Subject: test

This is a mail test
&

& is the interactive prompt used by mail. At this point, you can enter d to delete the message or q to quit.

Sending Mail from a File

If you have already prepared the message body, you can pipe the file contents into mail:

mail -s "test subject" user@example.com < message.txt

You can also send it to a local user:

mail -s "local test" "$USER" < message.txt

Whether mail can be sent to an external address depends on whether a mail transfer agent is configured on the machine, such as sendmail, postfix, or another compatible program. If you are only sending and receiving mail between local users on the same machine, a full external mail server configuration is usually not required.

Multi-User Terminal Communication: write and mesg

When Linux is used in a multi-user environment, a message like this may suddenly appear in the terminal:

Message from renee tty2...

It may also be accompanied by a notification sound. This means the user renee is trying to send you a terminal message with the write command. You can respond with:

write renee

This establishes a simple text communication line between your terminal and renee's terminal. What the other person types appears on your terminal, and what you type appears on the other person's terminal.

A common convention is:

(o)   Indicates that one turn is finished and it is the other person's turn
(oo)  Indicates that the conversation is over

Example:

renee's terminal:                 frank's terminal:

[renee@xteam renee]$ write frank
                                  Message from renee tty2...
                                  $ write renee
Message from you tty1...

did you forget lunch? (o)         did you forget lunch? (o)
ten minutes (o)                   ten minutes (o)
ok (oo)                           ok (oo)
Ctrl-D                            Ctrl-D
EOF                               EOF

In addition to Ctrl-D, you may also be able to use Delete to exit the write command, depending on your terminal settings.

If you do not want others to send messages to your terminal, use:

mesg n

To allow messages again:

mesg y

When you send a write request to a user who has disabled message receiving, or to a user who is not logged in, write will display the reason why communication is not possible.

Reposted from: http://edu.codepub.com/2010/0413/21978.php

Leave a Reply