Pages

Saturday 19 August 2017

How to send mass emails from Gmail account

This post explains how to send mass emails using google spreadsheet and a simple google script from your gmail account.













Scenario:

We have a following spreadsheet in google drive, which has marks of students for an assignment.
Our task is to send them a mail with the marks.

Col A - Email addresses of students
Col B - Total mark of the student
Col C to R - Marks of Question 1 to 5, whose total is stored in column B


Message body of the mail should look like as following:

TOTAL-47
1A-5
1B-4
1C-2
2A-3
2B-1
2C-4
3A-5
3B-2
3C-3
3D-2
4A-1
4B-4
4C-5
5A-2
5B-3
5C-1

Steps: 

1. Log into google account and create a spreadsheet in google drive as shown above. (keep it open in the browser)

2. In the spreadsheet, goto Tools -> Script editor

3. Paste following code in the script editor and save it. 

function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();

// upper-left-most cell (1,1)
var startRow = 1;
var startCol = 1;
// number of rows and cols to fetch from upper-left-most cell
var numRows = 3;
var numCols = 18;

// fetching range and data
var dataRange = sheet.getRange(startRow, startCol, numRows, numCols);
var data = dataRange.getValues();

// Subject of the mails
var subject = "Assignment 1 Marks";

// first row content will be stored
var header;

for (i in data) {
if(i==0){
header = data[i]; // i=0 first row
}
else{
var row = data[i]; // fetching row content
var emailAddress = row[0];
var message=header[0]; // header[0] is blank

// appending header and corresponding row content
for(var j=1; j<numCols; j++){
message = message.concat(header[j]);
message = message.concat("-");
message = message.concat(row[j].toString());
message = message.concat("\n");
}

// sending mail
MailApp.sendEmail(emailAddress, subject, message);
}
}

}


4. We are ready to run the script. Run the script emails will be sent from your gmail account. 



No comments:

Post a Comment