由于JSON错误,使用sendgrid使用PHP发送邮件失败

由于JSON错误,使用sendgrid使用PHP发送邮件失败,php,sendgrid,Php,Sendgrid,我在这里举一个例子: 我已经删除了大部分参数以发送一封非常基本的电子邮件: <?php require 'vendor/autoload.php'; // If you're using Composer (recommended) $apiKey = getenv('SENDGRID_API_KEY'); $sg = new \SendGrid($apiKey); $request_body = json_decode('{ "content": [

我在这里举一个例子:

我已经删除了大部分参数以发送一封非常基本的电子邮件:

<?php
require 'vendor/autoload.php'; // If you're using Composer (recommended)
$apiKey = getenv('SENDGRID_API_KEY');
$sg = new \SendGrid($apiKey);

$request_body = json_decode('{
  "content": [
    {
      "type": "text/html", 
      "value": "<html><p>Hello, world!</p></html>"
    }
  ], 
  "from": {
    "email": "alice@domain.com",
    "name": "Sender Alice"
  }, 
  "personalizations": [
    {
      "to": [
        {
          "email": "bob@domain.com", 
          "name": "Receiver Bob"
        }
      ]
    }
  ], 
}');

try {
    $response = $sg->client->mail()->send()->post($request_body);
    print $response->statusCode() . "\n";
    print_r($response->headers());
    print $response->body() . "\n";
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}
?>

尝试删除请求体变量中的最后一个

改变

"personalizations": [
{
  "to": [
    {
      "email": "bob@domain.com", 
      "name": "Receiver Bob"
    }
  ]
}], 
对此

"personalizations": [
{
  "to": [
    {
      "email": "bob@domain.com", 
      "name": "Receiver Bob"
    }
  ]
}]
当我遇到这些问题时,我会对验证器运行json。很多时候,它会让我知道从哪里开始


这是一个多么幼稚的错误。谢谢你找到它和提示。@equinoxe5没问题!很高兴这有帮助。