0

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.

  • It might be more helpful if you'd include the actual error message(s) you're getting. – user3439894 Apr 9 '16 at 2:24
  • It complains at the "send" command is unknown – mrmrw Apr 10 '16 at 2:47
  • The send command acts on an outgoing message, which is a new email message, not one that has been saved to a mailbox. If you manually open, double-click, a message in the 9AM mailbox you'll notice two things, there is no Send message icon on the Toolbar and on the Message menu there is no Send command, only Send Again for which there is not a command in the Mail AppleScript Dictionary. – user3439894 Apr 10 '16 at 5:03
  • Is there please any way to migrate the message back to a send email? – mrmrw Apr 11 '16 at 10:25
  • I'd be equally happy to have whatever emails are inside my "Drafts" folder to send out at 9am – mrmrw Apr 11 '16 at 10:25
1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .