Skip to content

Instantly share code, notes, and snippets.

Last active May 12, 2023 07:49
What would you like to do?
Email automation App Script
Replace the "<DOCID>" with your document ID, or the entire URL per say. Should be something like:
var EMAIL_TEMPLATE_DOC_URL = '<DOCID>/edit?usp=sharing';
var EMAIL_SUBJECT = 'This is an important email';
* Sends a customized email for every response on a form.
* @param {Object} e - Form submit event
function onFormSubmit(e) {
var responses = e.namedValues;
// If the question title is a label, it can be accessed as an object field.
// If it has spaces or other characters, it can be accessed as a dictionary.
NOTE: One common issue people are facing is an error that says 'TypeError: Cannont read properties of undefined'
That usually means that your heading cell in the Google Sheet is something else than exactly 'Email address'.
The code is Case-Sesnsitive so this HAS TO BE exactly the same on line 25 and your Google Sheet.
var email = responses['Email address'][0].trim();
Logger.log('; responses=' + JSON.stringify(responses));
to: email,
htmlBody: createEmailBody(),
Logger.log('email sent to: ' + email);
// Append the status on the spreadsheet to the responses' row.
var sheet = SpreadsheetApp.getActiveSheet();
var row = sheet.getActiveRange().getRow();
var column = e.values.length + 1;
sheet.getRange(row, column).setValue('Email Sent');
* Creates email body and includes the links based on topic.
* @param {string} name - The recipient's name.
* @param {string[]} topics - List of topics to include in the email body.
* @return {string} - The email body as an HTML string.
function createEmailBody() {
// Make sure to update the emailTemplateDocId at the top.
var docId = DocumentApp.openByUrl(EMAIL_TEMPLATE_DOC_URL).getId();
var emailBody = docToHtml(docId);
return emailBody;
* Downloads a Google Doc as an HTML string.
* @param {string} docId - The ID of a Google Doc to fetch content from.
* @return {string} The Google Doc rendered as an HTML string.
function docToHtml(docId) {
// Downloads a Google Doc as an HTML string.
var url = '' +
docId + '&exportFormat=html';
var param = {
method: 'get',
headers: {'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()},
muteHttpExceptions: true,
return UrlFetchApp.fetch(url, param).getContentText();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment