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

mikehudak opened this issue Nov 1, 2014 · 1 comment


None yet
2 participants
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'])) {
$key = file_get_contents($key_file_location);
$scopes = array(
$cred = new Google_Auth_AssertionCredentials($service_account_name, $scopes, $key);
if ($client->getAuth()->isAccessTokenExpired()) {
$_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();
$service->users_messages->send(“me”, $msg);
} catch (Exception $e) {

It keeps failing with error:

Error calling POST (500) Backend Error

Thank you for your help.

This comment has been minimized.

Copy link

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: 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:

@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.