Searching emails in gmail based on time

后端 未结 2 1509
抹茶落季
抹茶落季 2021-01-14 09:39

I want a list of all yesterday\'s emails from gmail. I am trying to process it using google apps script, by writing a query on my inbox, and then using GmailApp.search. The

相关标签:
2条回答
  • 2021-01-14 10:03

    I actually figured out a way after some trial and error, that it is indeed possible to search gmail emails by time. I noticed that the Date() returned in google apps script was according to my timezone. The code below will return all previous day's emails in inbox, assuming new Date() is giving the date and time according to your timezone. Note that getTime() returns milliseconds while the newer/older query expects seconds.

    var month = new Date().getMonth();
    var date = new Date().getDate();
    var year = new Date().getFullYear();
    var time1 = new Date(year, month, date, 0, 0, 0).getTime();
    var time2 = time1 - 86400000;
    var query = "newer:" + time2/1000 + " older:"  + time1/1000 + " in:inbox";
    var conversations = GmailApp.search(query);
    

    I hope it helps someone.

    0 讨论(0)
  • 2021-01-14 10:06

    Can you give the exact search string you are using along with how you construct the before and after dates ? You can use the Utilities.formatDate() function to format the date string to the timezone you are in.

    An alternate solution is to fetch all mails (maybe a 100 or so) and then discard all those which do not fit in the time period you are interested in.

    0 讨论(0)
提交回复
热议问题