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\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!