2016-10-03 61 views
3

編輯:修正了第一個錯誤!現在列出郵件ID,仍然試圖解決如何從收件箱中顯示完整電子郵件,如果任何人都可以提供幫助,將不勝感激。GMail API - 如何顯示收件箱中的完整電子郵件

所以我試圖使用GMail API來調用我的收件箱中的電子郵件,所以我可以在我自己的應用程序中使用它們。我已經得到所有訪問排序,並且目前有一個數組正在返回。我遇到的問題是試圖顯示消息。

我的PHP看起來如下

<?php 
ini_set('display_errors' , 'On'); 
error_reporting(E_ALL); 

// Start Session 
session_start(); 

//Include API autoloader 
require_once __DIR__ . '/vendor/autoload.php'; 

// Create Google Client 
$client = new Google_Client(); 
$client->setClientId('ID in here'); 
$client->setClientSecret('Secret here'); 
$client->setRedirectUri('http://localhost:8888/gmail/'); 
$client->addScope('https://mail.google.com/'); 

//Create Google Service 
$service = new Google_Service_Gmail($client); 

if(isset($_REQUEST['logout'])){ 
    unset($_SESSION['access_token']); 
} 

// Check if there is an auth token 
if(isset($_GET['code'])) 
{ 
    $client->authenticate($_GET['code']); 
    $_SESSION['access_token'] = $client->getAccessToken(); 
    $url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; 
    header('Location: ' . filter_var($url, FILTER_VALIDATE_URL)); 
} 

// Check for access token in session 
if(isset($_SESSION['access_token'])) 
{ 
    $client->setAccessToken($_SESSION['access_token']); 
} else 
{ 
    $loginUrl = $client->createAuthUrl(); 
    echo 'Click <a href="' . $loginUrl . '">here</a> to login'; 
} 

// Check if we have an access token ready for API Call 
try 
{ 
    if(isset($_SESSION['access_token']) && $client->getAccessToken()) 
    { 
     //make API calls 
     $messages = $service->users_messages->listUsersMessages('me',['maxResults'=> 2, 'labelIds'=> 'INBOX']); 

     foreach ($messages as $message) 
     { 
      print 'Message with ID: ' . $message->getId() . '<br/>'; 
     } 

     return $messages; 
    } 
} 
catch (Google_Auth_Exception $e) 
{ 
    echo 'Looks like you dont have an access token or its expired'; 
} 
+0

您必須按照以前的方式列出消息,但必須向[** get **]發出單獨的請求( https://developers.google.com/gmail/api/v1/reference/users/messages/get#try-it)郵件內容。 [**這個答案**](http://stackoverflow.com/questions/32655874/cannot-get-the-body-of-email-with-gmail-php-api/32660892#32660892)可能會有所幫助。 – Tholle

回答

0

添加到什麼@Tholle建議的messages名單將具有From屬性以指示郵件的發件人標頭。您只需在foreach messages內添加一個新循環或專門調用payload[x].header.From

相關問題