第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Gmail API - PHP - 使用服務(wù)帳戶發(fā)送電子郵件

Gmail API - PHP - 使用服務(wù)帳戶發(fā)送電子郵件

PHP
烙印99 2023-05-12 16:11:36
問題:我一直遇到 HTTP 400 錯(cuò)誤,指出“先決條件檢查失敗”。每當(dāng)我調(diào)用 sendMessage() 方法時(shí)。我不知道我有什么可以考慮的:?jiǎn)⒂?Gmail API。創(chuàng)建了一個(gè)服務(wù)帳戶。設(shè)置全域委派(針對(duì) G-Suites)。為服務(wù)啟用域范圍的委派。請(qǐng)注意,我已成功運(yùn)行 quickstart.php,因此我知道谷歌庫(kù)已正確安裝。下面是我的代碼:<?php require_once('../../vendor/autoload.php');$client = new Google_Client();$credentials_file = '../../vendor/google/auth/credentials.json';$client->setAuthConfig($credentials_file);$client->setApplicationName("no-reply mailing");$client->setScopes(['https://www.googleapis.com/auth/gmail.send']);$service = new Google_Service_Gmail($client);$message = createMessage('me', 'some@email.com', 'This is but a test', 'Please work...');// Email a usersendMessage($service, 'me', $message);/*** @param $sender string sender email address* @param $to string recipient email address* @param $subject string email subject* @param $messageText string email text* @return Google_Service_Gmail_Message*/function createMessage($sender, $to, $subject, $messageText) { $message = new Google_Service_Gmail_Message(); $rawMessageString = "From: <{$sender}>\r\n"; $rawMessageString .= "To: <{$to}>\r\n"; $rawMessageString .= 'Subject: =?utf-8?B?' . base64_encode($subject) . "?=\r\n"; $rawMessageString .= "MIME-Version: 1.0\r\n"; $rawMessageString .= "Content-Type: text/html; charset=utf-8\r\n"; $rawMessageString .= 'Content-Transfer-Encoding: quoted-printable' . "\r\n\r\n"; $rawMessageString .= "{$messageText}\r\n"; $rawMessage = strtr(base64_encode($rawMessageString), array('+' => '-', '/' => '_')); $message->setRaw($rawMessage); return $message;}function sendMessage($service, $userId, $message) {  try {    $message = $service->users_messages->send($userId, $message);    print 'Message with ID: ' . $message->getId() . ' sent.';    return $message;  } catch (Exception $e) {    print 'An error occurred: ' . $e->getMessage();  }}?>任何幫助是極大的贊賞!
查看完整描述

1 回答

?
隔江千里

TA貢獻(xiàn)1906條經(jīng)驗(yàn) 獲得超10個(gè)贊

開始編碼之前的要求。

  1. 獲取 Google Client Library(確保 php 在您的系統(tǒng)路徑中 > Install?Composer?> Install the library?composer require google/apiclient:^2.0

  2. 啟用 Gmail API(Google Developer console?> Library > Search 'Gmail API' > Enable(如果已啟用,您將看到管理按鈕)

  3. 創(chuàng)建服務(wù)帳戶(Google Developer console?> IAM & Admin > Service Accounts > 點(diǎn)擊“Create Service Account” > 像往常一樣填寫第 1 步 > 對(duì)于第 2 步,我將我的服務(wù)帳戶設(shè)置為項(xiàng)目所有者。 > 第 3 步我跳過了。)

  4. 創(chuàng)建一個(gè)密鑰(谷歌開發(fā)者控制臺(tái)> IAM 和管理 > 服務(wù)賬戶 > 點(diǎn)擊你新創(chuàng)建的服務(wù)賬戶的“操作”菜單 > 創(chuàng)建密鑰 > JSON > 將它存儲(chǔ)在你的代碼可以訪問的地方。)

  5. 設(shè)置全域委派(Google 管理員> 安全 > API 權(quán)限 > 一直向下滾動(dòng)以找到“管理全域委派” > 點(diǎn)擊“添加新” > 輸入在您剛剛下載的 json 文件中找到的客戶端 ID > 輸入在您需要的范圍內(nèi)。要通過 gmail 發(fā)送電子郵件,請(qǐng)查看此處的“授權(quán)”。)

  6. 啟用全域委派(Google Developer console?> IAM & Admin > Service Accounts > 單擊新創(chuàng)建的服務(wù)帳戶 > 單擊“編輯”> Show Domain-wide Delegation > Enable G-Suite Domain-wide Delegation)

如果您已遵循并完成了這些步驟,那么您就可以繼續(xù)執(zhí)行代碼部分了!

代碼本身。

<?php

// Library obtained from https://developers.google.com/gmail/api/quickstart/php

require_once('../../vendor/autoload.php');


// Some user within your G-Suites domain

$user_to_impersonate = "your@domain.com";


$sender = $user_to_impersonate;

$to = 'another@domain.com';

$subject = 'The subject of an email.';

$messageText = 'Finally this works!';


// The path to your service account credentials goes here.

putenv("GOOGLE_APPLICATION_CREDENTIALS=credentials.json");

$client = new Google_Client();

$client->useApplicationDefaultCredentials();

$client->setSubject($sender);

$client->setApplicationName("Quickstart");

$client->setScopes(["https://mail.google.com/",

? ? ? ? ? ? ? ? ? ? "https://www.googleapis.com/auth/gmail.compose",

? ? ? ? ? ? ? ? ? ? "https://www.googleapis.com/auth/gmail.modify",

? ? ? ? ? ? ? ? ? ? "https://www.googleapis.com/auth/gmail.send"]);

$service = new Google_Service_Gmail($client);


// Main Process

try {

? $msg = createMessage($sender, $to, $subject, $messageText);

? sendMessage($service, $sender, $msg);

} catch (Exception $e) {

? print "An error occurred: " . $e->getMessage();

}


function sendMessage($service, $sender, $msg) {

? $service->users_messages->send($sender, $msg);

}


function createMessage($sender, $to, $subject, $messageText) {

? $rawMsgStr = "From: <{$sender}>\r\n";

? $rawMsgStr .= "To: <{$to}>\r\n";

? $rawMsgStr .= 'Subject: =?utf-8?B?' . base64_encode($subject) . "?=\r\n";

? $rawMsgStr .= "MIME-Version: 1.0\r\n";

? $rawMsgStr .= "Content-Type: text/html; charset=utf-8\r\n";

? $rawMsgStr .= 'Content-Transfer-Encoding: quoted-printable' . "\r\n\r\n";

? $rawMsgStr .= "{$messageText}\r\n";


? // The message needs to be encoded in Base64URL

? $mime = rtrim(strtr(base64_encode($rawMsgStr), '+/', '-_'), '=');

? $msg = new Google_Service_Gmail_Message();

? $msg->setRaw($mime);

? return $msg;

}

??>


查看完整回答
反對(duì) 回復(fù) 2023-05-12
  • 1 回答
  • 0 關(guān)注
  • 527 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)