php 当我们有API时发送电子邮件?Sendgrid

ibps3vxo  于 2023-05-05  发布在  PHP
关注(0)|答案(1)|浏览(124)

我试图发送一封电子邮件与php使用sendgrid和我不能这样做
我在用这个密码

<?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 \SendGrid\Mail\Mail(); 
$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

eufgjt7s

eufgjt7s1#

我是Twilio博客PHP团队的成员。
根据提供的错误消息,我猜您可能没有设置SENDGRID_API_KEY环境变量,或者没有将其设置为当前活动的SendGrid API密钥。请仔细检查它是否已设置且正确。之后,如果问题仍然存在,请告诉我,我会看看我还能做些什么。

相关问题