Find Gmail Messages that are Awaiting Response

F
Published in: Gmail - Google Apps Script

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);
    }

  }
}
Published in: Gmail - Google Apps Script

Looking for something? Find here!

Meet the Author

Web Geek, Tech Columnist
A
Amit Agarwal

Amit Agarwal is a Google Developer Expert in GSuite and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. Read more on Lifehacker and YourStory

Get in touch