Zsh: clear the “You have mail“ message

Dramatic shell opening.

Do you see this message when you open a new terminal session?

You have mail.

The “You have mail” message comes from Zsh, checking the Unix email system. Zsh performs the check once on startup, printing the message before its first prompt.

Now, if you use the Unix email system, all is probably fine, you expect this message, and you know how you like to read your email. But if you’re like me and most people, you use a web-based email provider, so this email system only receives messages from processes on your computer, like cron.

To check the Unix mailbox, you can run the mail program, which opens up a prompt like this:

$ mail
Mail version 8.1 6/6/93.  Type ? for help.
"/var/mail/hacker": 16865 messages 16865 unread
>U  1 hacker@MBP  Mon Jul 18 22:21  20/869   "Cron <hacker@MBP> find ~/Documents/Projects -type d -iname '.tox' -ctime +4w -delete"
U  2 hacker@MBP  Wed Jul 20 15:21  20/800   "Cron <adamjohnson@Adams-MBP> find ~/Documents/Projects -type d -iname '.tox' -ctime +4w -delete"
U  3 hacker@MBP  Wed Jul 20 16:00  20/821   "Cron <adamjohnson@Adams-MBP> /usr/local/bin/lifelogger download && logger lifelogger downloaded >~/tmp/cron.log 2>&1"
...
?

The listing shows a few statistics and then up to one screenful of emails. Each email shows its number, receiver, date, sizes, and subject. They are stored locally in the mailbox path listed on the second line, typically /var/mail/<username>.

The ? at the bottom is a prompt. If you just press enter, it will show the first email:

?
Message 1:
From hacker@MBP.local  Mon Jul 18 22:21:00 2022
X-Original-To: hacker
Delivered-To: hacker@Adams-MacBook-Pro.local
From: adamjohnson@Adams-MacBook-Pro.local (Cron Daemon)
To: adamjohnson@Adams-MacBook-Pro.local
Subject: Cron <adamjohnson@Adams-MacBook-Pro> find ~/Documents/Projects -type d -iname '.tox' -ctime +4w -delete
X-Cron-Env: <SHELL=/bin/bash>
X-Cron-Env: <HOME=/Users/chainz>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=adamjohnson>
X-Cron-Env: <USER=adamjohnson>
Date: Mon, 18 Jul 2022 22:21:00 +0100 (BST)

find: /Users/chainz/Documents/Projects: Operation not permitted

Perhaps you’re like me and a failing background task running under cron, which uses the OS mail system to report problems. Or maybe some other process emailed you.

Regardless, if you don’t want the displayed email any more, you delete it with delete:

? delete
?

Commands in mail don't give any feedback unless there’s an error. Pressing enter again will show the next email. You can keep pressing it to page through. If you determine you don’t want any of the emails saved, you can delete them all by specifying *:

? delete *
?

Then quit with q:

? quit

Then, when you next start Zsh, the mail message will be gone. That is, unless whatever process was sending you mail still runs. In my case, I needed to remove my broken background task from cron's schedule with crontab -e.

By the way, to wrap all the above up and clear all emails in a single command, feed delete * as the input to mail:

$ mail <<< 'delete *'

Might be useful if you need to repeatedly clear known-useless emails.

Fin

May you be free of spam and terminal noise,

—Adam


😸😸😸 Check out my new book on using GitHub effectively, Boost Your GitHub DX! 😸😸😸


Subscribe via RSS, Twitter, Mastodon, or email:

One summary email a week, no spam, I pinky promise.

Related posts:

Tags: