【发布时间】:2018-08-20 07:55:13
【问题描述】:
我应该将邮件服务从旧项目替换为 GMail。但是,该项目正在使用 PHP,并且 PHP 代码的文档在 google 文档页面中尚不可用。我想测试发送一封带有简单邮件正文的电子邮件,代码如下:
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);
问题出在这部分,还没写完:
$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);
我知道我必须创建这些类的对象,但是我不知道应该使用它们的设置器在参数中设置什么样的数据。
请给出一个简单的代码来发送“Hello world!”发送到 foo@gmail.com 等电子邮件地址。谢谢!
【问题讨论】:
-
请查看此链接:w3schools.com/php/func_mail_mail.asp 并使用邮件功能。这是最简单的电子邮件发送方式。
-
@LalbhusanYadav,客户希望我们使用 GMail API。
-
@Logos 这个 API 到底有什么未记录的? PHP 参考:developers.google.com/resources/api-libraries/documentation/… 以及创建和发送电子邮件的整体教程:developers.google.com/gmail/api/guides/sending
-
@Logos 老实说,我不知道有什么不清楚的地方。此外,本教程是用 Java 编写的,并不意味着您不能将其重写为 PHP。看一下教程说的,看看能不能在PHP客户端找到对应的类。谷歌是您可以信赖的公司之一,可以让他们的客户保持统一。 Java 是一种不同的语法,但这可能是唯一的区别。不要成为那些“它不是用我最喜欢的语言编写的,所以它不可能工作”的程序员之一;)
标签: php email google-api gmail