Use AppleScript to send all emails in a folder at a certain time

I'd very much like to please develop an AppleScript to send all emails inside a specific mailbox whenever it is run. Then I can create a mailbox called "9AM", place emails inside there, and with a little help from cron/iCal, have all those emails send at 9am each day allowing me to type emails late at night but have them send in the morning.

My code below however fails on the send command.

tell application "Mail"
    repeat with theMessage in (every message of (mailbox "9AM" of account "accountnamehere"))
        send theMessage
    end repeat
end tell

I also tried moving them to the "Outbox" mailbox but that just seems to make them disappear...

tell application "Mail"
    repeat with theMessage in (every message of (mailbox "9AM" of account "accountnamehere"))
        set mailbox of theMessage to mailbox "Outbox"
    end repeat
end tell

Any suggestions anyone? Help greatly appreciated!

This setup would be ideal as I can just leave a Mac switched on and then even save emails to this "9AM" mailbox from my iPhone. I hope it is possible.


This is not necessarily meant to be a complete answer and is too much as a comment. Because the Mail AppleScript Dictionary lacks some verbs, e.g. send again, one therefore needs to workaround any shortcomings.

The code below could be incorporated into your script. It uses System Events to manipulate menu commands to send composed messages from the designated mailbox.

Example Code:

tell application "Mail"
    activate
    repeat with theMessage in (every message of (mailbox "9AM"))
        open theMessage
        delay 1
        tell application "System Events" to click menu item "Send Again" of menu "Message" of menu bar 1 of process "Mail"
        delay 1
        tell application "System Events" to click menu item "Send" of menu "Message" of menu bar 1 of process "Mail"
        delay 1
    end repeat
end tell

What this does is automate the tasks that one could/would do manually to achieve the same results, sending the unsent saved messages from a given mailbox.

What this doesn't do is, if the mailbox is other then Drafts, remove the messages from the target mailbox and additional coding will be required.

Notes:

The problem with automating a series of manual tasks in this manner, aka. UI programming, is if at the designated time the script runs and you are doing something else and it take focus away from Mail then the script will not be able to properly complete. This is because if the Mail menu is not visible then the calls being made by System Events cannot be completed.

The value of the delay command may need to be adjusted, e.g. delay 1.25, if required or whatever value works on your system. Slower systems need higher delay values.

Obviously if the target mailbox is Drafts keep in mind that unless you code accordingly all messages in the target mailbox will be sent and therefore using e.g. "9AM" allows keeping drafts that aren't ready to send from being sent. The disadvantage of other then using Drafts is requiring additional coding to remove the messages that have now been sent via Send Again > Send in this manner.