-
-
Save RayanAbid/492624942c4f8481b9eeac35d7a492ec to your computer and use it in GitHub Desktop.
Email automation App Script ( send email with aliases script )
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
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. | |
var email = responses['Email address'][0].trim(); | |
Logger.log(' email =' + responses['Email address']); | |
Logger.log('; responses=' + JSON.stringify(responses)); | |
GmailApp.sendEmail(email, EMAIL_SUBJECT, createEmailBody(), { | |
htmlBody: createEmailBody(), | |
// bcc: '[email protected]', | |
// cc: '[email protected]', | |
// from: '[email protected]', // Enter your alias mail here | |
// name: 'name of the sender', | |
// replyTo: '[email protected]', | |
// noReply: true, // if the email should be sent from a generic no-reply email address (not available to gmail.com users) | |
// attachments: emailTemplate.attachments, | |
// inlineImages: emailTemplate.inlineImages | |
}); | |
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(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment