a Mail.app rule to log incoming messages

To keep track of my job, I need to have a record of any email I receive. Long ago in the days when email was done via terminal windows, I had set up an automatic procedure to write the basic information on each incoming email to a log file. Now that email is done by servers and local clients, this method has become impractical. I needed another way to generate my email log.

This page describes a way to log email using the rule facility of apple's Mail.app . Before doing this I searched for a pre-existing rule, but couldn't find one. So I'm writing down what I did, so others can use it.

The mail rule acts on all incoming mail messages and executes an applescript called Log Mail Headers.scpt. Here is the applescript:

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			repeat with eachMessage in theMessages
				set summary to "From: " & sender of eachMessage
				set summary to summary & "; Subject: " & subject of eachMessage
				set summary to summary & "; sent: " & date sent of eachMessage
				set summary to summary & "; size: " & message size of eachMessage & " bytes"
				do shell script "/bin/echo " & quoted form of summary & " >> ~/ReceivedMailLogs/log"
			end repeat
		end tell
	end perform mail action with messages
end using terms from
To set this up, go into the applescript editor, open an new window and paste in this script. Name your script Log Mail Headers.scpt and put it in /Library/Scripts/Mail Scripts/Rule Actions/. (You might prefer to put the script in your own ~/Library/Scripts/... folder instead.) Next, create a folder in your home directory called RecievedMailLogs, and create a text file inside this folder called log. Now go into Mail.app and create the new rule. Mine is called Log Headers. For the first blank that selects when the rule is to be invoked, select Messages, of type mail. For the second blank that tells what the rule does, select "run applescript" then browse to the

The script makes log records that look like this:

From: "Nature Publishing Group" ; Subject: Help us to better understand authors; sent: Saturday, February 15, 2014 8:25:05 AM; size: 26330 bytes

remarks and caveats

tips for your log file

ways the script can malfunction