use Mail::Sendmail; @givers = qw( Ingrid Jonathan Carin Jess Sam Jack Margit ); @recipients = @givers; %relations = ( "Ingrid", "Jonathan", "Jonathan", "Ingrid", "Carin", "Jess", "Jess", "Carin", "Sam", "XXXXXXX", "Jack", "Margit", "Margit", "Jack" ); @temp=@recipients; $success = 0; while ( $success == 0 ) { print("\n"); print(" Make an attempt.\n"); @givers= &randomizeArray(@givers); $success = &checkResults; } &printResults; sub checkResults { @temp=@recipients; for ( $idx = 0; $idx<@givers; $idx++) { $giver = $givers[$idx]; $recipient = pop(@temp); print( $giver, " gives to ", $recipient, ".\n"); if ( $giver eq $recipient ) { print ( " Error 01 - giver and recipient are the same!\n" ); return 0; } $relative=$relations{$giver}; #print( "giver: ", $giver, " relative: ", $relative, " rece: ", $recipient, ".\n"); #print( $giver, " gives to ", $recipient, " relative: ", $relative, ".\n"); if ( $relative eq $recipient ) { print ( " Error 02 - giver and recipient are related!\n" ); return 0; } } return 1; } sub randomizeArray { srand; my @inputArray = @_; @randomArray = (); while (@inputArray) { push(@randomArray, splice(@inputArray, rand @inputArray, 1)); } return @randomArray; } sub printResults { @temp=@recipients; for ( $idx = 0; $idx<@givers; $idx++) { $giver = $givers[$idx]; $recipient = pop(@temp); print( "email giver, ", $giver, ", to give to ", $recipient, ".\n"); # ======== mail =================================== print ("1" ); unshift @{$Mail::Sendmail::mailcfg{'smtp'}} , 'mail.adelphia.net'; print ("2" ); $mailto=name2email( $giver ); %mail = ( To => '$mailto', CC => 'jess@jesslilly.com', From => 'jess@jesslilly.com', Subject => 'Secret Santa 1', Message => "Hello $giver. This is the Secret Santa Program which randomly chose a person for you. Since this is the first time the program has been used, it has to be validated. That's why Jess is on the CC line. I have to make sure it works right. Anyway, your secret santa is below: (scroll down)\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nalmost there\n\n\n\n\n\n\\n\n\n\n\nn\n\n\n\n $recipient " ); print ("3" ); sendmail(%mail) or die $Mail::Sendmail::error; print ("4" ); print "OK. Log says:\n", $Mail::Sendmail::log; } } sub name2email() { $name=$_; $address="jess@jesslilly.com"; if ( $name eq "Jack" ) { $address="jackandmargit@svcable.netm"; } else if ( $name eq "Margit") { $address="jackandmargit@svcable.net"; } else if ( $name eq "Ingrid") { $address="ililly@LearnLink.Emory.Edu"; } else if ( $name eq "Jonathan") { $address="jmcmenamin-balano@ncc.commnet.edu"; } else if ( $name eq "Carin") { $address="carinlilly@hotmail.com"; } else if ( $name eq "Jess") { $address="jess@jesslilly.com"; } else if ( $name eq "Sam") { $address="whammywonderpants@hotmail.com"; } return $address; } # Assign givers to receivers. #foreach $giver ( @givers ) { # # print("Working on ", $giver, "...\n"); # # foreach $recipient ( @recipients ) { # # print( " ", $giver, " gives to ", $recipient, ".\n"); # } #} # #for ( $idx = 0; $idx<@givers; $idx++) { # for ( $jdx = 0; $jdx<@givers; $jdx++) { # print( " ", $givers[$idx], " gives to ", $recipients[$jdx], ".\n"); # } #}