Search code examples
gmail-api

How can I check mail is reply mail or simple mail using google api client?


$optParams['maxResults'] = 5; 

$optParams['labelIds'] = 'INBOX'; 

$messages = $service->users_messages->listUsersMessages('sender@mail.com',$optParams);

$messageList = $messages->getMessages();

Got list of mail how can I differentiate reply mail and simple mail using this function ?


Solution

  • You cannot do it directly with users.messages.list

    Once you have the message list, you need to perform with each message Id a call to users.messages.get and look at the headers.

    Response for simple mail:

    "headers": [
        ...
    
          {
            "name": "Subject",
            "value": "Hello"
          },
          {
    
        ]
    

    Response for reply:

    "headers": [
         ...
          {
            "name": "References",
            "value": "\u003cCAKk+M3QBfT964eMSLRsUziugpF7uK+W7YrMv-kKE=tM4+DPxHw@mail.gmail.com\u003e"
          },
          {
            "name": "In-Reply-To",
            "value": "\u003cCAKk+M3QBfT964eMSLRsUziugpF7uK+W7YrMv-kKE=tM4+DPxHw@mail.gmail.com\u003e"
          },
    
          {
            "name": "Subject",
            "value": "Re: Good morning"
          },
    
        ]
    

    Differences:

    • A reply subject contains Re:
    • A reply contains the header References
    • A reply contans the header In-Reply-To