php 使用SendGrid发送电子邮件

lsmd5eda  于 2023-02-03  发布在  PHP
关注(0)|答案(1)|浏览(222)

我目前正在遵循SendGrid发布的关于如何发送基本电子邮件的教程。该教程在这里发送电子邮件教程的"你好电子邮件"部分提供。
除此之外,我还遵循了SendGrid自己建议的YouTube Tutorial**。
我已经逐字逐句地遵循了教程,目前正在使用wamp64服务器作为本地主机。
1.我已经创建了一个没有问题的config.json文件,按照YouTube教程的指示,将其保存到wamp64服务器,如下所示:

<?php

define('SENDGRID_API_KEY', '<my api key>');

1.我已经创建了一个composer.php文件,按照YouTube教程的指示,将其保存到wamp64服务器,如下所示:

{

    "require":{

        "sendgrid/sendgrid": "~7"

    }

}

1.我已经按照YouTube教程的指示,通过Composer下载了相关文件,没有任何问题。
1.然后我创建了一个php文件,命名为test-email.php,并将其保存到wamp64服务器,代码如下所示:

<?php  

require_once 'config.php';
require 'vendor/autoload.php'; 

$email = new \SendGrid\Mail\Mail(); 
$email->setFrom("test@example.com", "Example User"); //applying my email
$email->setSubject("Sending with SendGrid is Fun");
$email->addTo("test@example.com", "Example User");//applying my email
$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('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";
}

?>

我的文件目录设置如下:
Screenshot of file directory setup
1.然后我运行http://localhost/sendgrid/sendgrid/test-email.php并按回车键,得到错误消息:

401 Array ( [0] => HTTP/1.1 401 Unauthorized [1] => Server: nginx [2] => Date: Wed, 01 Feb 2023 15:33:59 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] => Strict-Transport-Security: max-age=600; includeSubDomains [12] => [13] => ) {"errors":[{"message":"Permission denied, wrong credentials","field":null,"help":null}]}

现在我的问题是:我做错了什么,我在这里错过了什么?没有教程提到任何关于验证SSL认证或创建环境的内容?
我尝试过用下面的代码修改config.json来创建一个环境变量:

echo "export SENDGRID_API_KEY='YOUR_API_KEY'" > sendgrid.env //using my API Key
echo "sendgrid.env" >> .gitignore
source ./sendgrid.env

然而,我得到了下面的错误消息Parse error: syntax error, unexpected 'echo' (T_ECHO), expecting ';' or ',' in C:\wamp64\www\sendgrid\SendGrid\config.php on line 6,考虑到文件中甚至没有第6行,我真的不明白,而且我已经复制并粘贴了确切的代码-除了插入我自己的API密钥-所以我能想到的唯一解释是SendGrid需要改变他们的指令。
我试过加上";"或"",但无济于事。
我错过了什么?

lbsnaicq

lbsnaicq1#

我看到你使用new \SendGrid('SENDGRID_API_KEY');来访问你定义的常量。这是行不通的,你现在访问的是一个包含 “SENDGRID_API_KEY” 的常量。尝试:new \SendGrid(SENDGRID_API_KEY);不带引号。就像这样:

<?php  

require_once 'config.php';
require 'vendor/autoload.php'; 

$email = new \SendGrid\Mail\Mail(); 
$email->setFrom("test@example.com", "Example User"); //applying my email
$email->setSubject("Sending with SendGrid is Fun");
$email->addTo("test@example.com", "Example User");//applying my email
$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(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";
}

?>

相关问题