An AppleScript to batch-redirect email in Mail.app

Apr 27, '04 10:34:00AM

Contributed by: Apollo18Pnut

I've been trying to come up with a way to dump a bunch of email from a few of my email accounts to my new Gmail beta account. The problem is, I can't just forward the emails, because then the original headers (specifically the From address) get lost. Solution: the Redirect command. But Mail.app doesn't let you use Redirect on more than one email at a time! Hence, this AppleScript.

Obviously, replace firstname.lastname@gmail.com with the intended recipient of your redirected emails, and name with the full text of what you want to appear in the "Account" dropdown (i.e., the account that you want to use to send the redirected emails).

using terms from application "Mail"
  on perform mail action with messages selectedMessages
    set AppleScript's text item delimiters to ""
    set theRedirectRecipient to "firstname.lastname@gmail.com"
    set theRedirectSender to "name <name@fastmail.fm>"
    repeat with eachMessage in selectedMessages
      tell application "Mail"
        (* Uncomment this block and the "end if" below
        to ask if it's ok to forward each email
        set theSubject to the subject of eachMessage
        set theResult to display dialog "Redirect " & theSubject & ¬
        " to Gmail?" buttons {"OK", "Cancel"} default button 1
        if button returned of theResult is equal to "OK" then
        *)
        set theRedirectedEmail to redirect eachMessage ¬
          with opening window
        tell theRedirectedEmail
          make new to recipient at beginning of to recipients ¬
            with properties {address:theRedirectRecipient}
        end tell
        set the sender of theRedirectedEmail to theRedirectSender
        send theRedirectedEmail
        (* Also uncomment this block
        end if
        *)
      end tell
    end repeat
  end perform mail action with messages
end using terms from
That should do it. Save the AppleScript in your user's Library -> Scripts -> Mail Scripts folder, and you're good to go.

Comments (13)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20040425063255443