How to format body text coming from gmail api in PHP?

I am fetching several data from gmail API using credentials and token from Google Cloud Console Gmail API. Over here I am fetching data from the API:

public function listMessages()


$service = new Google_Service_Gmail($this->client);

// Print the labels in the user's account.

$userId = 'me';

$pageToken = NULL;

$messages = array();

$opt_param = array();

$i =0;


if ($i == 5) break;


try {

if ($pageToken){

$opt_param['pageToken'] = $pageToken;


$messageResponse = $service->users_messages->listUsersMessages($userId, $opt_param);

if ($messageResponse->getMessages()){

$messages = array_merge($messages,$messageResponse->getMessages());

$pageToken = $messageResponse->getNextPageToken();



catch (Exception $e){

print 'An error occurred: '. $e->getMessage();


}while ($pageToken);

$list = $service->users_messages->listUsersMessages('me', [

'maxResults' => 10


$messageList = $list->getMessages();

$inboxMessage = [];

foreach ($messageList as $message){

// print 'Message with ID: ' . $message->getId() . '<br/>';

$msg = $service->users_messages->get($userId, $message->getId());

$parts = $msg->getPayload()->getParts();

$message_id =$message->getId();

$headers = $msg->getPayload()->getHeaders();

$snippet = $msg->getSnippet();

foreach($headers as $single) {

if ($single->getName() == 'Subject') {

$message_subject = $single->getValue();

} elseif ($single->getName() == 'Date') {

$message_date = $single->getValue();

$message_date = date('M jS Y h:i A', strtotime($message_date));

} elseif ($single->getName() == 'From') {

$message_sender = $single->getValue();

$message_sender = str_replace('"', NULL, $message_sender);

$message_sender = str_replace("\'", NULL, $message_sender);



if (count($parts) > 0){

//$data = $msg->getPayload()->getParts()[0]->getBody()->getData();

$data = $parts[0]->getBody()->getData();



$data = $msg->getPayload()->getBody()->getData();


$out = str_replace("-","+",$data);

$out = str_replace("_","/",$out);

$body = base64_decode($out);

$inboxMessage[] = [

'messageId' => $message_id,

'messageSnippet' => $snippet,

'messageSubject' => $message_subject,

'messageDate' => $message_date,

'messageSender' => $message_sender,

'messageBody' => $body



return $inboxMessage;



And over here I am displaying it:

public function go()


$conn = new Connection();

if ($conn->is_connected()){

$gmail = new Gmail($conn->get_client());

return $gmail->listMessages();



return $conn->get_unathuenticated_data();



$ID = $_GET['id'];

$Main = new Main();

$aData = $Main->go();

<div class="container mt-5">

<?php print $aData[$ID]['messageBody']; ?>


But the body is coming like in the attached picture

I want it to be formatted nicely like in Gmail it comes formatted. How do I do it please?

Kemahiran: PHP, Email Developer, Format & Susun Atur, HTML, CSS

Lihat lagi: gmail api php, gmail api php 2009, gmail api php contact, list gmail api php, translate text speech google api php, how to send mail from localhost in php using xampp, how to send mail from localhost in php using wamp, how to retrieve data from database in php using mysql, how to fetch data from database in php using mysqli, how to fetch data from database in php using ajax, gmail api php send email, gmail api php example, gmail api php github, how to download attachments from gmail api, vba outlook format body text, google text to speech api php, send mail gmail api php, how to read text from image in php

Tentang Majikan:
( 0 ulasan ) Ulhasnagar, India

ID Projek: #30530635

Dianugerahkan kepada:


Hello! I am Ravi Oza and I am a professional PHP | Magento developer | .Net developer with more then five years of experience. As per your proposal details, You need format your API response. And as per my experienc Lagi

₹1500 INR dalam 2 hari
(2 Ulasan)