Force mail to autoreplay to reply to not to sender

When setting a Mail Rule with the action ‘reply to message’, the generated reply always goes to the original sender, completely ignoring the ‘reply-to’ address.

I have an autoreply rule set up to send out a password when notified of payment.
Paypal recently changed their from address to a generic ‘member@’ address and started putting the purchaser’s address in the ‘reply to’ field.
My replies started failing.
I contacted them and they say this is a permanent change.

Can applescript get around this? (i.e. can it force mail to use the reply to address?)

Thanks

Unfortunately, no. You may have better luck with a different scriptable mail client. Mac Mail’s pretty squirrely when being run by scripts.

Thanks Nik,

not the answer I was looking for but after having tried to make applescript control replies in mail for a while now I tend to agree with you.
Perhaps I was waiting for a miracle cure that does not (yet I hope) exsit.

I’ll keep working on a script able to extract the client’s email address from the email’s text or subject, create a new msg and send that instead. I’ll post if i get it going without glitches.

Hi. Try this:

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			repeat with aMessage in theMessages
				set aMessage to (contents of aMessage)
				
				set replyAddress to extract address from aMessage's reply to
				set replyName to extract name from aMessage's reply to
				set mymail to make new outgoing message at the beginning of outgoing messages with properties {subject:"This is my Subject"}
				tell mymail
					make new to recipient at beginning of to recipients with properties {address:replyAddress, name:replyName}
					set content to "This is my content"
				end tell
				--show message window	
				set visible of mymail to true
				activate
				--send mymail  -- uncomment to send the message				
				
			end repeat
		end tell
	end perform mail action with messages
end using terms from

Thanks adayzdone,

I’ll test it tomorrow

adayzdone, is that “extract email” handler new? I don’t recall that from the last time I was crawling around in Mail’s dictionary.

It has been there as long as I can remember.

Hi adayzdone,

Thanks once more for your input.

I have just finished testing:
Your script works like a charm when “apply rules” is chosen from the menu but fails each time when the rule is auto acting on receipt of a new message.
After having thought for a while that the applescript never even started I added some say statements as you can see below. The try statements on error assigns lorem ipsum values to the variables so the script can continue execution. The applescript starts, runs point one then fails on two and three and succeeds on all subsequent commands.
You can test by sending a qualifying msg to yourself.

I researched the apple forums but was unable to come up with a valid reason for this behaviour.

using terms from application "Mail"	
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			repeat with aMessage in theMessages
				set aMessage to (contents of aMessage)
				tell me to say "one ok"
				-- tell me to delay 30 -- did not work two still failed
				try
					set replyAddress to extract address from aMessage's reply to
					-- set replyAddress to extract address from reply to of aMessage -- failed same as above
					-- set replyAddress to aMessage's reply to as rich text --  -- failed same as above
					tell me to say "two ok"
				on error
					set replyAddress to "joeBlog@wordpower-software.com"
					tell me to say "two failed"
				end try
				try
					set replyName to extract name from aMessage's reply to
					tell me to say "three ok"
				on error
					set replyName to "Joe Blog"
					tell me to say "three failed"
				end try
				try
					set mymail to make new outgoing message at the beginning of outgoing messages with properties {subject:"This is my Subject"}
					say "four ok"
				on error
					tell me to say "four failed"
				end try
				try
					tell mymail
						make new to recipient at beginning of to recipients with properties {address:replyAddress, name:replyName}
						set content to "This is my content Now"
						tell me to say "five ok"
					end tell
				on error
					tell me to say "five failed"
				end try
				try
					--show message window	
					set visible of mymail to true
					tell me to say "six ok"
				on error
					tell me to say "six failed"
				end try
				--try
				activate
				-- send mymail -- uncomment to send the message				
				-- tell me to say "seven ok"
				--on error
				--tell me to say "seven failed"
				--end try
			end repeat
		end tell
		beep 5
	end perform mail action with messages
end using terms from

I have been thinking outside the box and worked on a script that is applying the rules and make your script work when run separately say each hour. It is better than having to do it manually. The script will be loaded at login and idle forever. Each hour it will execute. Sorry I have not included that code but I have done it before and the timer code is not difficult (when the minutes of the current time get to 59 it goes). I post it as it might be useful to someone:


-- stay open and idle, when the minutes of current time is 59 (or whatever other value 0 to 60) then run the code below:
tell application "Mail"
	activate
	set selected mailboxes of message viewers to {inbox}
	tell application "System Events"
		tell process "Mail"
			tell menu bar 1
				tell menu bar item "Message"
					tell menu "Message"
						click menu item "Apply Rules"
					end tell
				end tell
			end tell
		end tell
	end tell
end tell
delay 0.5 -- may be not necessary

I’m not sure exactly what’s failing, but this script is working for me. Sorry the syntax is a little different from yours, I just started fresh. Instead of lots of “say” commands and beeps, I just sent all the progress and error into to the system log. If you open Console.app it’ll show up there.

property logPath : "/users/nikft/desktop/maillogger.txt"

using terms from application "Mail"
	
	
	on run
		my logEvent("Running")
		tell application "Mail"
			set theMessages to selection
			my replyToMessages(theMessages)
		end tell
	end run
	
	on perform mail action with messages theMessages for rule theRule
		my logEvent("Executing Rule")
		try
			my replyToMessages(theMessages)
		on error errMsg
			my logEvent(errMsg)
		end try
	end perform mail action with messages
	
	
	
	on replyToMessages(theMessages)
		
		-- Set some defaults in case we fail downstream
		
		set rta to "nobody@nowhere.nu"
		set rtn to "Testy McTesterson"
		
		tell application "Mail"
			repeat with aMessage in theMessages
				set rt to reply to of aMessage
				try
					set rta to extract address from reply to of aMessage
					my logEvent("Extracted address: " & rta)
				on error errMsg
					my logEvent("Error extracting address: " & errMsg)
				end try
				
				try
					set rtn to extract name from reply to of aMessage
					my logEvent("Extracted name: " & rtn)
				on error errMsg
					my logEvent("Error extracting name: " & errMsg)
				end try
				
				try
					set newMessage to make new outgoing message with properties {subject:"Autoreply from rule."}
					my logEvent("Created Message" & (subject of newMessage))
				on error errMsg
					my logEvent("Error creating email: " & errMsg)
				end try
				
				try
					tell newMessage
						make new to recipient with properties {address:rta, name:rtn}
					end tell
					
					my logEvent("Added Recipient:" & (rta & "/" & rtn))
				on error errMsg
					my logEvent("Error adding recipient: " & errMsg)
				end try
				
				try
					tell newMessage to set visible to true
					my logEvent("Message is visible.")
				on error errMsg
					my logEvent("Error making visible: " & errMsg)
				end try
				
			end repeat
			
		end tell
		
	end replyToMessages
	
end using terms from

on logEvent(s)
	do shell script "logger " & quoted form of ("Mail rule event: " & s)
end logEvent

I think I spoke too soon. The rule worked when triggered by the contextual menu, but failed after that. Console output included these delightful messages:

What this means is that the reply-to, sender, and headers objects are unavailable during the rule’s execution. Messages are only partially indexed at this point, so that’s probably the cause.

As a work-around, you might want to try assigning a category/color to messages that meet your criteria. Then you could have a scheduled event run a script on only those messages. That scheduled event could run quite a bit more frequently, and wouldn’t require UI scripting.

Interesting. I think it is having the same issue as this script:

http://macscripter.net/viewtopic.php?id=40407

Apparently Mail doesn’t apply an ID attribute nor do any other sorts of useful indexing until after the message hits the mailbox – i.e. after mail actions have completed. This, unfortunately, also eliminates any possibility of working around the problem by using the Spotlight metadata cache or any other ways into the mailbox.

And so I am forced to repeat:

Shame, too. There was a gleam of hope there… Mail’s AppleScript support is, at best, half-baked. It’s more a matter of working around bugs than anything else.

I’ve had fairly good luck with Sparrow for scripting email responses, and I believe PostBox works pretty well, too. May want to try that if this is a temporary challenge. Or use the categorize/re-process approach I suggested. That should work well enough.

And I am forced to repeat … Try this:
:wink:


using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		try
			tell application "Mail"
				repeat with aMessage in theMessages
					set aMessage to (contents of aMessage)
					-- A few extra variable in case you need them
					set {senderAddress, dateReceived, mID, mMbox, mAccount, mContent} to {extract address from sender, date received, message id, mailbox's name, mailbox's account's name, content} of aMessage
					set replyTo to my mailIsSquirrely(mMbox, mAccount, mID)
					set replyAddress to extract address from replyTo
					set replyName to extract name from replyTo
					set mymail to make new outgoing message at the beginning of outgoing messages with properties {subject:"This is my Subject"}
					tell mymail
						make new to recipient at beginning of to recipients with properties {address:replyAddress, name:replyName}
						set content to "This is my content"
					end tell
					--show message window    
					set visible of mymail to true
					--send mymail -- uncomment to send the message                
				end repeat
			end tell
			
		on error errMsg number errNum
			tell application "SystemUIServer"
				activate
				display alert errMsg & return & return & "Error number" & errNum buttons "Cancel" cancel button "Cancel"
			end tell
		end try
		
	end perform mail action with messages
end using terms from


on mailIsSquirrely(mMbox, mAccount, mID)
	do shell script "osascript -e 'tell app \"Mail\" to get reply to of first message of mailbox \"" & mMbox & "\" of account \"" & mAccount & "\" whose message id = \"" & mID & "\"'"
end mailIsSquirrely

Hi adayzdone,

Bravo!!!

Thanks, excellent, it works and you rule!!!

Don’t you mean adayzdone?

Yes Nigel,

Sorry about that, slip of the pen.

I meant thank you adayzdone!!!

Thanks for pointing it out and I corrected it above.

That’s extremely clever. Bravo!

I have exactly the same problem as the OP with Paypal and have been trying to create an AppleScript workaround.

The script posted by adayzdone (2013-05-16 08:43:25 pm) works perfectly if I select a message and then use the “Apply Rules” command, but if I set the filter to run automatically, I get a script editor error that reads:

[i][b]Unrecognized direct parameter type, please specify a string like “John Doe <>”

Error number-1703
[/b][/i]
Can anyone help?

Thank you in advance!

AppleScript: 2.5.1
Browser: Safari 536.30.1
Operating System: Mac OS X (10.8)