简体   繁体   English

SendGrid API 405 尝试发送 email 时方法不允许

[英]SendGrid API 405 Method Not Allowed when trying to send an email

EDIT 3: This 405 error has something to do with my IIS Express dev environment.编辑 3:这个 405 错误与我的 IIS Express 开发环境有关。 I managed to get everything working on my azure web app.我设法让我的 azure web 应用程序上的一切正常工作。

EDIT 2: Duh.编辑2:呃。 I didnt have the environment variable set.我没有设置环境变量。

EDIT: I have made some progress thanks to you helpful fellows.编辑:感谢您的帮助,我取得了一些进展。 Seems I have an authentication problem now even though I am using a fresh API key from SendGrid.即使我使用来自 SendGrid 的新 API 密钥,我现在似乎也遇到了身份验证问题。 I am current getting this error:我目前收到此错误:

 401 Array ( [0] => HTTP/1.1 401 Unauthorized [1] => Server: nginx [2] => Date: Sun, 17 Nov 2019 20:50:16 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}]}

I have a simple button to call a test php file and am working on getting SendGrid for my website.我有一个简单的按钮来调用测试 php 文件,并且正在为我的网站获取 SendGrid。

 <form method="post" action="php/send_form_email.php"> <input type="submit" value="click on me!"> </form>

Then here is the send_form_email.php file:然后这里是 send_form_email.php 文件:

 <?php // You need to install the sendgrid client library so run: // composer require sendgrid/sendgrid try { require 'D:/home/site/wwwroot/vendor/autoload.php'; } catch (Exception $e) { echo 'exception'. '<br>'; var_dump($e); } $email = new \SendGrid\Mail\Mail(); $email->setFrom("test@example.com", "Example User"); $email->setSubject("Sending with SendGrid is Fun"); $email->addTo("test@example.com", "Example User"); $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('API_KEY_HERE')); 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"; }?>

I installed any composer/sendgrid/curl that I have come across successfully on both my machine and my azure web app.我在我的机器和我的 azure web 应用程序上安装了我成功遇到的任何 composer/sendgrid/curl。

Any help is appreciated.任何帮助表示赞赏。 Thanks.谢谢。

This is not the solution but help for debug:这不是解决方案,而是调试帮助:

Replace:代替:

$response = $sg->client->mail()->send()->post($mail);

if ($response->statusCode() == 202) {
 // Successfully sent
 echo 'done';
} else {
 echo 'false';
}

By经过

try {
    $response = $sg->client->mail()->send()->post($mail);
    if ($response->statusCode() == 202) {
       echo 'done';
    } else {

        echo 'false' . '<br>';
        var_dump($response);

    }
} catch (Exception $e) {
    echo 'exception' . '<br>';
    var_dump($e);

}

Then post the results here.然后在这里发布结果。

The code that I edited in my original post now works.我在原始帖子中编辑的代码现在可以工作了。 The main thing was the php debugging needed to be turned on and then I started getting real errors.最主要的是需要打开 php 调试,然后我开始遇到真正的错误。 Make sure this is at the top of the php file so you can get error reporting.确保它位于 php 文件的顶部,以便获得错误报告。

PHP Code: PHP 代码:

 <?php error_reporting(-1); ini_set('display_errors', 'On'); set_error_handler("var_dump");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM