2017-02-13 169 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); 
    }