Laravel 5.2 通过 oriceon/oauth-5-laravel 发送由 swift_message 创建的电子邮件

Laravel 5.2 sending emails created by swift_message via oriceon/oauth-5-laravel

如下图所示,我正在尝试使用 Laravel 5.2 和 oriceon/oauth-5-laravel package 来授权我网站上的用户,然后允许该用户通过自己的帐户向其他人发送电子邮件通过我的网站。

Attempt:1 using laravels inbuilt swift_message(note emails are just examples)
$message = Swift_Message::newInstance();
$message->setSubject("Test Email");
$message->setFrom("dukeforsythtester@gmail.com");
$message->setTo("tester@gmail.com");
$message->setReplyTo("dukeforsythtester@gmail.com");
$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"]= "dukeforsythtester@gmail.com"; 
$envelope["to"]  = "dukeforsyth@outlook.com";
$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”,可以在用户授权后为用户发送电子邮件。我还确保发件人地址是授权用户的电子邮件地址,但我不确定我是在做一些愚蠢的事情还是在编码中遗漏了什么。

任何帮助将不胜感激,现在已经坚持了两天!

这是我的解决方案,使用 Laravel 5.2 和 oriceon/oauth-5-laravel 包以及 google 的 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 = 'dukeforsythtester@gmail.com';
        // 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);
    }