使用gmail api php发送邮件

时间:2018-06-06 16:22:11

标签: php email oauth-2.0 gmail-api

我一直在搜索Google文档,使用gmail oauth 2凭据发送邮件。我刚在console.developers.google中创建了一个新项目,并使用gmail api启用了gmail。我创建了oauth凭据,甚至在同意屏幕中配置了它们。但是每当我在服务器上运行我的php文件时,我都无法发送邮件,只是得到以下响应。

An error occurred: {
"error": {
"errors": [
{
 "domain": "global",
 "reason": "insufficientPermissions",
 "message": "Insufficient Permission"
}
],
"code": 403,
"message": "Insufficient Permission"
}
}

我尝试执行以下代码

<?php
require __DIR__ . '/vendor/autoload.php';
function getClient()
{
$client = new Google_Client();
$client->setApplicationName('Send mail');
$client->setSubject('Verification mail');
$client->setScopes(Google_Service_Gmail::GMAIL_READONLY);
$client->setAuthConfig('client_secret.json');
$client->setAccessType(["https://www.googleapis.com/auth/gmail.compose"]);
$credentialsPath = expandHomeDirectory('credentials.json');
if (file_exists($credentialsPath)) {
    $accessToken = json_decode(file_get_contents($credentialsPath), true);
} else {
    $authUrl = $client->createAuthUrl();
    printf("Open the following link in your browser:\n%s\n", $authUrl);
    print 'Enter verification code: ';
    $authCode = trim(fgets(STDIN));
    $accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
    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);
if ($client->isAccessTokenExpired()) {
    $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
    file_put_contents($credentialsPath,     json_encode($client->getAccessToken()));
}
return $client;
}
function expandHomeDirectory($path)
{
$homeDirectory = getenv('HOME');
if (empty($homeDirectory)) {
    $homeDirectory = getenv('HOMEDRIVE') . getenv('HOMEPATH');
}
return str_replace('~', realpath($homeDirectory), $path);
}
$client = getClient();
$service = new Google_Service_Gmail($client);
try {
    $strSubject = "Verificaion mail";
    $strRawMessage = "From: Me<rammanojpotla1608@gmail.com>\r\n";
    $strRawMessage .= "To: manoj<rammanoj.potla@gmail.com>\r\n";
    $strRawMessage .= "CC: rammanoj<rammanojpotla1608@gmail.com>\r\n";
    $strRawMessage .= "Subject: =?utf-8?B?" . base64_encode($strSubject) .     "?=\r\n";
    $strRawMessage .= "MIME-Version: 1.0\r\n";
    $strRawMessage .= "Content-Type: text/html; charset=utf-8\r\n";
    $strRawMessage .= "Content-Transfer-Encoding: base64" . "\r\n\r\n";
    $strRawMessage .= "A simple verification mail!" . "\r\n";
    $mime = rtrim(strtr(base64_encode($strRawMessage), '+/', '-_'), '=');
    $msg = new Google_Service_Gmail_Message();
    $msg->setRaw($mime);
    $service->users_messages->send("me", $msg);
} catch (Exception $e) {
    print "An error occurred: " . $e->getMessage();
}
?>

任何人都可以提供上述代码的更好解释以及错误原因。谢谢!

2 个答案:

答案 0 :(得分:0)

我猜错误是由于我更改了访问类型时的访问类型,它给了我另一个错误。我想它会引发错误,说没有权限访问您的帐户。这可能是由于在crdentials.json文件中提供了错误的访问令牌,或者可能是因为访问令牌过期。但是最小化上面的代码。我尝试了解并写了一个简单的。

<?php
require __DIR__ . '/vendor/autoload.php';
 $client = new Google_Client();
 $client->setScopes(Google_Service_Gmail::GMAIL_COMPOSE  );
 $client->setAuthConfig('client_secret.json');
 $credentialsPath = __DIR__.'/credentials.json';
 if (file_exists($credentialsPath)) {
    $accessToken = json_decode(file_get_contents($credentialsPath), true);
 } else {
    $authUrl = $client->createAuthUrl();
    printf("Open the following link in your browser:\n%s\n", $authUrl);
    print 'Enter verification code: ';
    $authCode = trim(fgets(STDIN));
    $accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
    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);
 if ($client->isAccessTokenExpired()) {
    $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
    file_put_contents($credentialsPath,   json_encode($client->getAccessToken()));
}
$service = new Google_Service_Gmail($client);
try {
    $strSubject = "Verificaion mail";
    $strRawMessage = "From: Me<rammanojpotla1608@gmail.com>\r\n";
    $strRawMessage .= "To: manoj<rammanoj.potla@gmail.com>\r\n";
    $strRawMessage .= "CC: rammanoj<rammanojpotla1608@gmail.com>\r\n";
    $strRawMessage .= "Subject: =?utf-8?B?" . base64_encode($strSubject) .     "?=\r\n";
    $mime = rtrim(strtr(base64_encode($strRawMessage), '+/', '-_'), '=');
    $msg = new Google_Service_Gmail_Message();
    $msg->setRaw($mime);
    $service->users_messages->send("me", $msg);
    echo "mail sent";
} catch (Exception $e) {
    print "An error occurred: " . $e->getMessage();
}

?>

Google_client会创建一个新的Google客户端。需要使用指定或给予的访问权限来定义要执行的操作类型。最初,它会检查名为credentials.json的文件是否存在。如果它在那里,它访问用户的access_token并执行所需的操作。我的操作是使用gmail api发送邮件。因此,我创建了一个新的Google_Service_Gmail类来执行它。如果没有credentials.json文件,则它将访问包含client_id和client_secrent的client_secret.json文件。使用它们都可以生成访问令牌。

答案 1 :(得分:0)

确保在授权gmail服务客户端时指定了多个不同的范围。您可以找到列表here

有关错误的进一步讨论,请参阅此SO post