退回 <nobody@gmail.com> Gmail API 发送失败
Bounce <nobody@gmail.com> Gmail API sending failure
我正在尝试使用 google API 发送电子邮件。我能够阅读电子邮件,根据 quickstart 说明使用 client_secret.json
文件通过宣誓进行身份验证。我几乎可以正常发送电子邮件,但无法成功发送。
我的电子邮件退回了,我无法指定要发送到的电子邮件(因此是 nobody@gmail.com
地址)。
下面的代码大部分都有效。我已经注释掉了有效的部分:(阅读电子邮件)。
代码:
<?php
require 'google-api-php-client/src/Google/autoload.php';
define('APPLICATION_NAME', 'Gmail API Quickstart');
define('CREDENTIALS_PATH', '~/.credentials/gmail-api-quickstart.json');
define('CLIENT_SECRET_PATH', 'client_secret.json');
define('SCOPES', implode(' ', array(
Google_Service_Gmail::MAIL_GOOGLE_COM,
Google_Service_Gmail::GMAIL_COMPOSE)
));
/**
* 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->setAuthConfigFile(CLIENT_SECRET_PATH);
$client->setAccessType('offline');
// Load previously authorized credentials from a file.
$credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
if (file_exists($credentialsPath)) {
$accessToken = file_get_contents($credentialsPath);
} 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->authenticate($authCode);
// Store the credentials to disk.
if(!file_exists(dirname($credentialsPath))) {
mkdir(dirname($credentialsPath), 0700, true);
}
file_put_contents($credentialsPath, $accessToken);
printf("Credentials saved to %s\n", $credentialsPath);
}
$client->setAccessToken($accessToken);
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->refreshToken($client->getRefreshToken());
file_put_contents($credentialsPath, $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);
$msg = new Google_Service_Gmail_Message();
$mime = rtrim(strtr(base64_encode("TEST MESSAGE OR SOMETHING"), '+/', '-_'), '=');
$msg->setRaw($mime);
// Print the labels in the user's account.
$userId = 'me';
function sendMessage($service, $userId, $message) {
try {
$message = $service->users_messages->send($userId, $message);
print 'Message with ID: ' . $message->getId() . ' sent.';
// <----------- GET'S HERE AND THIS WORKS
return $message;
} catch (Exception $e) {
print 'An error occurred: ' . $e->getMessage();
}
}
try {
sendMessage($service, $userId, $msg);
} catch (Exception $Ex ) {
echo $Ex->getMessage();
}
电子邮件回复(在我的 Gmail 收件箱中):
An error occurred. Your message was not sent.
TEST MESSAGE OR SOMETHING Date: Tue, 21 Jul 2015 14:51:12 -0700 Message-Id: <
我什至不确定如何指定目标地址,例如 personimsendingto@gmail.com
。我真的找不到很多文档。如果有人可以帮助我或指出正确的方向,我将不胜感激。
您需要将完整的 RFC822 电子邮件信息放入原始字段。所以在你有 "TEST MESSAGE OR SOMETHING" 的地方你应该有一个像这样的字符串:
To: someguy@gmail.com
From: myaddress@gmail.com
Subject: this is my cool subject
here's a text/plain email, neato?
记得在行之间有 \r\n
,在 headers 和 body 之间有两个。 php 看来 PEAR::Mail_Mime 是构建此类 rfc822 MIME 电子邮件消息字符串的好库。
有关详细信息,请参阅 Sending Email。
我正在尝试使用 google API 发送电子邮件。我能够阅读电子邮件,根据 quickstart 说明使用 client_secret.json
文件通过宣誓进行身份验证。我几乎可以正常发送电子邮件,但无法成功发送。
我的电子邮件退回了,我无法指定要发送到的电子邮件(因此是 nobody@gmail.com
地址)。
下面的代码大部分都有效。我已经注释掉了有效的部分:(阅读电子邮件)。
代码:
<?php
require 'google-api-php-client/src/Google/autoload.php';
define('APPLICATION_NAME', 'Gmail API Quickstart');
define('CREDENTIALS_PATH', '~/.credentials/gmail-api-quickstart.json');
define('CLIENT_SECRET_PATH', 'client_secret.json');
define('SCOPES', implode(' ', array(
Google_Service_Gmail::MAIL_GOOGLE_COM,
Google_Service_Gmail::GMAIL_COMPOSE)
));
/**
* 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->setAuthConfigFile(CLIENT_SECRET_PATH);
$client->setAccessType('offline');
// Load previously authorized credentials from a file.
$credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
if (file_exists($credentialsPath)) {
$accessToken = file_get_contents($credentialsPath);
} 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->authenticate($authCode);
// Store the credentials to disk.
if(!file_exists(dirname($credentialsPath))) {
mkdir(dirname($credentialsPath), 0700, true);
}
file_put_contents($credentialsPath, $accessToken);
printf("Credentials saved to %s\n", $credentialsPath);
}
$client->setAccessToken($accessToken);
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->refreshToken($client->getRefreshToken());
file_put_contents($credentialsPath, $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);
$msg = new Google_Service_Gmail_Message();
$mime = rtrim(strtr(base64_encode("TEST MESSAGE OR SOMETHING"), '+/', '-_'), '=');
$msg->setRaw($mime);
// Print the labels in the user's account.
$userId = 'me';
function sendMessage($service, $userId, $message) {
try {
$message = $service->users_messages->send($userId, $message);
print 'Message with ID: ' . $message->getId() . ' sent.';
// <----------- GET'S HERE AND THIS WORKS
return $message;
} catch (Exception $e) {
print 'An error occurred: ' . $e->getMessage();
}
}
try {
sendMessage($service, $userId, $msg);
} catch (Exception $Ex ) {
echo $Ex->getMessage();
}
电子邮件回复(在我的 Gmail 收件箱中):
An error occurred. Your message was not sent.
TEST MESSAGE OR SOMETHING Date: Tue, 21 Jul 2015 14:51:12 -0700 Message-Id: <
我什至不确定如何指定目标地址,例如 personimsendingto@gmail.com
。我真的找不到很多文档。如果有人可以帮助我或指出正确的方向,我将不胜感激。
您需要将完整的 RFC822 电子邮件信息放入原始字段。所以在你有 "TEST MESSAGE OR SOMETHING" 的地方你应该有一个像这样的字符串:
To: someguy@gmail.com
From: myaddress@gmail.com
Subject: this is my cool subject
here's a text/plain email, neato?
记得在行之间有 \r\n
,在 headers 和 body 之间有两个。 php 看来 PEAR::Mail_Mime 是构建此类 rfc822 MIME 电子邮件消息字符串的好库。
有关详细信息,请参阅 Sending Email。