Reputation: 21
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("[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);
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!
Upvotes: 0
Views: 365
Reputation: 21
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 = '[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);
}
Upvotes: 2