duanmei4362 2017-02-13 20:18
浏览 38
已采纳

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

As shown in my attempts belowbelow I am trying to use Laravel 5.2 with oriceon/oauth-5-laravel package to authorize a user on my website and then allow that user to send a email to someone else thruough their own account via my website.

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


\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);

Either way I have the response of:

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

I believe I have the correct scopes 'https://www.googleapis.com/auth/gmail.send' for sending emails for the user after they authorize it. I also made sure that the from address is the authorized users email address, but I am unsure if I am just doing something silly or missing something in the encoding.

Any help would greatly be appreciated, been stuck on this for two days now!

  • 写回答

1条回答 默认 最新

  • douwen7516 2017-02-15 19:34
    关注

    Here is my solution using Laravel 5.2 with oriceon/oauth-5-laravel package along with google's google/apiclient package to send the actual message. Big thanks to @Mr.Rebot for the guidance!

        $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);
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败