Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

106
Views
Get Emails from Gmail using Google App Script as 10 mins before to current time

I want to search email using Google App Script as 10 mins before to current time, I wrote the below script but it's not working, as I have emails with the same subject as defined in query and before 10 mins(even 1 min before mails are there) but GAS showing zero threads.

function testforemails(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Emails");
  var Gmail = GmailApp;
  var lasttime = sheet.getRange("Z1").getValue(); // it will use later as the time of last searched email.
  Logger.log(lasttime);
  var cdate = new Date();
  var ctime = cdate.getTime();
  var qDate = new Date(ctime - 600000); // less 10 minutes from current time.
  Utilities.formatDate(qDate, Session.getScriptTimeZone(), "dd-MMM-yy hh:mm a")
  Logger.log("QDATE IS " + qDate); //  perfectly displaying time less than 10 minutes
  var qtime = qDate.getTime();
  Logger.log("qtime is " + qtime);

  // SEARCH EMAIL
  var query = 'subject: defined subject of emails, after:' + (qDate);
  var threadsNew = Gmail.search(query);
  Logger.log(threadsNew.length);
  var folderid = 'define folder id'
  var folder = DriveApp.getFolderById(folderid);
  for(var n in threadsNew){
    var thdNew  = threadsNew[n]; 
    var msgsNew = thdNew.getMessages(); 
    var msgNew = msgsNew[msgsNew.length-1];
  // GET ATTACHMENT
    var bodyNew = msgNew.getBody();
    var plainbody  = msgNew.getPlainBody();
    var subject = msgNew.getSubject();
    var Etime = msgNew.getDate();
    var attachments = msgNew.getAttachments();
    var attachment = attachments[0];
    
    Logger.log(Etime);
    Logger.log(subject);
  }
    
    Logger.log(threadsNew.length);
    
    
}

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Update

This is accualy can be done, see the asnwer of @Iamblichus. Anyway, my method also works so I keep for the user to choose which method better for his needs.


A workaround to achive this goal:

function testforemails(){
  // SEARCH EMAIL
  let now = new Date();
  let query = 'subject: YOUR_SUBJECT, after:' + now.toISOString().slice(0, 10); // find mails from today
  console.log(query);
  let threadsNew = GmailApp.search(query);
  console.log("threadsNew.length: " + threadsNew.length);

  for(let thread of threadsNew){
    let lastMsgTime = thread.getLastMessageDate();
    let tenMinutesAgo = new Date(now - 600000);

    if(lastMsgTime - tenMinutesAgo > 0) { // if the last message on the thread was less than 10 minutes ago
      // do your magic here
      console.log(thread.getLastMessageDate())
    }  
  }    
}
about 4 years ago · Juan Pablo Isaza Report

0

Issue:

In Gmail search operators, before and after timestamps work with seconds, not with milliseconds.

Solution:

Divide the timestamp by 1000, making sure it's an integer:

function testforemails(){
  var cdate = new Date();
  var ctime = cdate.getTime();
  var qDate = new Date(ctime - 600000); // less 10 minutes from current time.
  var query = 'after:' + Math.floor(qDate.getTime()/1000);
  var threadsNew = GmailApp.search(query);
  Logger.log(threadsNew.length);
  // ...
}

Other issues:

  • In your query you provided the date directly, without transforming to the number of milliseconds. This cannot work:
var qDate = new Date(ctime - 600000);
var query = 'subject: defined subject of emails, after:' + (qDate);

Related:

  • Is it possible to query Gmail messages by timestamp?
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!