Skip to content

Instantly share code, notes, and snippets.

@eeeps
Last active December 12, 2019 22:06
Show Gist options
  • Save eeeps/c3fb1df73dbdff31413efc295c9d3c0b to your computer and use it in GitHub Desktop.
Save eeeps/c3fb1df73dbdff31413efc295c9d3c0b to your computer and use it in GitHub Desktop.
Pairs people up and emails them with their pair
require 'net/smtp'
people = [ {
:name => "Eric Portis",
:email => "eric@cloudinary.com",
:address => "1812 Obstruction Pass Rd, Olga, WA 98279"
},
{
:name => "Mr Worldwide",
:email => "mrworldwide@pitbull.party",
:address => "Worldwide, Earth"
}
# etc...
]
givers = people.dup
getters = people.shuffle
pairs = []
# match givers to getters
while givers.length > 0
if givers.last != getters.last
pairs << { :giver => givers.pop, :getter => getters.pop }
elsif givers.length == 1 # shuffling will do no good! start over!
pairs = []
givers = people.dup
getters = people.shuffle
else
getters.shuffle!
end
end
# send emails
pairs.each do |pair|
giver_name = pair[:giver][:name]
getter_name = pair[:getter][:name]
giver_email = pair[:giver][:email]
getter_address = pair[:getter][:address]
message = <<MESSAGE_END
From: Eric Portis <eric.portis@gmail.com>
To: #{giver_name} <#{giver_email}>
Subject: #{giver_name}'s secret santa draw
#{giver_name}, you drew #{getter_name} out of the hat! Their address is #{getter_address}. Buy them a < $25 gift!
MESSAGE_END
smtp = Net::SMTP.new 'smtp.gmail.com', 587
smtp.enable_starttls
smtp.start("localhost", "eric.portis@gmail.com", "the-code-is-open-source-but-eric's-gmail-password-isn't!", :login) do
smtp.send_message(message, "eric.portis@gmail.com", giver_email)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment