Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gmail - sending email #376

Closed
mikehudak opened this issue Nov 1, 2014 · 1 comment

Comments

Projects
None yet
2 participants
@mikehudak
Copy link

commented Nov 1, 2014

I am trying to send email with this code:

require_once ‘Google/Client.php';
require_once ‘Google/Service/Gmail.php';
// service account
$service_account_name = ‘SERVICE_EMAIL'; //Email Address
$key_file_location = ‘key.p12′; //key.p12
$client = new Google_Client(array(‘use_objects’ => true));
if (isset($_SESSION['service_token'])) {
$client->setAccessToken($_SESSION['service_token']);
}
$key = file_get_contents($key_file_location);
$scopes = array(
‘https://mail.google.com’,
‘https://www.googleapis.com/auth/gmail.compose’,
‘https://www.googleapis.com/auth/gmail.modify’,
);
$cred = new Google_Auth_AssertionCredentials($service_account_name, $scopes, $key);
$client->setAssertionCredentials($cred);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();
$service = new Google_Service_Gmail($client);
try {
//$message = '<html><body><p style="color: red;">Hello World!</p></body></html>';
$message = 'Hello World!';
// The message needs to be encoded in Base64URL
$mime = rtrim(strtr(base64_encode($message), ‘+/’, ‘-_’), ‘=’);
$msg = new Google_Service_Gmail_Message();
$msg->setRaw($mime);
$service->users_messages->send(“me”, $msg);
} catch (Exception $e) {
print($e->getMessage());
}

It keeps failing with error:

``
Error calling POST https://www.googleapis.com/gmail/v1/users/me/messages/send: (500) Backend Error


Thank you for your help.
@ianbarber

This comment has been minimized.

Copy link
Contributor

commented Nov 7, 2014

Looks OK to me (assuming that gmail supports sending from service accounts) - one thing I am aware of is that you may need to use the URLsafe base 64 encode: https://github.com/google/google-api-php-client/blob/master/src/Google/Utils.php#L26 rather than regular base64_encode.

This tracker is for issues with the library itself though - your best bet for gmail specific questions is the Stack Overflow tag: https://stackoverflow.com/questions/tagged/gmail-api

@ianbarber ianbarber closed this Nov 7, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.