我使用sendgrid PHP發送電子郵件。這是我的代碼:Sendgrid:權限被拒絕,錯誤憑據
require("sendgrid-php.php");
$from = new SendGrid\Email("Example User", "[email protected]");
$subject = "Sending with SendGrid is Fun";
$to = new SendGrid\Email("Example User", "[email protected]");
$content = new SendGrid\Content("text/plain", "and easy to do anywhere, even with PHP");
$mail = new SendGrid\Mail($from, $subject, $to, $content);
$apiKey = getenv('SG.key......');
$sg = new \SendGrid($apiKey);
$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();
echo '<pre>';
print_r($response->headers());
echo '</pre>';
echo $response->body();
當運行上述代碼它顯示以下錯誤消息我:
401陣列( [0] => HTTP/1.1 401未授權 [1] =>服務器:nginx =>日期:2017年5月29日星期一17:52:44 GMT [3] => Content-Type:application/json [4] =>內容長度:88 [5] =>連接:保持活動 [6] => X-Frame-Options:DENY [7] => Access-Control-Allow-來源:https://sendgrid.api-docs.io =>訪問控制 - 允許 - 方法:POST [9] =>訪問控制允許標題:授權,內容類型,代表,x-sg-elas- acl => Access-Control-Max-Age:600 [11] => X-No-CORS-Reason:https://sendgrid.com/docs/Classroom/Basics/API/cors.html [12] => [13] =>){「errors」 {「message」:「Permission denied,wrong credentials」,「field」:null,「help」:null}]}
我不明白爲什麼我得到權限相關的錯誤,因爲我剛創建API密鑰。
[使用sendgrid發送郵件中的憑據錯誤]的完全重複(https://stackoverflow.com/questions/38368968/wrong-credentials-in-sending-mail-using-sendgrid) – Veve
請閱讀PHP手冊'getenv()':http://php.net/manual/en/function.getenv.php –