Skip to content

Instantly share code, notes, and snippets.

@JayGoldberg
Last active January 18, 2025 18:55
Show Gist options
  • Save JayGoldberg/3d11c5219d9d3c8186648047315a171f to your computer and use it in GitHub Desktop.
Save JayGoldberg/3d11c5219d9d3c8186648047315a171f to your computer and use it in GitHub Desktop.
Apps Script email Google form responses automatically, with the responses inside the email

When someone submits a response to a Google Form, you get an email telling you that someone submitted a response but not what they actually submitted

That suuuucks.

So this project is meant to send you the actual response the person entered, and if you capture their email, the Reply feature in your email client will go back to the respondent.

  1. Insert this code into the Script editor of the Google Form.
  2. Copy the template into an HTML file in the script editor
  3. Install a From form - On form submit trigger to call sendFormResponseByEmail() when someone submits the form.
function sendFormResponseByEmail(event) {
let destinationEmail = "[email protected]";
let brand = "Brand name";
let formName = "Volunteer";
let response = event.response;
let emailFriendlyName = `${brand} Website`;
let emailBodyHeadline = `${formName}:`;
let textBody = '';
let itemResponses = response.getItemResponses();
let emailSubject = `${brand} ${formName} response`;
let respondentEmail = "[email protected]";
respondentEmail = response.getRespondentEmail(); // necessary if "collect emails is used vs an email input field", otherwise we should change this to a loop to locate the email response field and use that
for (let response of itemResponses) { // iterate through form response fields
if (response.getItem().getTitle() == "Name") {
emailSubject = `${brand} ${formName} - ${response.getResponse()}`
}
}
// Build the email body
let htmlTemplate = HtmlService.createTemplateFromFile("mail"); // build from the HTML template
htmlTemplate.response = response;
htmlTemplate.emailBodyHeadline = emailBodyHeadline;
let htmlOutput = htmlTemplate.evaluate().getContent();
// Send the email
GmailApp.sendEmail(destinationEmail, emailSubject, textBody, {
name: emailFriendlyName,
htmlBody: htmlOutput,
replyTo: respondentEmail
});
// MailApp.sendEmail({
// to: destinationEmail,
// cc: respondentEmail,
// subject: emailSubject,
// replyTo: respondentEmail,
// htmlBody: htmlOutput,
// name: emailFriendlyName
// });
// send to more than one person
// let emails = ["[email protected]", "[email protected]", "[email protected]"];
// for (let email of emails) {
// GmailApp.sendEmail(email, subject, message);
// }
}
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<h2>
<?= emailBodyHeadline; ?>
</h2>
<p><b>Email<br><?= response.getRespondentEmail(); ?></p>
<? let itemResponses = response.getItemResponses(); for (let i = 0; i < itemResponses.length; i++) { let item = itemResponses[i]; ?>
<p><b><?= item.getItem().getTitle(); ?></b><br>
<?= item.getResponse(); ?>
</p>
<? } ?>
<p>
<span style="color:LightCoral;"><i>Replying to this email will go back to the person who submitted this form. It will reveal your email address and the email thread!</i></span>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment