How to read ‘var/mail/‘ on localhost

Easily read /var/mail/ on you localhost MAC or UNIX device with this PHP and HTML template on your apache run machine.

I swear I do a search for this every few months after I use terminal a few times and it pauses at launch to tell me “You have new mail in /var/mail” on my Mac.

Read var mail

I always go searching the web on where var/mail is, because I always forget. I think I might remember now, go to Finder, select Go / Go to Folder and enter /var/mail/. Simple.

go-to-folder

The not so simple part is reading the local user or users email. In my case my username is austinpassy. Opening up that file opens TextEdit as a locked file. So no edits can be made. Now if you are looking for a way to edit or view this file directly in Terminal I suggest you try another tutorial. I’ve done it before I just don’t feel like explaining that process here.

So if not Terminal, than?

That’s what I’ve been trying to do for the last few months and last night I finally started the process in accomplishing this. In a few quick steps this is what I did.

  1. Using local apache (in my case Desktop Server – although for local development I manually create development sites) I created a local site names http://mail.dev
  2. Installed a basic HTML template which can be found on GitHub.
  3. Uh, That’s it.

Pretty simple actually. What I did is create a PHP class that reads the mail file and outputs it as a HTML page. There is even a clear all button that will clear the whole file!

Getting started

Before it will work for you you’ll need to change the mail username on line 11 $this->mail = '/var/mail/username';, or use a GET command ?mail=/var/mail/username to get the email.

That is really the only thing you will need. Simple, short and sweet.

Download the code from GitHub and any pull requests for bugs.

Known issues

Wrapping each email in PRE tags to separate each email in the list. But the only thing I could find before each starting email was a -- and eleven numerical and alphabetical characters followed by a period. So since I am not the best at REGEX I came up with the best I could which is: /(-{2}[a-z0-9]{11}[.])/i. Know a better way? Let me know in the comments below!

Austin
Austin

πŸ’πŸ½β€β™‚οΈ Husband to Jeana.
⚾️ Dodgers & Brewers.
πŸ’» PHP Engineer.
πŸŒπŸΌβ€β™‚οΈGolfer; ~13 HDC.
🌱 Hydroponic Gardner.
🍿 Plex nerd.
πŸš™ '15 WRX, '22 Model 3 LR, & '66 Corvette C2.

Follow me on Twitter @TheFrosty & Instagram @TheFrosty.

Articles: 293

5 Comments

    • I don’t understand your issue. $this->mail = '/var/mail/username'; is referring to the location of your mail in your username folder. Replace “username” with your login name.

  1. Warning: fopen(/var/mail/username): failed to open stream: Permission denied in /Users/username/Projects/mail/classes/Mail/Mbox.php on line 227

    I am getting this error. πŸ™

Comments are closed.