Saturday, February 4, 2017

Productivity: Google spreadsheet plugin hack sending mass emails with dynamic template


How this hack is going to help?
  • If you have a list of email address to which you want to one email with custom message/email body then this hack will help with that.
  • Mass email campaign is a good example of this 
  • Single email notifications to a group of email addresses
STEP-1: Create a google spreadhseet with two sheets.
1. 1st sheet (Sheet Name: EmailList): have the following columns: (export the email data and store in the below format)
    -- Email Address
    -- First Name
    -- Last Name
and
     2nd sheet (Sheet Name: SendEmail): have the following columns:
     -- Email (Formula: =EmailList!A2)
     -- Message (Formula: =CONCATENATE(C2, CHAR(10),CHAR(10), G2, CHAR(10),CHAR(10), "Thanks,",CHAR(10),H2))
     -- Sub Message (Formula: =CONCATENATE("Hello ",E2, ", "))
     -- Full Name (Formula: =CONCATENATE(E2," ", F2))
     -- First Name (Formula: =EmailList!B2)
     -- Last Name (Formula: =EmailList!C2)
     -- Email Template Content (Store Email body/template)
     -- From Name (Store from name)
STEP-2: Copy paste the formula to multiple rows
STEP-3: Go to menu->Tools->Script Editor and paste the following code snippet
/**
* @description Google Spreadsheets plugin to bulk email
*/

function onInstall(){
  onOpen();
}

function onOpen() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var menuEntries = [   {name: "Send Bulk Emails", functionName: "sendbulkEmails"},
                    ];
  ss.addMenu("Send Bulk Email", menuEntries);
}


function sendbulkEmails() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2;  // First row of data to process
  var numRows = 2;   // Number of rows to process
  // Fetch the range of cells A2:B5
  var dataRange = sheet.getRange(startRow, 1, numRows, 5)
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  for (i in data) {
    var row = data[i];
    var emailAddress = row[0];  // First column
    var message = row[1];       // Second column
    var subject = "Sending emails from a Spreadsheet";
    MailApp.sendEmail(emailAddress, subject, message);
  }
}
STEP-4: Save the script with "BulkEmail" project name.
STEP-5: Come back to spreadsheet refresh the page and you will see the "Send Bulk Email" menu. Clicking the menu link will send the mass emails to the all the rows selected in the script.

Screenshots: