Search code examples
pythongoogle-apigmailgmail-apigoogle-api-python-client

How to get the number of mail read and unread or sent and received with gmali-api?


I want to use python to know how many emails have been sent and how many have been received.

label_id_one = 'INBOX'
label_id_two = 'UNREAD'
# Getting all the unread messages from Inbox
unread_msgs = GMAIL.users().messages().list(userId='me', labelIds=[label_id_one, label_id_two]).execute()

When I use this code,I can get the count of unread mails from inbox.

But how to get the count of readed mails?

Use the count of all mails minus unread mails?I don't think that's good.

I queried the API at this link, but no API provided such data.

How can I get such data?


Solution

  • I dont think the information you are looking for exists. The closest thing would be Users: getProfile

    which has a field in the response

    messagesTotal integer The total number of messages in the mailbox.

    {
     "emailAddress": "[email protected]",
     "messagesTotal": 66617,
     "threadsTotal": 14010,
     "historyId": "4618566"
    }
    

    However as soon as you delete a message the data is going to change and you will have one less total massages.

    As far as messages sent you could do a message.list and search for the messages in the sent folder and get the result back for that but thats going to be the messages you sent since you last cleaned out that folder.

    Answer: There is no data in the gmail api that states how many emails you have sent or received in the lifetime of your gmail account. The closest thing you could get would be

    • Sent: the number of messages in your sent folder (in:sent)
    • Received: the number of messages on your account that are not in your sent folder. (not in:sent)

    which would change as soon as you deleted a message.

    Tip: search messages

    Check Not read not in:sent label:unread

    unread messages

    unread_msgs = GMAIL.users().messages().list(userId='me', q='not in:sent label:unread').execute()
    

    read messages

    unread_msgs = GMAIL.users().messages().list(userId='me', q='not in:sent label:read').execute()