how can I get list of email in gmail api with title and sender of email?

I have tried using GET https://www.googleapis.com/gmail/v1/users/userId/messages

but only get id and threadId as a response.

I want to display list of email with title and from email. Is there an api exist to get info or any way I can get it?

2 answers

  • answered 2019-09-11 09:28 DaImTo

    The user messages.list method only returns a message list of message id and thread id. This is a limitation in the API itself and there is nothing you can do to change that. Its a free api and we are bound by the limitations of google.

    {
     "messages": [
      {
       "id": "16d1f7849145662a",
       "threadId": "16d1f55457d4e145"
      },
      {
       "id": "16d1f69d541016ee",
       "threadId": "16d1f55457d4e145"
      },
    

    In order to get additional information about the message you need to do a message.get on each message.

    GET https://www.googleapis.com/gmail/v1/users/userId/messages/id
    

    Sorry its the only way its a lot of gets.

    batching

    You can try the batching endpoing batch You're limited to 100 calls in a single batch request. If you need to make more calls than that, use multiple batch requests.

    POST /batch/farm/v1 HTTP/1.1
    Authorization: Bearer your_auth_token
    Host: www.googleapis.com
    Content-Type: multipart/mixed; boundary=batch_foobarbaz
    Content-Length: total_content_length
    
    --batch_foobarbaz
    Content-Type: application/http
    Content-ID: <item1:12930812@barnyard.example.com>
    
    GET /farm/v1/animals/pony
    
    --batch_foobarbaz
    Content-Type: application/http
    Content-ID: <me@gmail.com>
    
    PUT users/me/messages/1
    Content-Type: application/json
    Content-Length: part_content_length
    
    --batch_foobarbaz
    Content-Type: application/http
    Content-ID: <me@gmail.com>
    
    GET users/me/messages/2   
    
    --batch_foobarbaz--
    

    SMTP

    beyond that you can go though the smtp server directly. This will require a programming language which can handle direct calls to a mail server.

  • answered 2019-09-11 10:02 ziganotschka

    Gmail API is designed in such a way, that

    1. you need to obtain the IDs of all emails of interest (you can use the search parameter q to filter the results) with GET https://www.googleapis.com/gmail/v1/users/userId/messages - as you already did
    2. you need to retrieve each of the messages separately by their ID with GET https://www.googleapis.com/gmail/v1/users/userId/messages/id

    If you write a code in a language of your choice you can automatize this process with loops.

    Sample how to do it with Apps Script:

    function myFunction() {
      var myMessages=Gmail.Users.Messages.list("me",{'maxResults': 5 }).messages;
      Logger.log(myMessages);
      for(var i=0;i<myMessages.length;i++){
        var id=myMessages[i].id;
        Gmail.Users.Messages.get('me', id).payload.headers.forEach(function(e){
          if(e.name=="Subject"||e.name=="From"){
            Logger.log(e.name+": "+e.value)
          }
         }
        );
      }
    }