Skip to content

Instantly share code, notes, and snippets.

@uwemneku
Last active April 25, 2021 08:48
Show Gist options
  • Save uwemneku/e13ab2668881800903ae57946165c392 to your computer and use it in GitHub Desktop.
Save uwemneku/e13ab2668881800903ae57946165c392 to your computer and use it in GitHub Desktop.
How to send custom emails from a spreadsheet using google apps script.
function createDocument(name, email, position, status){
//get the email template from google doc using the url of the file
const templateUrl = 'https://docs.google.com/document/d/1sRHXLlb3nsaiGVodhPNdt0SeYE1Gy-UnB4HQ1r2Wn-8/edit'
//Get that template
const template = DocumentApp.openByUrl(templateUrl)
// Extract the text in the body of the documemt
const text = template.getBody().getText()
//Replace all placeholders
const emailBody = text.replace('#Name', name)
.replace('#position', position)
.replace('#status', status)
MailApp.sendEmail(
email,
'Response to Job Application',
emailBody
)
}
function sendEmails() {
//get the spreadsheet with the emails
const emialSpreadsheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
//get all data inside the spreadsheet
const data = emialSpreadsheet.getDataRange().getValues()
//Iteriate through the data
data.forEach((applicant, index) => {
if (index > 0) //Skips the first array which is the header
{
createDocument(...applicant)
}
})
SpreadsheetApp.getUi().alert('ALL Emails have been sent') //Show an alert on google sheet that the mails have been sent.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment