#!/usr/bin/perl -w use strict; use List::Util qw(shuffle); use Digest::MD5 qw(md5_hex); use Net::SMTP::TLS; # Parameters my $authority = ''; my $value = 10; my $participantsFile = 'santaList.txt'; # Name#email\n my $previousFile = 'lastYear.txt'; # sorted md5 hashes\n my $serverInfoFile = 'emailCredentials.dat'; # Declare variables with file-wide scope my (%emails, @names, %matching, @previous, @content); # Initialize data open INPUT, '<' . $participantsFile; while () { chomp; my ($name,$email) = split '#'; $emails{$name} = $email; } close INPUT; open INPUT, '<' . $previousFile; while () { chomp; push @previous,$_; } close INPUT; # Generate the list @names = keys(%emails); # Collect the list of all participants GENERATE: while (1) { # Picking loop @matching{@names} = shuffle(@names); # Match 'em up # No one has themselves foreach (@names) { next GENERATE if $_ eq $matching{$_}; } # Generate digests for comparison my @digest = (); foreach (@names) { push @digest, md5_hex($_, '=>', $matching{$_}); } @digest = sort @digest; # Compare against previous year's results my $index = 0; foreach (@digest) { while ($index < @previous and $_ gt $previous[$index]) {$index++} last if $index == @previous; next GENERATE if $_ eq $previous[$index]; } #Passes - store the digests for next year open OUTPUT, '>' . $previousFile; print OUTPUT join "\n", @digest; close OUTPUT; last GENERATE; # Passes } # E-mail the results { open INPUT, '<' . $serverInfoFile; my $host = ; chomp $host; my $port = ; chomp $port; my $user = ; chomp $user; my $password = ; chomp $password; close INPUT; my $server = Net::SMTP::TLS->new( $host, Port => $port, User => $user, Password=> $password, ) or die "Can't open mail connection: $!"; # Independent auditor if ($authority) { @content = (); $content[0] = "The picks for this year\'s gifting are as follows\:"; foreach (@names) { push @content, "$_ is giving to $matching{$_}"; } send_mail($server, $authority, $user, 'Secret Santa Drawing', @content); } foreach (@names) { @content = (); $content[0] = "Dear $_\,"; $content[1] = ""; $content[2] = "Thank you for participating in this year\'s Secret"; $content[3] = "Santa gift exchange\. This year\, you will be giving"; $content[4] = "a gift to $matching{$_}\. Please remember to limit the"; $content[5] = "monetary value of any gift you give to \$$value\."; $content[6] = ""; $content[7] = "Happy Holidays\,"; $content[6] = "Robo-Santa 5000"; send_mail($server, $emails{$_}, $user, 'Secret Santa Drawing', @content); } $server->quit; } sub send_mail { my($server, $to, $from, $subject, @body) = @_; $server->mail($from); $server->to($to); $server->data(); $server->datasend("To: $to\n"); $server->datasend("From: $from\n"); $server->datasend("Subject: $subject\n"); $server->datasend("\n"); foreach (@body) { $server->datasend("$_\n"); } $server->dataend(); }