The script corrects most of my munged mail... but there are some
problems:
The first - the time picked up is that in the last received header,
which is generally the time that the mail hit the first MTA. This is
great if the broken part of the date is the sent time, but in my case
the sent time was fine - the received time was b0rked. When Entourage
crashes with the imap server connected (dovecot 0.99.11), it begins
re-receiving all of the email but uses the current time as the received
time. Oddly enough, this became evident after a number of crashes of
the script on a large group of emails. Examination of the event log led
to the identification of an apparent flaw in the script:
The header of a typical email that caused a crash, munged for privacy:
Received: from pu.ogauud.com (ns.ogauud.com [x.x.x.x])
by ogud.com (8.12.11/8.12.11) with ESMTP id j2AGv5qW008089;
Thu, 10 Mar 2005 11:57:05 -0500 (EST)
(envelope-from
[email protected])
resulting in:
"Invalid date and time Thu, 10 Mar 2005 11:57:05 -0500 (EST)
(envelope-from
[email protected])."
Another:
Received: by mailagent1.ientrymail.com (PowerMTA(TM) v2.0r1) id
h5hp7m03us84; Mon, 7 Mar 2005 10:25:47 -0500 (envelope-from
<
[email protected]>)
Resulting in:
"Invalid date and time Mon, 7 Mar 2005 10:25:47 -0500 (envelope-from
<
[email protected]>)."
Examining the emails themselves showed clearly that the script was
looking at the last instance of the Received header (correctly
obviously, according to Barry's 1.0 script), which is generally the
first MTA the email hit on it's way out - so more likely the sent
time, and not the received time in my system. And of course, the odd
data appended to the headers were the result of non-standard
configuration of the sending MTA. While my inbound SMTP imap server may
also be misconfigured, at least it will be consistent ;-). And
naturally it will have the correct received time. Sort of (more later).
So I solved both problems by modifying:
repeat with aHeader in theHeaders
if aHeader starts with "Received:" then copy contents of
aHeader to receivedHeader
end repeat
with
repeat with aHeader in theHeaders
if aHeader starts with "Received:" then copy contents of
aHeader to receivedHeader
if contents of receivedHeader is not "" then exit repeat
end repeat
That results in the first Received header time being picked up. OK, all
consistent with Barry.
The next problem (which highlighted a more significant issue) is that
the script fails when it hits a timestamp that includes that odd
appendix (envelope-from
[email protected]). I solved that by modifying:
set AppleScript's text item delimiters to {";"}
set theDate to (last text item of receivedHeader)
set time received of theMess to date theDate
to
set AppleScript's text item delimiters to {";", "("}
set theDate to (last text item of receivedHeader)
set time received of theMess to date theDate
However, it became evident that for some reason the script is not able
to deal with timezones in the time stamp. So:
Received: from mail pickup service by smtp1.bellevue.com with Microsoft
SMTPSVC;
Thu, 7 Apr 2005 12:18:27 -0000 (UTC)
results in the time being corrected to 12:18:27 rather than 05:18:27
which is correct for my local timezone. I am unable to find any
references in the Apple Scripting man pages to indicate a mechanism to
handle timestamps that include an offset. My imap server is set to UTC
so all timestamps show -0000 (UTC).
In any case, the script solved almost all the problems, and at least
brought the time stamps within range (+7 hours) ;-)). Thanks!