googleapis / google-api-php-client

A PHP client library for accessing Google APIs
Apache License 2.0
9.2k stars 3.52k forks source link

Value of 'To' header in Gmail is blank, but isn't empty #2505

Closed JECGEdu closed 7 months ago

JECGEdu commented 9 months ago

Hi, I'm trying to access the value of the 'To' header using the Gmail Service with the API. It seems to show that there is a value, however it just comes up blank. I first get the message by passing it's ID, then try to find the 'To' header and get it's value. All the other headers seem to be showing their values correctly.

Below is the code I call to get the message

$message = $gmail->users_messages->get('me', $messageID);

Below is what I use to get the value of the 'To' header

// Get the email recipient
$to = '';
foreach($message->getPayload()->getHeaders() as $header) {
    if($header->name == 'To') {
        $to = $header->value;
        break;
    }
}

I use similar code to get the subject and from headers, which work as expected. When I var_dump the $message object itself, this is what shows for the 'To' header

object(Google\Service\Gmail\MessagePartHeader)#67 (5) {
        ["internal_gapi_mappings":protected]=>
        array(0) {
        }
        ["modelData":protected]=>
        array(0) {
        }
        ["processed":protected]=>
        array(0) {
        }
        ["name"]=>
        string(2) "To"
        ["value"]=>
        string(28) ""
      }

As you can see, it's saying that there is something there that is 28 characters, but nothing is showing. Any help would be appreciated.

Thanks!

saranshdhingra commented 8 months ago

Hi @JECGEdu I tried the exact same code that you provided, but I was able to see the value for the To header.

I would recommend you to try the API using the REST API reference.

Perhaps the anomaly you're seeing is because of a specific type of message or was a one-off.

Let me know if you see an empty response after trying the reference.

saranshdhingra commented 7 months ago

Closing this issue due to lack of activity. Please feel to reopen if this occurs again and you have tried the REST API reference as mentioned above.

JECGEdu commented 6 months ago

Sorry for the lack of inactivity, I found a work around for the use case I needed it for (saved the info I needed in a database). But now I need it again for a different use case and it still showing blank. I tried the REST API above and the values come through as expected but I am using a service account and don't think I can use the REST API with a service account. Still, it doesn't explain why the "To" header shows like that, other headers have the same behavior, "Message-Id", "In-Reply-To" and "References" are the ones I can see at the moment.

This happens with most message as well, so not a one-off