The Problem: I'm stuck with an HTTP 400 error that states "Precondition check failed." whenever I call the sendMessage() method.
I don't know what it can be considering that I have:
As a note, I've successfully run quickstart.php so I know that the google library is installed correctly. Below is my code:
<?php
require_once('../../vendor/autoload.php');
$client = new Google_Client();
$credentials_file = '../../vendor/google/auth/credentials.json';
$client->setAuthConfig($credentials_file);
$client->setApplicationName("no-reply mailing");
$client->setScopes(['https://www.googleapis.com/auth/gmail.send']);
$service = new Google_Service_Gmail($client);
$message = createMessage('me', 'some@email.com', 'This is but a test', 'Please work...');
// Email a user
sendMessage($service, 'me', $message);
/**
* @param $sender string sender email address
* @param $to string recipient email address
* @param $subject string email subject
* @param $messageText string email text
* @return Google_Service_Gmail_Message
*/
function createMessage($sender, $to, $subject, $messageText) {
$message = new Google_Service_Gmail_Message();
$rawMessageString = "From: <{$sender}>\r\n";
$rawMessageString .= "To: <{$to}>\r\n";
$rawMessageString .= 'Subject: =?utf-8?B?' . base64_encode($subject) . "?=\r\n";
$rawMessageString .= "MIME-Version: 1.0\r\n";
$rawMessageString .= "Content-Type: text/html; charset=utf-8\r\n";
$rawMessageString .= 'Content-Transfer-Encoding: quoted-printable' . "\r\n\r\n";
$rawMessageString .= "{$messageText}\r\n";
$rawMessage = strtr(base64_encode($rawMessageString), array('+' => '-', '/' => '_'));
$message->setRaw($rawMessage);
return $message;
}
function sendMessage($service, $userId, $message) {
try {
$message = $service->users_messages->send($userId, $message);
print 'Message with ID: ' . $message->getId() . ' sent.';
return $message;
} catch (Exception $e) {
print 'An error occurred: ' . $e->getMessage();
}
}
?>
Any help is greatly appreciated!
I contacted Google support for this and they've solved my issue!
Here's what I was told in the email from support: "You must impersonate the user on the code to be able to call the Gmail API with the Service Account." So the "Precondition check failed." error was caused by not authenticating properly.
So for completeness, I'll run through the process you must go through to allow your code to work. Note that you will need three things: G-Suites, access to the Google developer console and access to the G-Suites admin console.
composer require google/apiclient:^2.0
)If you have followed and completed these steps, you are all set to continue to the code portion!
<?php
// Library obtained from https://developers.google.com/gmail/api/quickstart/php
require_once('../../vendor/autoload.php');
// Some user within your G-Suites domain
$user_to_impersonate = "your@domain.com";
$sender = $user_to_impersonate;
$to = 'another@domain.com';
$subject = 'The subject of an email.';
$messageText = 'Finally this works!';
// The path to your service account credentials goes here.
putenv("GOOGLE_APPLICATION_CREDENTIALS=credentials.json");
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setSubject($sender);
$client->setApplicationName("Quickstart");
$client->setScopes(["https://mail.google.com/",
"https://www.googleapis.com/auth/gmail.compose",
"https://www.googleapis.com/auth/gmail.modify",
"https://www.googleapis.com/auth/gmail.send"]);
$service = new Google_Service_Gmail($client);
// Main Process
try {
$msg = createMessage($sender, $to, $subject, $messageText);
sendMessage($service, $sender, $msg);
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
function sendMessage($service, $sender, $msg) {
$service->users_messages->send($sender, $msg);
}
function createMessage($sender, $to, $subject, $messageText) {
$rawMsgStr = "From: <{$sender}>\r\n";
$rawMsgStr .= "To: <{$to}>\r\n";
$rawMsgStr .= 'Subject: =?utf-8?B?' . base64_encode($subject) . "?=\r\n";
$rawMsgStr .= "MIME-Version: 1.0\r\n";
$rawMsgStr .= "Content-Type: text/html; charset=utf-8\r\n";
$rawMsgStr .= 'Content-Transfer-Encoding: quoted-printable' . "\r\n\r\n";
$rawMsgStr .= "{$messageText}\r\n";
// The message needs to be encoded in Base64URL
$mime = rtrim(strtr(base64_encode($rawMsgStr), '+/', '-_'), '=');
$msg = new Google_Service_Gmail_Message();
$msg->setRaw($mime);
return $msg;
}
?>