Skip to content

Instantly share code, notes, and snippets.

@kcmckell
Last active December 22, 2015 12:28
Show Gist options
  • Save kcmckell/6472225 to your computer and use it in GitHub Desktop.
Save kcmckell/6472225 to your computer and use it in GitHub Desktop.
Multi-purpose confirmation email sender. Connected to the spreadsheet that collects form responses, it is triggered on form submit. It parses the registration data, and then sends out several emails: 1. Confirmation to the registering player. 2. A copy of that registration to the webmaster (to serve as data backup). 3. A notification to the volu…
function onFormSubmit(e) {
// Log me like a hurricane.
var len = e.values.length;
for (var i = 0; i<len; i++){Logger.log(i + "<==>" + e.values[i]);};
// Gather form info into JavaScript variables.
var whoAreYou = e.values[28];
var Player;
var guestArray = new Array;
if (whoAreYou === 'Player') {
// Register a Player.
Player = {
team: e.values[1],
name: {
first: e.values[2],
last: e.values[3]
},
gender: e.values[4],
email: e.values[5],
phone: e.values[6],
emergency: {
name: e.values[7],
phone: e.values[8]
},
food: e.values[9],
camping: e.values[10],
hoputeer: e.values[11],
paymentmethod: e.values[12],
survey: {
throwing: e.values[13],
athletic: e.values[14],
knowledge: e.values[15],
experience: e.values[16]
},
shirt: e.values[17],
waiver: e.values[18],
};
if (e.values[19] === 'Yes') {
// The Player would like to register a guest.
guestArray = [registerGuest1(e,20)];
// Check if the Player wants to register a second guest.
if (e.values[29].indexOf("Yes") >= 0) guestArray = guestArray.concat(registerGuest2(e,30));
};
} else {
// Register a Guest only.
guestArray = [registerGuest1(e,20)];
// Check if the Guest wants to register a second guest.
if (e.values[29].indexOf("Yes") >= 0) guestArray = guestArray.concat(registerGuest2(e,30));
}
var timestamp = e.values[0];
var timearray = timestamp.split(" ").map(function(el){return el.split(":");});
var paymentmethod = e.values[12];
// Conditional text strings.
var paymentprompt = {
"PayPal" : "Please proceed to the [online payment page](http://hopukalewa.com/individual_pay.php) to complete your registration.",
"Check" : "Please send a check for the appropriate amount ($120 for players until 10/1 or $145 until 10/29 and $80 for weekend guests) payable to -*Hopu Ka Lewa*- to:\nAri Patz \n1777 Ala Moana Blvd Suite 142-39 \nHonolulu, HI 96815",
"teambid" : "You've indicated that your fee was included with the team bid. We'll check with your team captain and let you know if there are any discrepancies. If you need to pay for any guests, please proceed to the [online payment page](http://hopukalewa.com/individual_pay.php) to complete their registration.",
"Cash" : "Please be sure to bring $170 per player and $80 per weekend guest with you to the fields when you arrive. Checks and credit cards -*will not be accepted*- at the fields.",
"HULAship" : "Congratulations on your HULAship! If there is any balance due, please bring it to any committee member as soon as you can. Mahalo! If you need to pay for any guests, please proceed to the [online payment page](http://hopukalewa.com/individual_pay.php) to complete their registration."
};
// Generate email.
var subject = "Hopu Ka Lewa 15 registration confirmation";
var body = "Thank you for your registration on " + timearray[0] + " at " + timearray[1][0] + ":" + timearray[1][1] + ".\n\n";
if (paymentprompt.hasOwnProperty(paymentmethod)) { body += paymentprompt[paymentmethod]; }
else if (paymentmethod.indexOf("team bid") >= 0) { body += paymentprompt["teambid"]; }
else if (paymentmethod.indexOf("Cash") >= 0) { body += paymentprompt["Cash"]; }
else if (paymentmethod.indexOf("HULAship") >= 0) { body += paymentprompt["HULAship"]; }
body += "\n\n";
body += "We received the following registration information. If there are any errors, please contact the [Tournament Directors](mailto:hoputd@hawaiiultimate.com).\n";
if (Player) {
body += "\n-*Player Info*-";
body += "\nName: " + Player.name.first + " " + Player.name.last;
body += "\nTeam: " + Player.team;
body += "\nGender: " + Player.gender;
body += "\nEmail: " + Player.email;
body += "\nPhone: " + Player.phone;
body += "\nEmergency contact: " + Player.emergency.name + " at " + Player.emergency.phone;
body += "\nFood preference: " + Player.food;
body += "\nCamping preference: " + Player.camping;
body += "\nHoputeer preference: " + Player.hoputeer;
body += "\nShirt size: " + Player.shirt;
};
for (var g = 0; g<guestArray.length; g++) {
body += "\n\n-*Guest" + (guestArray.length == 1 ? " " : " " + (g+1) + " ") + "Info*-";
body += "\nName: " + guestArray[g].name.first + " " + guestArray[g].name.last;
body += "\nGender: " + guestArray[g].gender;
body += "\nEmail: " + guestArray[g].email;
body += "\nPhone: " + guestArray[g].phone;
body += "\nEmergency contact: " + guestArray[g].emergency.name + " at " + guestArray[g].emergency.phone;
body += "\nFood preference: " + guestArray[g].food;
};
body += "\n\nAgain, please follow through with your payment method of choice as soon as possible.";
body += "\n\nWe're looking forward to seeing you in Waimanalo!";
body += "\n\nAloha,\nAaron and Stacey, 2013 Hopu Ka Lewa Tournament Directors";
// From the plaintext email body, make a pretty HTML email body.
var htmlbody = body.replace("\n\n","<p>");
htmlbody = htmlbody.replace("\n","<br>");
htmlbody = htmlbody.replace("-*","<strong>");
htmlbody = htmlbody.replace("*-","</strong>");
// Sweet REGEX for Markdown-to-HTML from here: https://gist.github.com/jbroadway/2836900 (adapted from PHP to JavaScript).
htmlbody = htmlbody.replace(/\[([^\[]+)\]\(([^\)]+)\)/,'<a href=\"$2\">$1</a>');
// Send the email.
var optAdvancedArgs = {
name : "Hopu Ka Lewa 15",
// Can't get Gmail to render HTML email well, so scrap it for now.
//htmlBody : htmlbody,
replyTo: "hoputd@hawaiiultimate.com"
};
// Send email to Player, or, if none, the first Guest registered.
var recipientEmail = Player ? Player.email : guestArray[0].email;
MailApp.sendEmail(recipientEmail, subject, body, optAdvancedArgs);
// Send backup email to the webmaster.
var webmasterSubject = "Hopu 15 Registration for " + (Player ? Player.name.first + " " + Player.name.last : guestArray[0].name.first + " " + guestArray[0].name.last);
MailApp.sendEmail("webmaster@hawaiiultimate.com", webmasterSubject, body, optAdvancedArgs);
// Send notice to Volunteer Coordinator if there is a volunteer.
var VCemail = "armytruff@yahoo.com";
var VCsub = "Hopu 15 Volunteer Data";
var hoputeer;
var maybeteer;
var VCbody;
if (Player && ((hoputeer=Player.hoputeer.indexOf("Yes")) >= 0 | (maybeteer=Player.hoputeer.indexOf("Maybe")) >= 0 )) {
VCbody = Player.name.first + " " + Player.name.last + " ";
VCbody += (hoputeer >= 0 ? "wants to be a Hoputeer" : "needs to be convinced") + ".";
VCbody += "\n\nTheir contact info is:\n";
VCbody += Player.email;
VCbody += "\n";
VCbody += Player.phone;
VCbody += "\n\n" + (Player.gender.indexOf("Male")>=0 ? "He" : "She") + " is playing for " + Player.team + ".";
VCbody += "\n\nParty on Wayne.";
Logger.log(VCbody);
MailApp.sendEmail(VCemail, VCsub, VCbody, optAdvancedArgs);
};
}
function registerGuest1(d, startindex) {
// Return a Guest object with associated data key/values.
var s = startindex;
var Guest = {
name: {
first: d.values[s+0],
last: d.values[s+1]
},
gender: d.values[s+2],
email: d.values[s+3],
phone: d.values[s+4],
emergency: {
name: d.values[s+5],
phone: d.values[s+6]
},
food: d.values[s+7]
};
return Guest;
}
function registerGuest2(d, startindex) {
// For some reason, the order of the fields is slightly different for the second guest registration form.
// Note the switched order in emergency.phone and food keys.
var s = startindex;
var Guest = {
name: {
first: d.values[s+0],
last: d.values[s+1]
},
gender: d.values[s+2],
email: d.values[s+3],
phone: d.values[s+4],
emergency: {
name: d.values[s+5],
phone: d.values[s+7]
},
food: d.values[s+6]
};
return Guest;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment