Skip to content

Instantly share code, notes, and snippets.

@AhsanAyaz
Last active April 1, 2024 17:53
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save AhsanAyaz/9e82b1297ffc2f1f89749ddd74d606a3 to your computer and use it in GitHub Desktop.
Save AhsanAyaz/9e82b1297ffc2f1f89749ddd74d606a3 to your computer and use it in GitHub Desktop.
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 = 'https://docs.google.com/document/d/asdasdakvJZasdasd3nR8kmbiphqlykM-zxcrasdasdad/edit?usp=sharing';
*/
var EMAIL_TEMPLATE_DOC_URL = 'https://docs.google.com/document/d/<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));
MailApp.sendEmail({
to: email,
subject: EMAIL_SUBJECT,
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 = 'https://docs.google.com/feeds/download/documents/export/Export?id=' +
docId + '&exportFormat=html';
var param = {
method: 'get',
headers: {'Authorization': 'Bearer ' + ScriptApp.getOAuthToken()},
muteHttpExceptions: true,
};
return UrlFetchApp.fetch(url, param).getContentText();
}
@afoeller85
Copy link

Hi, I'm trying to use this code, and I keep getting this error.

"TypeError: Cannot read properties of undefined (reading '0') at onFormSubmit(Code:25:41)"

Can you please assist? My Google Sheet column header matches line 25 of your code exactly as required.

Thanks!

@schmalberger
Copy link

Hi. I used your code but I encounter this error: TypeError: Cannot read properties of undefined (reading 'namedValues')
onFormSubmit.

Can you help repairing the code? Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment