I am supposed to replace the mailing service from an old project to GMail. However, the project is using PHP and the documentation for the PHP code is not yet available in the google documentation page. I wanted to test sending an e-mail with a simple mail body, here is the code:
require_once __DIR__."/google-api-php-client-2.2.1/vendor/autoload.php";
define("APPLICATION_NAME", "Gmail API PHP Quickstart");
define("CREDENTIALS_PATH", "~/.credentials/gmail-php-quickstart.json");
define("CLIENT_SECRET_PATH", __DIR__."/client_secret.json");
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/gmail-php-quickstart.json
define('SCOPES', implode(' ', array(
Google_Service_Gmail::GMAIL_READONLY)
));
date_default_timezone_set('America/New_York'); // Prevent DateTime tz exception
/*
if (php_sapi_name() != 'cli') {
throw new Exception('This application must be run on the command line.');
}
*/
/**
* Returns an authorized API client.
* @return Google_Client the authorized client object
*/
function getClient() {
$client = new Google_Client();
$client->setApplicationName(APPLICATION_NAME);
$client->setScopes(SCOPES);
$client->setAuthConfig(CLIENT_SECRET_PATH);
$client->setAccessType('offline');
// Load previously authorized credentials from a file.
$credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
if (file_exists($credentialsPath)) {
$accessToken = json_decode(file_get_contents($credentialsPath), true);
}
else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:\n%s\n", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
// Store the credentials to disk.
if(!file_exists(dirname($credentialsPath))) {
mkdir(dirname($credentialsPath), 0700, true);
}
file_put_contents($credentialsPath, json_encode($accessToken));
printf("Credentials saved to %s\n", $credentialsPath);
}
$client->setAccessToken($accessToken);
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
}
return $client;
}
/**
* Expands the home directory alias '~' to the full path.
* @param string $path the path to expand.
* @return string the expanded path.
*/
function expandHomeDirectory($path) {
$homeDirectory = getenv('HOME');
if (empty($homeDirectory)) {
$homeDirectory = getenv('HOMEDRIVE') . getenv('HOMEPATH');
}
return str_replace('~', realpath($homeDirectory), $path);
}
// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Gmail($client);
// Print the labels in the user's account.
$user = 'me';
$message = new Google_Service_Gmail_Message();
$messagePart = new Google_Service_Gmail_MessagePart();
$messagePartBody = new Google_Service_Gmail_MessagePartBody();
$messagePartHeader = new Google_Service_Gmail_MessagePartHeader();
$messagePartBody->setData("TEST BY FAROUK");
$messagePart->setHeader($messagePartHeader);
$messagePart->setBody($messagePartBody);
$message->setPayLoad($messagePart);
$service->users_messages->send($user, $message, null);
The problem lies in this part, which is not finished yet:
$user = 'me';
$message = new Google_Service_Gmail_Message();
$messagePart = new Google_Service_Gmail_MessagePart();
$messagePartBody = new Google_Service_Gmail_MessagePartBody();
$messagePartHeader = new Google_Service_Gmail_MessagePartHeader();
$messagePartBody->setData("TEST BY ME");
$messagePart->setHeader($messagePartHeader);
$messagePart->setBody($messagePartBody);
$message->setPayLoad($messagePart);
$service->users_messages->send($user, $message, null);
I know I have to create object of those classes but, I don't know what kind of data I should set in the parameter using the setters of them.
Please give a simple code to send "Hello world!" to an e-mail address like foo@gmail.com. Thanks!