Applescript to copy mail from one account to another

I am not great with AppleScript and I was after a script that would automatically copy mail for any new Sent Items in Account 1 to Sent items in Account 2. Is someone able to help me? Preferably it should work in Sierra (but I’d take El Capitan!).

Hey There,

There’s no way to automatically do this, because Mail’s rules do not accommodate outgoing mail.

The commercial plugin Mail Act-On adds outgoing rules to Mail.

-Chris

I already have Keyboard Maestro and was hoping to just set a rule to run an AppleScript periodically.

How many emails do you have in the mailbox you’re copying from?

Mail doesn’t work efficiently with whose clauses in large mailboxes.

For instance


set theDate to "5/1/2016"
set theDate to date theDate

tell application "Mail"
	set sentMailMailbox to mailbox "Sent" of account 3 -- 14168 Messages in the mailbox.
	set sentMessageList to messages of sentMailMailbox whose date received > theDate
end tell

This script takes 1.55 minutes to run on my system on that big mailbox.

By contrast “ running on a mailbox with only 3000 items takes about 1.5 seconds.

If I was doing this I’d just use a Mail Act-On outgoing mail rule “ that would be bombproof and automatic.

If I couldn’t use Mail Act-On I’d probably use a smart-mailbox to list mail in my Account-A sentmail-mailbox that was in the last 7 days and NOT flagged (some color) “ then I’d periodically flag it and copy it to Account-B sentmail-mailbox. That would be pretty easy to do by hand, although I’d probably figure out a way to script it or use a Keyboard Maestro macro.

-Chris

Thanks for that. The reason I didn’t want to use Mail Act On is that this will only move messages I send from my computer not those from other devices. Then it would be hard manually to know which ones to copy that weren’t sent from the computer. I like the smart folder idea. I’ll give that a shot as my mailbox is close to 80,000 so sounds like that will take too long!


tell application "Mail"
	
	-- I have "Sent" mailbox in this my iCloud account. It has messages.
	set sourceMailBox to mailbox "Sent" of account 2
	-- I created then mailbox "Sent" in this my Gmail account
	set destinationMailBox to mailbox "[Gmail]/Σημαντικά/Sent" of account 1
	
	with timeout of 600 seconds
		duplicate (every message of sourceMailBox whose date received < (current date) - 7 * days) to destinationMailBox
	end timeout
	
end tell

-- Result: sucessfully copied 2 messages