Skip to content

Instantly share code, notes, and snippets.

Created January 6, 2013 20:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/4470113 to your computer and use it in GitHub Desktop.
Save anonymous/4470113 to your computer and use it in GitHub Desktop.
This was a script I wrote to send delayed emails from macmail. The script is run in a cron every 15 minutes The script checks for items in the Mail draft folder that have a [SEND@XYZ] tag. For example, [SEND@16:00] The script checks if a drafted emails SEND@ tag matches or is near the current time. If so a new email is drafted, See step 4. The n…
#!/usr/local/bin/macruby
framework 'ScriptingBridge'
ACCOUNT = "AccountName"
DAY = Time.now.day
MONTH = Time.now.month
YEAR = Time.now.year
time_range = ((Time.now - 150)..(Time.now + 300))
@mail = SBApplication.applicationWithBundleIdentifier("com.apple.mail")
if @mail.isRunning
my_account = @mail.accounts.select { |account| account.name == ACCOUNT }.first
drafts_folder = my_account.mailboxes.select { |mailbox| mailbox.name == "Drafts" }.first
delayed_messages = drafts_folder.messages.select { |x| x.subject.match(/\[SEND@\d\d:\d\d\]/) }
delayed_messages.each do |draft|
tag = draft.subject.match(/\[SEND@\d\d:\d\d\]/)[0]
hour, minute = tag.scan(/\d\d/)
time = Time.local(YEAR, MONTH, DAY, hour, minute)
if time_range.cover?(time) && draft.recipients.length > 0
props = {}
props['subject'] = draft.subject.gsub(tag, '')
props['sender'] = draft.sender
props['content'] = draft.content.get
outgoing_msg = @mail.classForScriptingClass('outgoing message').alloc.initWithProperties(props)
@mail.outgoingMessages.addObject(outgoing_msg)
recievers = ""
draft.recipients.each do |recip|
recievers << "%s," % recip.address
end
recipient = @mail.classForScriptingClass('to recipient').alloc.initWithProperties({'address'=>recievers})
outgoing_msg.toRecipients.addObject(recipient)
if outgoing_msg.send
draft.delete
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment