Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if mail is read, gmail api

I am working on a gmail app (you can probably tell if you read my other questions), I can list my first 10 email, however I have no way of knowing weather they are read or not. Does anyone know how I would even go about this? Thanks!

like image 387
zoecarver Avatar asked Dec 07 '16 01:12

zoecarver


People also ask

Can you check if an email has been read in Gmail?

To find out when an email you sent was opened, you can request a read receipt. A read receipt is sent to you as an email with the time and date of when your message was opened. Want to get more out of Google apps at work or school? Sign up for a Google Workspace trial at no charge.

Is there a way to tell if an email is read?

Request a return receipt Most major email platforms will give you the option to request return/read receipts with email that you send. Some will also let you specify these receipts for every email you're composing.


1 Answers

If I list my newest message:

Request

GET https://www.googleapis.com/gmail/v1/users/me/messages?maxResults=1&access_token={ACCESS_TOKEN}

Response

{
 "messages": [
  {
   "id": "158d822014b7887b",
   "threadId": "158d822014b7887b"
  }
 ],
 "nextPageToken": "17683191771541399341",
 "resultSizeEstimate": 2
}

And then get the message:

Request

GET https://www.googleapis.com/gmail/v1/users/me/messages/158d822014b7887b?format=metadata&access_token={ACCESS_TOKEN}

Response

{
 "id": "158d822014b7887b",
 "threadId": "158d822014b7887b",
 "labelIds": [
  "UNREAD",
  "CATEGORY_SOCIAL",
  "INBOX"
 ],
 ...
}

You can see that the message has a labelId with the value of UNREAD. If the message doesn't have this labelId, it is read.

var isRead = message.labelIds.indexOf("UNREAD") === -1;
like image 106
Tholle Avatar answered Sep 30 '22 06:09

Tholle