First thing: I’m pretty new to Applescript, so go easy, and speak slowly…
I use a trouble-ticketing system here at work (Footprints) which allows for emailing information into tickets, but doesn’t retain header information. This means a lot of going into my Sent mail and forwarding messages on manually, something I’m trying to get around with applescript.
I’ve written a script that basically takes a selected set of emails and sends appropriately formatted messages from them to the ticketing system. It takes the content of each message, and the useful header info (From, To, Message ID, subject, Date/time) and sends it with the appropriate ticket # info to the ticketing system.
The main difficulty I have is that the “content” of a message in Applescript lacks quote levels, so I’ve had to try to take the “source” of each message and remove each line in the “all headers” of the message from the source, which would leave just the content, but with accurate quoting.
I seem to have this working…mostly. I can remove all the header info, but a few lines seem to remain, and I can’t figure out why. below is the script, and the headers that remain compared with the headers from the original. Any ideas? I’m at a loss.
Here’s an example of what I see left:
Received: from out4.smtp.messagingengine.com (out4.smtp.messagingengine.com [n.n.n.n])
by x.x.x.edu (8.12.11/8.12.11) with ESMTP id blah
for blah@blah.edu; Tue, 7 Mar 2006 13:49:15 -0500
Received: from frontend1.internal (mysql-sessions.internal [n.n.n.n])
by frontend1.messagingengine.com (Postfix) with ESMTP id blah;
Tue, 7 Mar 2006 13:28:31 -0500 (EST)
Received: from web1.messagingengine.com ([n.n.n.n])
by frontend1.internal (MEProxy); Tue, 07 Mar 2006 13:28:31 -0500
Received: by web1.messagingengine.com (Postfix, from userid nn)
id blah; Tue, 7 Mar 2006 13:28:31 -0500 (EST)
MIME-Version: 1.0
from the original:
Return-Path: blah2@blah2.edu
Received: from out4.smtp.messagingengine.com (out4.smtp.messagingengine.com [n.n.n.n])
by x.x.x.edu (8.12.11/8.12.11) with ESMTP id blah
for blah@blah.edu; Tue, 7 Mar 2006 13:49:15 -0500
Received: from frontend1.internal (mysql-sessions.internal [n.n.n.n])
by frontend1.messagingengine.com (Postfix) with ESMTP id blah;
Tue, 7 Mar 2006 13:28:31 -0500 (EST)
Received: from web1.messagingengine.com ([n.n.n.n])
by frontend1.internal (MEProxy); Tue, 07 Mar 2006 13:28:31 -0500
Received: by web1.messagingengine.com (Postfix, from userid nn)
id blah; Tue, 7 Mar 2006 13:28:31 -0500 (EST)
Message-Id:
X-Sasl-Enc: 4P4Dy2VgsM9DLe98Uq406hxmZZnLG0N0Hq+rmn6H156N 1141756111
From: “name1” blah2@blah2.edu
To: “name2” blah@blah.edu
Cc: “name3” blah3@blah3.net
Content-Disposition: inline
Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset=“ISO-8859-1”
MIME-Version: 1.0
X-Mailer: MIME::Lite 5022 (F2.73; T1.15; A1.64; B3.05; Q3.03)
References:
Subject: Re: this is a subject
In-Reply-To:
Date: Tue, 07 Mar 2006 13:28:31 -0500
X-Greylist: Delayed for 00:20:43 by milter-greylist-2.0.2 (xxxx); Tue, 07 Mar 2006 13:49:15 -0500 (EST)
and here’s the script:
using terms from application “Mail”
set AppleScript's text item delimiters to "ENTRY="
set theForwardedRecipient to "blah@blah.edu"
(* set useRaw to true if you want to include the raw source of the email instead of just the simple header and content *)
set useRaw to false
tell application "Mail" to set theSelectedMessages to selection
if (count of theSelectedMessages) is equal to 0 then
display dialog "Please select one or messages first, then run this script again."
else
repeat with eachMessage in theSelectedMessages
(* Check if it already has an ENTRY= field. Assumes the project is abilene *)
if subject of eachMessage contains "ENTRY" then
(*find text "ENTRY=(.*)" in subject of eachMessage with regexp *)
set theForwardedSubject to subject of eachMessage
else
set theForwardedTicket to display dialog "Enter Ticket # for Subject: \"" & "\"" default answer ""
set theForwardedSubject to subject of eachMessage & " ENTRY=" & text returned of theForwardedTicket
end if
(* dump entire source text if useRaw is set. Otherwise, use certain headers *)
if useRaw then
set theForwardedContent to source of eachMessage
else
set myHeaders to all headers of eachMessage
set mySource to source of eachMessage
(* we want the raw source, minus the ugly headers...so: *)
repeat with i from 1 to count of paragraphs of myHeaders
set lineToRemove to paragraph i of myHeaders as text
display dialog "removing: \"" & lineToRemove & "\""
set mySource to remove(mySource, lineToRemove)
end repeat
set theForwardedContent to "From: " & sender of eachMessage & return & ¬
"Date Received: " & date received of eachMessage & return & ¬
"Date Sent: " & date sent of eachMessage & return & ¬
"Subject: " & subject of eachMessage & return & ¬
"Message ID: " & message id of eachMessage & return & return & ¬
mySource
end if
tell application "Mail"
set theForwardedEmail to make new outgoing message with properties {subject:theForwardedSubject, address:theForwardedRecipient, content:theForwardedContent}
tell theForwardedEmail
set subject to theForwardedSubject
make new to recipient at beginning of to recipients ¬
with properties {address:theForwardedRecipient}
end tell
send theForwardedEmail
end tell
end repeat
end if
end using terms from
on remove(textToSearch, textToRemove)
set oldDelimiters to AppleScript’s text item delimiters
set AppleScript’s text item delimiters to textToRemove
set returnText to text items of textToSearch
set AppleScript’s text item delimiters to “”
set returnText to returnText as text
set AppleScript’s text item delimiters to oldDelimiters
return returnText
end remove