Skip to content

Instantly share code, notes, and snippets.

@abbood
Created November 15, 2017 10:09
Show Gist options
  • Save abbood/d4305aba0fec880e86004a74cc272337 to your computer and use it in GitHub Desktop.
Save abbood/d4305aba0fec880e86004a74cc272337 to your computer and use it in GitHub Desktop.
mass email
// This constant is written in column C for rows for which an email
// has been sent successfully.
var EMAIL_SENT = "initial contact";
var GOT_CONTACT = "got contact"
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 100; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, 7)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var name = row[0];
var firstName = name.split(" ")[0];
var status = row[3];
var company = row[4];
var title = row[5];
var email = row[6];
var testEmail = "abdullah.bakhach+"+firstName+"@gmail.com";
if (status == GOT_CONTACT && email !=null) { // Prevents sending duplicates
var subject = "Sr Full Stack Engineer Vacancy for a start-up in Beirut";
var message = "Hey "+firstName+",<br><br>"
message +="My name is Abdullah, responsible for engineering here at <a href='http://totersapp.com/'>Toters</a>. I saw your profile on Linkedin "
message +="and was interested in your experience at "+company+" as "+title+".<br><br>"
message +="We have a lot of interesting things going on here from Test Driven Development, Devops, and advanced Kanban usage. <br><br>"
message +="It would be great to have a chat, please let me know if you are interested.<br><br>"
message +="<b>Abdullah Bakhach<br></b>"
message +="<a style='text-decoration : none; color : black;weight:bold' href='http://totersapp.com/'>Toters Delivery</a>, "
message +="<b style='color: #333333'>CTO</b><br>"
message +="<a href='https://stackoverflow.com/users/766570/abbood?tab=profile'>StackOverflow</a><br>"
message +="<a href='https://www.linkedin.com/in/abdullahbakhach/'>Linkedin</a><br>"
MailApp.sendEmail(email, subject,'sample body', {
htmlBody: message
});
sheet.getRange(startRow + i, 4).setValue(EMAIL_SENT);
Logger.log("successfully sent email to: "+name+" at email: "+email);
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
}
}
}
function onEdit(e) {
updateStatusOnContact(e);
}
function updateStatusOnContact(e) {
Logger.clear();
Logger.log("hello");
var SHEET_NAME = 'details - batch 1';
var CONTACT_COLUMN_INDEX = 7;
var STATUS_COLUMN_INDEX = 4;
var range = e.range;
var INITIAL_STATUS = "requested contact";
var FINAL_STATUS = "got contact";
Logger.log("about to run check");
var activeSheet = e.source.getActiveSheet();
var status = activeSheet.getRange(e.range.rowStart, STATUS_COLUMN_INDEX).getValue();
Logger.log("activeSheet.getName: '" + activeSheet.getName() +"', range.getcolumn: '"+range.getColumn() + "', status: '"+status+"'");
if (activeSheet.getName() !== SHEET_NAME || range.getColumn() !== CONTACT_COLUMN_INDEX || status !== INITIAL_STATUS) {
Logger.log("check failed");
return;
}
activeSheet.getRange(e.range.rowStart, STATUS_COLUMN_INDEX).setValue(FINAL_STATUS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment