问题描述
我正在尝试开发一个通过Gmail API发送电子邮件的网络应用程序。但是我遇到了这个错误
致命错误:未捕获错误:在/var/www/html/wordpress/multi_users/approval.PHP中找不到类“ Google_Service_Gmail_Message”:18堆栈跟踪:#0 {main}抛出在/ var / www / html / wordpress中/multi_users/approval.PHP,第18行
<?PHP
// include your composer dependencies
require_once '/../../gmail/vendor/autoload.PHP';
// ERRORS
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(E_ALL);
$user = 'me';
$strSubject = 'Test mail using GMail API' . date('M d,Y h:i:s A');
$strRawMessage = "From: myAddress<[email protected]>\r\n";
$strRawMessage .= "To: toAddress <[email protected]>\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: quoted-printable' . "\r\n\r\n";
$strRawMessage .= "this <b>is a test message!\r\n";
// The message needs to be encoded in Base64URL
$mime = rtrim(strtr(base64_encode($strRawMessage),'+/','-_'),'=');
$msg = new Google_Service_Gmail_Message();
$msg->setRaw($mime);
//The special value **me** can be used to indicate the authenticated user.
$service->users_messages->send("me",$msg);
我发现了上面发送电子邮件的代码,但是我确定它缺少一些行。 该API刚刚已安装,并且可以正常工作。 错误来自此行: $ msg = new Google_Service_Gmail_Message();
解决方法
尝试在项目文件夹的终端中运行以下命令: 作曲家转储自动加载
致谢
,解决方案
您缺少Gmail service
设置。借助@Eliasu帮助修复了依赖性后,您将必须插入代码来构建和设置Gmail服务。
// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Gmail($client);
getClient()
函数将取决于您如何运行此应用程序:
如果它是服务器应用程序,则需要一个Service Account
客户端,否则,您可以使用Gmail API PHP Quickstart中发布的getClient()
函数。