2017-02-13 174 views
0

發送由swift_message創建的電子郵件如我在下面的嘗試中所示,我試圖使用Laravel 5.2和oriceon/oauth-5-laravel package來授權我的網站上的用戶,然後允許該用戶發送電子郵件給其他人通過我的網站通過自己的帳戶。Laravel 5.2通過oriceon/oauth-5-laravel

Attempt:1 using laravels inbuilt swift_message(note emails are just examples) 
$message = Swift_Message::newInstance(); 
$message->setSubject("Test Email"); 
$message->setFrom("[email protected]"); 
$message->setTo("[email protected]"); 
$message->setReplyTo("[email protected]"); 
$message->setBody("This is a test of adding a body!"); 
$to_send = rtrim(strtr(base64_encode($message->toString()), '+/', '-_'),'='); 
$result = $googleService->request('https://www.googleapis.com/gmail/v1/users/me/messages/send',$to_send); 


Attempt 2: using imap_mail_compose via php natively 
$envelope["from"]= "[email protected]"; 
$envelope["to"] = "[email protected]"; 
$envelope["subject"] = "Testing..."; 
$part1["type"] = TYPETEXT; 
$part1["subtype"] = "plain"; 
$part1["description"] = "description3"; 
$part1["contents.data"] = "contents.data3\n\n\n\t"; 

$body[1] = $part1; 

$mime = imap_mail_compose ($envelope , $body); 
$mime = rtrim(strtr(base64_encode($mime), '+/', '-_'), '='); 
$result = $googleService->request('https://www.googleapis.com/gmail/v1/users/me/messages/send',$mime); 

無論哪種方式,我的迴應:

TokenResponseException in StreamClient.php line 68: 
Failed to request resource. HTTP Code: HTTP/1.1 400 Bad Request 

我相信我有用於發送電子郵件的用戶,他們授權後,正確的範圍「https://www.googleapis.com/auth/gmail.send」。我也確定發件人地址是授權用戶的電子郵件地址,但我不確定我是否在做一些愚蠢或缺少編碼方面的東西。

任何幫助將不勝感激,一直堅持了兩天吧!

+0

你可能想要檢查你是如何編寫你的請求的。發生錯誤400:錯誤的請求可能是由於錯誤的請求格式,傳遞給Google服務器的必需參數不足。你可能想要檢查這個相關的SO [post](http://stackoverflow.com/a/32667951/5995040),討論瞭如何認證可以請求返回錯誤400.對於某些代碼實現,你可能想要檢查Google的[PHP快速入門](https://developers.google.com/gmail/api/quickstart/php)。希望這可以幫助。 –

+0

就像你說的這是一個格式不正確的請求。 我需要更多地瞭解的是正確請求的格式是什麼,正如這裏的文檔[link](https://developers.google.com/gmail/api/v1/reference/users/messages /發送)它聲明在發佈請求中我需要傳遞一個Users.messages資源。 從我在文檔中看到的這個看起來像一個json對象和正確的參數。我試圖從鏈接中模擬這個例子,如上所示,但我不確定是否需要手動設置ID等。 –

+0

我有一個工作解決方案!在這裏張貼只是因爲有人遇到過這個問題或類似的東西! –

回答

2

這裏是我的解決方案,使用Laravel 5.2與oriceon/oauth-5-laravel軟件包一起發送實際的消息以及谷歌的google/apiclient軟件包。非常感謝@ Mr.Rebot的指導!

$code = Input::get('code'); 
    $googleService = \OAuth::consumer('Google'); 
    // if code is provided get user data and sign in 
    if (! is_null($code)) 
    { 
     // Variables 
     $redirect_uri = 'your redirect url that is set on google console'; 
     $user_to_impersonate = '[email protected]'; 
     // Create client with these set credentials 
     $client = new \Google_Client(); 
     $client->setAuthConfig('path to your json given by google console'); 
     $client->setRedirectUri($redirect_uri); 
     $client->setSubject($user_to_impersonate); 
     $scopes = [ \Google_Service_Gmail::GMAIL_SEND ]; 
     $client->setScopes($scopes); 

     $token = $client->authenticate($code); 
     $client->setAccessToken($token);   
     if($client->isAccessTokenExpired()) 
     { 
      // need to refresh token 
      $refreshToken = $client->refreshToken($token); 
      $client->setAccessToken($refreshToken); 

     } 

     // Create the service 
     $service = new \Google_Service_Gmail($client); 

     // Create the message 
     $msg = new \Google_Service_Gmail_Message(); 
     $msg->setRaw($this->create_message()); 

     // Send off the message 
     $this->sendMessage($service, 'me', $msg); 
    } 
    else { 
     // Get googleService authorization 
     $url = $googleService->getAuthorizationUri(); 
     // return to google login url 
     return redirect((string)$url); 
    }