48

When I open up my terminal it says "you have mail", anyone has any idea of why? I am running OS X, but since it too is based on Unix and relies on files such as bashrc, bash_profile etc. I thought somebody here might know, and I'm not sure it's a platform specific problem!

6 Answers 6

43

It sounds like something has sent mail on (and to) the machine using the local mail exchanger. Most likely the email is an automated message from some installed package. Once you log in, type mail on the terminal to read and (presumably) delete the relevant mail. (Inside mail, use ? to find out what the commands are.) Once you've read or deleted any unread mail, you won't see the "You have mail" message again until/unless something else sends mail in the same way. Odds are once you know what's sending you the mail, you can find a configuration option to change where it sends it to.

1
  • I noticed that after deleting messages, if I exit with x the messages are still there when I reopen mail. But q "saves" it. I'm sure this "makes sense" from the docs we see when we type ?, but it wasn't apparent to me at first
    – floer32
    Mar 2, 2021 at 6:45
32

It is a "platform specific problem" :-)

Mac OS X uses as default shell the BASH like many other Un*x/Linux systems.

Mail from your system is stored in mailbox for your user, often located in /var/mail/$user

Because sometimes your mailbox will contain important messages (a failed cronjob task, etc.) you don't to check it every time by yourself.

The BASH (and many other shells) have a feature called mailcheck, which outputs your message the first time you open your terminal.

$MAILCHECK is an environment variable which contains the seconds when its time to check for new mail. Mail is also checked before displaying the primary prompt. See more in man bash - Bash Variables.

To prevent the checks for new mail, you can simply unset $MAILCHECK. The right place for this is your .bashrc file, which will be read when you open up a shell. Its in your user home directory.

Append to ~/.bashrc:

unset MAILCHECK

to disable the message.

Of course it is also possible to simply delete the mails like T.J. Crowder mentioned :-)

7

Using Raspian (Debian based distro for Raspberry Pi) over SSH, I had to comment out the following line in /etc/pam.d/sshd to get rid of it.

session    optional     pam_mail.so standard noenv # [1]
4
  • 4
    Could you comment a bit more on how you found this out? It might be of help to people with similar questions.
    – vonbrand
    Feb 26, 2013 at 13:52
  • I found the solution in a mailing list. the author says he stumbled over the line when poking around in /etc/pam.d/sshd. I’m afraid that’s all I know.
    – Frode
    Feb 26, 2013 at 14:14
  • This fixed it for me on ubuntu as well.
    – Jay
    Mar 20, 2013 at 1:27
  • Comment this line worked for me on Ubuntu (DigitalOcean) and Debian (local network) over SSH. I've also disable motd. Jul 22, 2015 at 16:06
3

I have a sheevaplug with Debian. I commented the reported line inserting # and solved the problem.

File to edit in debian:

/etc/pam.d/sshd

Line to comment in file:

session    optional     pam_mail.so standard noenv # [1]
2

Run crontab -e and add output to your cron jobs. If you want to disable emails for just one cron job, you will need to redirect its output to /dev/null by adding > /dev/null 2>&1 at the end of the line.

For example:

* * * * */5 /path/to/command arguments > /dev/null 2>&1
1

I was also having this issue of "You have mail" coming up every time I started Terminal. It was caused by a locally hosted website sending mail (Wordpress, in my case).

To view the messages

You can access the mail by simply using the command

mail

This launches you into Mail, and it will right away show you a list of messages that are stored there. If you want to see the content of the first message, use

t

This will show you the content of the first message, in full. You'll need to scroll down through the message to view it all, by hitting the down-arrow key.

If you want to jump to the end of the message, use the

spacebar

If you want to abort viewing the message, use

q 

To view the next message in the queue use

n

... assuming there's more than one message.

NOTE: You need to use these commands at the mail ? command prompt. They won't work whilst you are in the process of viewing a message. Hitting n whilst viewing a message will just cause an error message related to regular expressions. So, if in the midst of viewing a message, hit q to quit from that, or hit spacebar to jump to the end of the message, and then at the ? prompt, hit n.

Viewing the content of the messages in this way may help you identify what attempted to send the message(s).

You can also view a specific message by just inputting its number at the ? prompt. 3, for instance, will show you the content of the third message (if there are that many in there).

DELETING MESSAGES

Use the d command (at the ? command prompt )

d [message number]

To delete each message when you are done looking at them. For example, d 2 will delete message number 2. Or you can delete a list of messages, such as d 1 2 5 7. Or you can delete a range of messages with (for example), d 3-10.

You can find the message numbers in the list of messages mail shows you.

To delete all the messages, from the mail prompt (?) use the command d *.

You will need to use q to quit mail, which also saves any changes.

If you'd like to see the mail all in one output, use this command at the bash prompt (i.e. not from within mail, but from your regular command prompt):

cat /var/mail/<username>

And, if you wish to delete the emails all in one hit, use this command

sudo rm /var/mail/<username>

Source of messages

In my particular case, there were a number of messages. It looks like the one was a returned message that bounced. It was sent by a local Wordpress installation. It was a notification for when user "Admin" (me) changed its password. Two additional messages where there. Both seemed to be to the same incident.

What I don't know, and can't answer for you either, is WHY I only recently started seeing this mail notification each time I open Terminal. The mails were generated a couple of months ago, and yet I only noticed this "you have mail" appearing in the last few weeks. I suspect it's the result of something a workflow I installed in Alfred, and that workflow using Terminal bash to provide notifications... or something along those lines.

Deleting all messages without further ado

If you have no interest in determining the source of the messages, and just wish to get rid of them, it may be easier to do so without using the mail command (which can be somewhat fiddly). As pointed out by a few other people, you can use this command instead:

sudo rm /var/mail/YOURUSERNAME

(Originally posted by me here)

You must log in to answer this question.