Searching emails in gmail based on time

蹲街弑〆低调 提交于 2019-12-07 11:55:08

问题


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 after: and before: search query in gmail returns results that are not expected, since the query searches on the basis of the SMTP server time that the mail is sent from (which is google's server). Hence, being in a different time zone, the search yields inappropriate results to me. Is there a way to search gmail using a time criteria, so that I can accommodate for the time zone difference?

Please note that the local time zone, calendar, gmail etc. is correctly configured for my timezone, so the emails that I see in my inbox are correctly timed. Only the search is creating an issue.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/12261785/searching-emails-in-gmail-based-on-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!