Google Apps Script Code to Send Automated Emails
Google Apps Script Code to Send Automated EmailsRate:


Google Apps Script Code to Send Automated Emails
Tags: Tools, HR Tools

One of the major tasks in any organization is to keep everyone informed, and the best way to do the same is by sending out Emails. Here is an example of how to create your automated email-sending tool to make your work a bit easier using Google Apps Script.

Automated tools can be of great help when you want to inform people in your organization. Here we are going to take a simple example of sending out Automated Emails for Salary Increments to employees of your organization. The uses of the same can be many, for example;

  1. To send Billing Invoices
  2. To send interview schedules
  3. To send out Employee Performance Reviews
  4. To send out information about any policy update
  5. To send out Holiday Greetings and Announcements
  6. To send Event Invitations and Reminders

To make this tool, ensure to have the following things;

  1. Gmail Account
  2. Access to Google Sheets
  3. Access to Google Apps Script

Follow the below steps to create your Email Sending Tool;

Step 1: Open Google Sheets
You can access the Google Sheets by simply typing: sheets.google.com or by accessing the same using your Gmail account.

Step 2: Name the Columns
For our assumption, we are going to name our columns as follows;

Step 3: Add Details of Employees
Now start adding details of employees as shown in the image below;

Story pin image


Step 4: Click on Extensions
As shown in the below image, click on "Extensions" on the top ribbon, and you should be able to see "Apps Script" written. Then click on the apps script and you will be able to open Apps Script Editor.

Story pin image


Step 5: Copy and Paste Below Code
Add the below code to your code editor and press the save button.

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Email Sender')
      .addItem('Send Emails', 'sendEmails')
      .addToUi();
}
function sendEmails() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var data = sheet.getDataRange().getValues();
  
  // Loop through each row of data, assuming first row is headers
  for (var i = 1; i < data.length; i++) {
    var name = data[i][0];
    var email = data[i][1];
    var template = data[i][2];
    var subject = data[i][3];
    var otherData = data[i][4];
 
    if (email && template && subject) {
      var message = template.replace("{name}", name).replace("{Other Data}", otherData);
      sendEmail(email, subject, message);
    }
  }
}
function sendEmail(email, subject, message) {
  MailApp.sendEmail(email, subject, message);
}


Step 6:Click on Save & Run
As shown in the below image, click on "Run" for the first time, and Google will ask for your permission to access your account. Allow all the permissions and you should be able to see the "Email Sender" menu on your Google sheet.

Story pin image


Step 7: Click on the "Send Email" Button
By clicking on the "Send Emails" button, it should trigger the script and employees should receive email notifications.

Story pin image


You can add below template in Column C to fetch names and other data:

Hi {name},

Greetings of the Day,

This is an automated email notification for salary increase. Your new salary is: {Other Data}. For any queries, please feel free to contact,

Thanks,
HR Team

Doing this will help the script know that name is to be kept where {name} is written and other data is to be fetched where {Other Data} is placed. Therefore, you can actually fetch same data multiple time in an email as well, or you can even play around yourself with the placements of these data-fetching curly-brackets.

To get your copy of this Google sheet, you can click here.

Author: thewiki Editorial
Google Apps Script Code to Send Automated Emails
Rajat: Thanks for sharing, it is a great tool. It has made life really simple for sending out bulk emails.

Shivani: Great Tool, thanks for sharing. Can you please share how to add time-driven triggers as well?