当我们有API时发送电子邮件?发送网格



我试图使用sendgrid用php发送电子邮件,但无法完成

我正在使用这个代码

<?php
require 'vendor/autoload.php'; // If you're using Composer (recommended)
// Comment out the above line if not using Composer
// require("<PATH TO>/sendgrid-php.php");
// If not using Composer, uncomment the above line and
// download sendgrid-php.zip from the latest release here,
// replacing <PATH TO> with the path to the sendgrid-php.php file,
// which is included in the download:
// https://github.com/sendgrid/sendgrid-php/releases
$email = new SendGridMailMail(); 
$email->setFrom("test1@example.com", "test1");
$email->setSubject("Subject example");
$email->addTo("test@example.com", "Example");
$email->addContent("text/plain", "and easy to do anywhere, even with PHP");
$email->addContent(
"text/html", "<strong>and easy to do anywhere, even with PHP</strong>"
);
$sendgrid = new SendGrid(getenv('SENDGRID_API_KEY'));
try {
$response = $sendgrid->send($email);
print $response->statusCode() . "n";
print_r($response->headers());
print $response->body() . "n";
} catch (Exception $e) {
echo 'Caught exception: '. $e->getMessage() ."n";
}

我得到这个错误

401 Array ( [0] => HTTP/1.1 401 Unauthorized [1] => Server: nginx [2] => Date: Tue, 30 Oct 2018 08:11:54 GMT [3] => Content-Type: application/json [4] => Content-Length: 88 [5] => Connection: keep-alive [6] => Access-Control-Allow-Origin: https://sendgrid.api-docs.io [7] => Access-Control-Allow-Methods: POST [8] => Access-Control-Allow-Headers: Authorization, Content-Type, On-behalf-of, x-sg-elas-acl [9] => Access-Control-Max-Age: 600 [10] => X-No-CORS-Reason: https://sendgrid.com/docs/Classroom/Basics/API/cors.html [11] => [12] => ) {"errors":[{"message":"Permission denied, wrong credentials","field":null,"help":null}]}

我正在使用这个教程,快速启动,idk该做什么,提前感谢。https://github.com/sendgrid/sendgrid-php

我是Twilio博客PHP团队的成员。

根据提供的错误消息,我猜您要么没有设置SENDGRID_API_KEY环境变量,要么没有将其设置为当前活动的SendGrid API键。请仔细检查它是否设置正确。之后,如果问题仍然存在,请告诉我,我会看看我还能做什么。

相关内容

  • 没有找到相关文章

最新更新