//Gmail message truncated while read
Email content is : replying to this email with a very long text.
Why do we use it?
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
I am getting only this text : Why do we use it? It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-
Please help me!!
My PHP code is given below
<?php
require __DIR__ . '/vendor/autoload.php';
function getClient() {
$client = new Google_Client();
$client->setApplicationName('Gmail API PHP Quickstart');
$client->setScopes(array('https://mail.google.com/'));
$client->setAuthConfig('credentials.json');
$client->setAccessType('offline');
$client->setPrompt('select_account consent');
// Load previously authorized token from a file, if it exists.
$tokenPath = 'token.json';
if (file_exists($tokenPath)) {
$accessToken = json_decode(file_get_contents($tokenPath), true);
$client->setAccessToken($accessToken);
}
// If there is no previous token or it's expired.
if ($client->isAccessTokenExpired()) {
// Refresh the token if possible, else fetch a new one.
if ($client->getRefreshToken()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:\n%s\n", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
$client->setAccessToken($accessToken);
// Check to see if there was an error.
if (array_key_exists('error', $accessToken)) {
throw new Exception(join(', ', $accessToken));
}
}
// Save the token to a file.
if (!file_exists(dirname($tokenPath))) {
mkdir(dirname($tokenPath), 0700, true);
}
file_put_contents($tokenPath, json_encode($client->getAccessToken()));
}
return $client;
}
$client = getClient();
$gmail_service = new Google_Service_Gmail($client);
$gmail_user = 'me';
$results = $gmail_service->users_messages->listUsersMessages($gmail_user, array('q' => 'is:unread'));
$messages = $results->getMessages();
foreach ($messages as $message) {
$messageId = $message->id;
$message = $gmail_service->users_messages->get($gmail_user, $messageId);
$body = $message->getSnippet(); //**message is truncated**
echo $body;die;
}
Try replacing $message->getSnippet() with the following:
$body = base64_decode($message->getPayload()->getParts()[0]->getBody()->getData());
Try replacing
$message->getSnippet()with the following:$body = base64_decode($message->getPayload()->getParts()[0]->getBody()->getData());
If I need only reply of an email then what should I do? Please suggest me?
Thank you very much for your help
You could use the value of $message->getThreadId() to call Users.threads.get, then search the list of messages for the reply you need.
Hello @jdpedrie
I have used
$message->getThreadId()
and used below getThread function then also I am getting truncated message.
function getThread($service, $userId, $threadId) {
try {
$thread = $service->users_threads->get($userId, $threadId);
$messages = $thread->getMessages();
$msgCount = count($messages);
print 'Number of Messages in the Thread: ' . $msgCount;
return $thread;
} catch (Exception $e){
print 'An error occurred: ' . $e->getMessage();
}
}
While I am using your first suggestion
$body = base64_decode($message->getPayload()->getParts()[0]->getBody()->getData());
Then I am getting encoded characters instead of proper email, please check below scenario
1) Original email content screenshot

2) I am getting below result via $body = base64_decode($message->getPayload()->getParts()[0]->getBody()->getData());

Hi @pragneshrupapara,
Unfortunately, this forum is more intended to resolve bugs and specific support requests related to the Google API PHP Client. The answers I've given you thus far were obtained by studying the Gmail API reference documentation. I'd suggest taking some time to read and understand the underlying API. If you have questions regarding implementation details, we suggest searching https://stackoverflow.com/questions/tagged/gmail-api, and if your question has not yet been answered, posting for assistance.
That said, should you run into problems which are specific to this client, please ask and we'll continue to attempt to assist you.
edit: This question on Stackoverflow may provide some specific guidance you would find useful.
Most helpful comment
Try replacing
$message->getSnippet()with the following: