Find Gmail Messages that are Awaiting Response

Google Apps Script for Gmail, creates a label that matches all threads where you are the last sender. Useful for finding email messages in your mailbox that are still awaiting a response from the customer. Credit @GSAPU.

function label_last_sent_message() {
  var emailAddress = Session.getEffectiveUser().getEmail();
  Logger.log(emailAddress);
  var EMAIL_REGEX = /[a-zA-Z0-9\._\-]+@[a-zA-Z0-9\.\-]+\.[a-z\.A-Z]+/g;
  var label = GmailApp.createLabel('LastSentMessage');
  var d = new Date();
  d.setDate(d.getDate() - 7);
  var dateString = d.getFullYear() + '/' + (d.getMonth() + 1) + '/' + d.getDate();
  threads = GmailApp.search('in:Sent after:' + dateString);
  for (var i = 0; i < threads.length; i++) {
    var thread = threads[i];
    var lastMessage = thread.getMessages()[thread.getMessageCount() - 1];
    var lastMessageSender = lastMessage.getFrom().match(EMAIL_REGEX)[0];
    if (thread.getMessageCount() > 1 && lastMessageSender == emailAddress) {
      thread.addLabel(label);
    }
  }
}

Amit Agarwal is a web geek, solo entrepreneur and loves making things on the Internet. Google recently awarded him the Google Developer Expert and Google Cloud Champion title for his work on Google Workspace and Google Apps Script.

Awards & Recognition

Google Developer Expert

Google Developer Expert

Google awarded us the Developer Expert title recogizing our work in Workspace

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional title for 5 years in a row

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator award for technical expertise

Want to stay up to date?
Sign up for our email newsletter.

We will never send any spam emails. Promise 🫶🏻