Skip to content

Instantly share code, notes, and snippets.

@indyone
Last active November 22, 2021 10:24
Show Gist options
  • Save indyone/78052134bbf7748f318c2c65b7c7f0c2 to your computer and use it in GitHub Desktop.
Save indyone/78052134bbf7748f318c2c65b7c7f0c2 to your computer and use it in GitHub Desktop.
SprinklesEverywhere

This code snippet with help you send e-mail messages to the candidates (aka the Santas) with instructions where to send their "secret" gift.

using CsvHelper;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
namespace SprinklesEverywhere
{
class Program
{
static void Main(string[] args)
{
var candidates = new CsvReader(new StreamReader("SecretSanta.csv"), CultureInfo.InvariantCulture)
.GetRecords<dynamic>().ToArray();
var colorfulSprinkles = new Random();
var magicNumbers = Enumerable.Range(0, candidates.Length).OrderBy(_ => colorfulSprinkles.Next()).ToArray();
var messengerElf = new SmtpClient("smtp.gmail.com") { Port = 587, Credentials = new NetworkCredential("xxx@gmail.com", "xxx"), EnableSsl = true };
for (var i = 0; i < magicNumbers.Length; i++)
{
IDictionary<string, object> santa = candidates[magicNumbers[i]];
IDictionary<string, object> child = i + 1 == magicNumbers.Length ? candidates[magicNumbers[0]] : candidates[magicNumbers[i + 1]];
var from = $"{child["Name"]} <xxx@gmail.com>";
var recipients = $"NorthPole <{santa["Username"]}>";
var subject = "Letter to Santa";
var body = "Dear Santa,\n\n" +
"I hope that you, Mrs Santa and all your elves at the North Pole are doing well!\n" +
"How is Rudolph and the other eight reindeer's? They must be getting very excited that Christmas Eve is not that far away and that they will soon be helping you to deliver presents to excited children all around the world like myself.\n\n" +
"I am writing to let you know that I have tried very hard to be good all year and so I hope that you will have a Christmas present for me on your sleigh. " +
"I understand that you cannot always bring us exactly what we would like but I have written a few things below that are on my wish list this year just in case you have them on your toy factory...\n\n" +
$"{child["Interests"].Or("Anything!")}\n\n" +
"I will be listening for sleigh bells on Christmas Eve and I will also leave a snack and a drink for you and your reindeer, so please make sure that you come to...\n\n" +
$"{child["Name"]}\n{child["Address 1"]}{child[""]}\n{child["Address 2"].IfEmpty("", child["Address 2"] + "\n")}" +
$"{child["City"]} {child["Postal Code"]}\n{child["Country"]}\n\n" +
$"Happy Christmas and thank you,\n{child["Name"]}";
if (args.Any(a => a == "-ForReal"))
{
recipients = $"{santa["Name"]} <g.stavrinos+santa@kaizengaming.com>";
messengerElf.Send(from, recipients, subject, body);
Console.Write(".");
}
else if (args.Any(a => a == "-ForRealReal"))
{
messengerElf.Send(from, recipients, subject, body);
Console.Write(".");
}
else
{
Console.WriteLine($"From: {from}\nTo: {recipients}\nSubject: {subject}\n\n{body}\n\n---\n");
}
}
}
}
internal static class HelperElf
{
public static string IfEmpty(this object checkValue, string thisValue, string otherValue)
{
return string.IsNullOrWhiteSpace(checkValue.ToString()) ? thisValue : otherValue;
}
public static string Or(this object thisValue, string otherValue)
{
return string.IsNullOrWhiteSpace(thisValue.ToString()) ? otherValue : thisValue.ToString();
}
}
}
Timestamp Username Name Address 1 Address 2 City Postal Code Country Interests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment