Created
December 12, 2014 15:16
-
-
Save mikolajb/8cfb51ea6473ef9fbe2e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'mail' | |
=begin | |
Put participants in file INPUT_FILE in a format: | |
participant_name:participant_email | |
selection will be backed up in BACKUP_FILE. | |
=end | |
INPUT_FILE = "participants.txt" | |
BACKUP_FILE = "lottery.txt" | |
Mail.defaults do | |
delivery_method :smtp, { :address => "", | |
:port => 587, | |
:domain => '', | |
:user_name => '', | |
:password => '', | |
:authentication => '', | |
:enable_starttls_auto => true } | |
end | |
participants = File.read(INPUT_FILE).lines. | |
to_a.map(&:strip).reject(&:empty?).map { |i| i.split(':') } | |
pairs = {} | |
participants.shuffle! | |
participants.length.times do |i| | |
pairs[participants[i-1][0]] = participants[i][1] | |
end | |
backup = File.open(BACKUP_FILE, "w+") | |
pairs.each do |p1, p2| | |
backup.puts "#{p2}:#{p1}" | |
end | |
backup.close | |
pairs.each do |p1, p2| | |
puts "delivering message to #{p2}..." | |
message = Mail.new do | |
to p2 | |
from "" | |
subject "Secret Santa event" | |
body """Hi, | |
We are happy to inform you have been selected as Secret Santa for #{p1}. | |
Regards, | |
Secret Santa inc. | |
""" | |
end | |
message.deliver! | |
puts "message delivered" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment