Scripting Mail to autoreply with an attachment

I am trying to create an Applescript, which will be triggered with a rule, to automatically send out a specific document to an e-mailer. What I want to happen is this:
= An e-mail arrives in the in-box with a specific To address (say:
= Mail app receives the e-mail on its periodic mail download.
= The To address triggers a rule that creates an e-mail reply, which will go to the sender of the incoming e-mail, and which will have a specific attachment (a PDF doc, say).

Where I am so far is that I can make and send the reply e-mail with the attachment (and the desired subject line and message text). The problem is triggering the rule when the mail arrives at Mail. As I have it now, it will be triggered if I run the script manually with the desired e-mail selected in the Mail inbox. But I want the script to run on the e-mail when it arrives, through a rule, without any need for user intervention.

So what the script needs to do is somehow, as the e-mails arrive, grab the replyto addresses of triggering e-mails, create the appropriate outgoing mail message (determined by the incoming address), and send it. It’s the first step–grabbing the replyto address from the triggering e-mail–that has me stumped.

The secondary problem is that the script only seems to work when it is loaded in the script editor. Not working as an application. Why would that be?

Additional note: This can be done in Outlook 2003 for Windows easily, without a script. Just requires a Rule and an Outlook template file.

David, great idea. I’d love to have this script too!



this should do what you requested.
Just change the path in the first line to the attachment you want

set theAttachment to "path:to:your:attachment"
using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		repeat with eachMessage in theMessages
			tell application "Mail"
				tell eachMessage
					set {theSender, theAddress, theSubject} to {extract name from sender, extract address from sender, subject}
				end tell
				set newMessage to make new outgoing message with properties {subject:"Re: " & theSubject, visible:true}
				tell newMessage
					make new to recipient at end of to recipients with properties {name:theSender, address:theAddress}
					tell content to make new attachment with properties {file name:theAttachment as alias} at after the last paragraph
				end tell
				send newMessage
			end tell
		end repeat
	end perform mail action with messages
end using terms from