我在这里遵循示例: https: //github.com/sendgrid/sendgrid-php/blob/master/examples/mail/mail.php我已经删除了大部分参数来发送一封非常基本的电子邮件:<?phprequire '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";}?>执行时出现以下错误{"errors":[{"message":"Content-Type should be application/json.","field":null,"help":null}]}看看原来的例子,我没有看到任何我可能删除的东西会导致这种情况,尽管我可能是错的。感谢您的任何意见。
1 回答
茅侃侃
TA贡献1842条经验 获得超21个赞
,尝试删除request_body 变量中的最后一个。
改变
"personalizations": [
{
"to": [
{
"email": "bob@domain.com",
"name": "Receiver Bob"
}
]
}],
对此
"personalizations": [
{
"to": [
{
"email": "bob@domain.com",
"name": "Receiver Bob"
}
]
}]
当我遇到这些问题时,我会针对验证器运行 json。很多时候它会让我知道从哪里开始。
https://jsonlint.com/
- 1 回答
- 0 关注
- 95 浏览
添加回答
举报
0/150
提交
取消