Secret Santa PHP script

Secret Santa PHP script

Today I have knocked together a Secret Santa script in PHP.
It takes a list of names and email addresses and randomly assigns people then emails the Secret Santa with their person to buy for.

Feel free to use the script – and if you want to say thanks to me please click on an ad that interests you :)

Change the recipient list, message and you’re all set.

Permalink to the script: secretsanta.php

Merry Christmas
$santas = array(
array("Alexander 'Scrooge' User", "[email protected]"),
array("Barry 'Baubles' User", "[email protected]"),
array("Charles 'Tinsel' User", "[email protected]"),
array("Dominic 'Rudolph' User", "[email protected]"),
array("Ernest 'Humbug' User", "[email protected]"),
array("Ferdinand 'Elfie' User", "[email protected]"),
array("Georgina 'Santa's Little Helper' User", "[email protected]"),
array("Helena 'Born In A Barn' User", "[email protected]")
array("Isla 'Mulled Wine' User", "[email protected]")
$numsantas = count($santas);

$combos = array_fill(0, $numsantas, -1);

echo “There are $numsantas Secret Santas this year.\n”;

$fp = fopen(“santalist.txt”, “w”);
fwrite($fp, “Secret Santa Draw ” . date(“r”) . “\n\n”);

$x = -1;
foreach($santas as $name => $email)
$valid = false;
$r = rand(0, $numsantas – 1);
if($r != $x && $combos[$r] == -1)
$combos[$r] = $x;
echo $santas[$x][0] . ” will be buying for ” . $santas[$r][0] . “\n”;
$to = “\”” . $santas[$x][0] . “\” <” . $santas[$x][1] . “>”;
$subject = “Your Secret Santa”;
$headers = “From: \”Your Name, Secret Santa Organiser\” <[email protected]>\r\n” .
“Reply-To: \”Your Name, Secret Santa Organiser\” <[email protected]>\r\n” .
“Cc: \”Your Name, Secret Santa Organiser\” <[email protected]>\r\n” .
“X-Mailer: PHP/” . phpversion();
$message = “Hello ” . $santas[$x][0] . “\r\n” .
“In the random drawing of Your Organisation’s Secret Santa you have been selected to buy a gift for (insert drumroll sound effect here)\r\n” .
$santas[$r][0] . “\r\n” .
“As the aim of this is to be a Secret Santa please do not tell anyone who you have purchased a gift for.\r\n” .
“Please limit the value of the gift to be £1 maximum, and be creative – there will be a prize for the most appropriate gift for the recipient.\r\n” .
“(The cost of wrapping should not be deducted from the gift value – newspapers are free)\r\n”;
mail($to, $subject, $message, $headers);
fwrite($fp, $santas[$x][0] . ” will be buying for ” . $santas[$r][0] . “\n”);
$valid = true;

Related posts

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.