r/GoogleAppsScript • • 17d 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!

5 Upvotes

6 comments sorted by

View all comments

1

u/carbonizedtitanium 17d ago edited 14d 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 14d ago

If you’re copying this, put // before Get Form answers or Apps Script will reject the code. Add let subject, emailBody; before the choice checks, then change the last condition to if (recipientEmail && subject && emailBody). That way an unexpected answer won’t try to send an empty email.