r/GoogleAppsScript • u/Standard_Guitar_1165 • 19d ago
Question Google Forms Email Response
Heya! I was wondering if it is possible to automatically send an email to a respondent depending on their answers on the from. So if they choose option a) they'll get email a. I've never done anything like this before so please let me know like I'm a child lol. I need this for an assignment - any help would be appreciated!
1
u/carbonizedtitanium 18d ago edited 16d ago
here's a sample:
function sendConditionalEmail(e) {
const namedValues = e.namedValues; //Get Form answers
const recipientEmail = namedValues["Email Address"][0]; // Replace "Email Address" with the exact header name of your email column
const userChoice = namedValues["What candy do you like?"][0]; // Replace with your exact form question text. 0 index is for the first choice
let subject, emailBody;
if (userChoice === "Snickers") {
subject = "Snicker sucks!"; //Replace with whatever subject you want
emailBody = `Hi,\n\nThank you for reaching out. We have received your submission.`; //Replace with your own text
} else if (userChoice === "Kitkat") {
subject = "You are with the cool team!";
emailBody = `Hi,\n\nThank you for reaching out. We have received your submission.`; //Replace with your own text
}
// send email
if (recipientEmail && subject && emailBody) {
MailApp.sendEmail(recipientEmail, subject, emailBody);
}
}after you save, you have to add Trigger with event type: On form submit
edit: thanks to u/NinjaConsultingLLC for corrections
1
u/NinjaConsultingLLC 16d ago
If you’re copying this, put
//beforeGet Form answersor Apps Script will reject the code. Addlet subject, emailBody;before the choice checks, then change the last condition toif (recipientEmail && subject && emailBody). That way an unexpected answer won’t try to send an empty email.
0
u/WicketTheQuerent 19d ago
What should the email include? Will the email subject and body be the same for all respondents?
How many responses do you expect to get per day?
1
u/Standard_Guitar_1165 19d ago
it’s for an assignment, so someone fills out a form to receive a review of a performance and fills out a form to determine which review you should receive via email
1
u/estadoux 19d ago
Yes. It’s possible as the onFormSubmit event gets the full response so you could use it to evaluate and decide which email to send. You could even use response values to populate the email.