Matt Harris

Just another WordPress weblog

Using local mail (/var/mail/user)

2009-06-16T06:06:37+00:00June 16th, 2009

When developing websites it is quite common for there to be some kind of contact form or other mailing function. The problem is that should you, like me, have your email being forwarded through Google Mail you will find those emails never get to your Google address. The reason for this is simple, your computer is not recognised as a legitimate SMTP server so Google rejects the mail. The good news is you can instead tell your Website to send the emails to your local computer instead.

Very simply you just need to give the website you are testing the email address username@computername.local. For example my username is matt and my computer is called tribble-mbp so my email would be matt@tribble-mbp.local. Alternatively you can instead use the computer name as localhost making the emailmatt@localhost.

Once you have the email set to your local machine you can use the built in mail program in MacOS.

Using command line mail

To use mail you need to fire up the Terminal.app (Finder > Go > Utilities > Terminal) and type mail. Most of the time you will be presented with a very boring response similar to this:

No mail for matt

If you do have mail you will instead see an interactive prompt similar to this:

>N  1 matt@tribble-mbp.loc  Tue Jun 16 10:40  14/493   "Test"
? 

This is your mailbox, and in my case shows I have 1 new message from matt@tribble-mbp.loc(al) which was sent on Tuesday June 16 at 10:20. The subject of the message is “Test”. This is helpful but how do I view the message?

Handy mail commands

Command Purpose
1 (or any other number) Display the message with the ID equal to 1
help Display the help screen
h List the headers of the emails
d1 Delete message 1 (warning this is instant, there is no confirmation step)
d1-10 Delete messages 1 through 10
q Quit mail

Sending mail

The easiest way to send mail is to use the website you are developing but, should you wish to, you can use the command line tool. The best way to understand this is an example. We’re going to send an email to ourselves with the subject “Test” and the message “What’s up doc?”. To do this fire up Terminal.app and do the following:

  • Enter mail matt@tribble-mbp.local replacing matt@tribble-mbp.local with the correct address for you.
  • You will be prompted for the Subject: so enter Test and press enter/return.
  • It now looks like nothing is happening but in reality mail is waiting for you to enter the body of the message. On the line that has just started type What's up doc? but do not press enter.
  • To finish the message we have to ‘interrupt’ the program. We do this by holding down ctrl on the keyboard and pressing “ (sometimes you need to press it a couple of times).
  • You should find the letters EOT appear and the program exits. If not, release any keys you have and try pressing ctrl and d again until it does.

When all is done the screen should look similar to this:

mail matt@tribble-mbp.local
Subject: Test
What's up doc?EOT

Great! you’ve sent your first mail. Lets take a look at it by entering mail in the terminal. You should find something similar to this:

>N  1 matt@tribble-mbp.loc  Tue Jun 16 10:44  14/486   "Test"
? 1
Message 1:
From matt@tribble-mbp.local  Tue Jun 16 10:44:10 2009
X-Original-To: matt@tribble-mbp.local
Delivered-To: matt@tribble-mbp.local
To: matt@tribble-mbp.local
Subject: Test
Date: Tue, 16 Jun 2009 10:44:10 +0100 (BST)
From: matt@tribble-mbp.local (Matt Harris)
What's up doc?

To delete the message type d1, replacing the 1 with the correct ID of the message.

Sending attachments

Sending attachments is again something you would probably want to do using your web application however it is quite straight forward. The method here does have it’s problems with encoding but works out of the box – as this is only for development and on the local machine it’s something I’m happy living with. To send an attachment use the following command:

uuencode /path/to/file.jpg filename-for-recipient.jpg | mail matt@tribble-mbp.local

This will send a single message with the @file.jpg@ attached to the message. Helpful but what about sending some text with the message as well? Well, this is achieved using echo and the | technique, like this:

(echo "My message test"; uuencode /path/to/file.jpg filename-for-recipient.jpg) | mail matt@localhost

Alternatively you can send a text file as message text using the cat command like this:

(cat mytextfile.txt; uuencode /path/to/file.jpg filename-for-recipient.jpg) | mail matt@localhost

Summary

Hopefully this has been useful for you. I found it partifularly helpful for debugging this. Knowing how mail works also means you can setup automated mailings from your MacOS to yourself, such as log files or alerts.

When you start using mail a lot, managing messages in the command line can become quite laboured so in my next post I’ll show you how to read the command line mail using Thunderbird.