How to access emails in Apple Mail programmatically

emailmail.app

I have an iMac running OS X 10.6.8 and use Apple Mail version 4.5.

I need to access specific parts of specific emails programmatically using Mathematica (others of course could do this more generally with any other programming language, I need to use Mathematica).

I need to find the specific eMails, which I can arrange to identify by either the:

  • "From" and/or
  • "Subject"

fields.

I then need to read either a string of numbers and characters from either the:

  • "Subject" or
  • "Body"

fields of the eMails.

So,

  • Can one do this?
  • If yes, how are emails in Apple Mail kept (flat file, database file, csv, unique format, other)?
  • What is the name of the above file?
  • In what directory can I find it?
  • How do I access the file and the relevant information in the file?

Thanks in advance.

Best Answer

One can do this. The following shows what I've found...

Individual Apple Mail emails reside in a directory, typically something like the following:

/Users/UserName/Library/Mail/POP-myEmail@isp.com@pop.secureserver.net/INBOX.mbox/Messages

as ".emix" files readable by any text editor.

".emix" files have a sequential numbering by mails received by one's Apple Mail itself rather than the particular mailbox, something like:

18373.emix
18376.emix
18378.emix

The ".emix" files have clearly marked identifiers of their content. An abridged ".emix" file follows::

Received: (somemailprovider ...); 16 Jul 2012 00:04:00 -0000
Received: from unknown ...; 16 Jul 2012 00:04:00 -0000
X-IronPort-Anti-Spam-Result: AuEPABVXA1DR...
Received: from ....isp.com ...; 15 Jul 2012 17:03:59 -0700
Received: by qcsg1 ...; Sun, 15 Jul 2012 17:03:59 -0700 (PDT)

DKIM-Signature: ...
Received: ...; Sun, 15 Jul 2012 17:03:59 -0700 (PDT)

Return-Path: <myEmail@isp.com>
Received: from iMac-6.local ....

Date: Sun, 15 Jul 2012 20:03:54 -0400 (EDT)
From: myFromEmail@isp.com
To: myToEmail@isp.com
Message-ID: <50035a6e.02d0e00a.1fef.....com>
Subject: TEST
MIME-Version: 1.0
Content-Type: multipart/mixed;boundary="----=_Part_0_1792110529.1342397034510"
X-Nonspam: None

------=_Part_0_...
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit 

------=_Part_0_...
Content-Type: image/gif; name=0001.gif
Content-Transfer-Encoding: base64
Content-Disposition: inline; filename=0001.gif

So, given all of this one needs to:

  • Identify which email one wants to read (I can use date and a specific email directory) ;
  • Import the text of the email into Mathematica;
  • Parse the required information using the appropriate identifiers in the ".emix" file.

A little cumbersome, but workable.