[英]Bounce <nobody@gmail.com> Gmail API sending failure
我正在嘗試使用Google API發送電子郵件。 我能夠閱讀電子郵件,根據快速入門說明使用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電子郵件。 所以,如果你有“測試消息或某些東西”,你應該有一個像這樣的字符串:
To: someguy@gmail.com
From: myaddress@gmail.com
Subject: this is my cool subject
here's a text/plain email, neato?
記得\\r\\n
之間有\\r\\n
,在標題和正文之間有兩個。 對於php來說, PEAR :: Mail_Mime似乎是構建這樣的rfc822 MIME電子郵件消息字符串的好庫。
有關詳細信息,請參閱發送電子郵件 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.