Skip to content

Instantly share code, notes, and snippets.

@justinperkins
Created June 25, 2010 18:54
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 justinperkins/453279 to your computer and use it in GitHub Desktop.
Save justinperkins/453279 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#######################
# description:
# sends an email to one or more people on a certain day of the week, which rotates every week
# an email is sent on monday to advise your recipients which day of the week the taco day falls on
# then on the day of the taco day, a brief email will be sent to remind people once again
# use cron to schedule this script to be invoked on weekdays at whatever time you want the email to be sent
# here is an example: 0 11 * * 1-5 ~/bin/taco-mailer.rb
#######################
require 'date'
require 'net/smtp'
to = ['user1@domain.com', 'user2@domain.com']
from = 'taco-lover@domain.com'
baseline = Date.new(2009, 10, 8)
now = Date.today
def taco_specials_for_day(day)
[
nil,
"El Picosito (also available as a veggie taco)",
"Achiote Chicken",
"Shrimp Taco & Chili Relleno",
"Scallop Taco & Tacolomo",
"Ceviche Yucatan & Guacasabi Tuna"
][day]
end
def get_this_weeks_taco_day(now)
# b/c taco day does not start at the dawn of time, it starts on thursday
# the trailing plus one is to make the weekday numbers start at 1 instead of zero to match the ruby wday method result
# the first plus one is to move the start of the week from sunday to monday
taco_start = 3
((((now.jd + 1) / 7) + taco_start) % 5) + 1
end
def normal_email(baseline, now, taco_day)
taco_day_name = now.wday == taco_day ? 'Today' : Date::DAYNAMES[taco_day]
msg = <<END
Subject: This week's taco day is #{ taco_day_name }
Just wanted to remind you that according to our calculations*, #{ taco_day_name } is taco day.
END
msg << "\nThe specials for #{ taco_day_name }: #{taco_specials_for_day(taco_day) }\n"
msg << "\n- Your Friendly Automated Taco Reminder Task"
msg << "\n\n* This calculation is based on the taco baseline of #{ baseline.strftime('%a %b %d %Y')} which was established during the taco accords of 2009."
msg
end
def quickie_email(taco_day)
rotating_subjects = [
'You touch my taco, I break your face',
'Do not mess with taco day',
'Le taco mon amour',
'Take this taco and shove it. Into your mouth.',
'Pity the foo who touches my taco',
"If you want something, reach out and taco it",
"Not my taco, not my problem",
"I hear you tacoin' but I don't know what you're sayin'",
"Doña gonna get ya sucka"
]
msg = <<END
Subject: #{ rotating_subjects[rand(rotating_subjects.length)] }
Specials for today: #{ taco_specials_for_day(taco_day) }
END
msg << "\nOh and FYI, next Monday is also taco day. Wrap around FTW" if taco_day == 5
msg
end
def send_email(msg, to, from)
standard_fields =<<END
From: Scallop Taco <#{ from }>
To: #{ to.join(',') }
END
msg.insert(0, standard_fields)
Net::SMTP.start('smtp.server.com', 25, 'domain.com', 'taco-lover@domain.com', 'password', :login) do |smtp|
smtp.send_message(msg, from, to)
end
end
taco_day = get_this_weeks_taco_day(now)
send_email(normal_email(baseline, now, taco_day), to, from) if now.wday == 1
send_email(quickie_email(taco_day), to, from) if taco_day == now.wday && taco_day != 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment