Skip to content

Instantly share code, notes, and snippets.

@crosson
Forked from anonymous/delay_mail.rb
Last active December 10, 2015 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crosson/4470417 to your computer and use it in GitHub Desktop.
Save crosson/4470417 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
CURRENT_TIME = ((Time.now - 150)..(Time.now + 300))
def draft_tag(draft)
draft.subject.match(/\[SEND@\d+:\d+\]/)[0]
end
def send_now?(draft)
tag = draft_tag(draft)
hour, minute = tag.scan(/\d+/)
time = Time.local(YEAR, MONTH, DAY, hour, minute)
CURRENT_TIME.cover?(time) && draft.recipients.length > 0
end
def build_new_draft(draft, mail = @mail)
props = {}
props['subject'] = draft.subject.gsub(draft_tag(draft), '')
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)
outgoing_msg
end
@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|
if send_now?(draft)
outgoing_msg = build_new_draft(draft)
if outgoing_msg.send
draft.delete
end
end
end
end
@crosson
Copy link
Author

crosson commented Jan 6, 2013

For some reason I cannot set the indentation to 2.

@kotp
Copy link

kotp commented Jan 7, 2013

For your gist or your editor? It almost looks like your editor is using tabs, though I can't fully confirm this at the moment. I will clone this down to get a closer look. How about scheduling a community event to look at this together? Not necessarily with me but a few people in the community might like to do so.

@crosson
Copy link
Author

crosson commented Jan 7, 2013

Yea it was my editor. I would love to do a community event.

@crosson
Copy link
Author

crosson commented Jan 7, 2013

Hmm. Even pasting it from textmate the tabs look odd.

@kotp
Copy link

kotp commented Jan 7, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment