我在这里遵循示例: https: //github.com/sendgrid/sendgrid-php/blob/master/examples/mail/mail.php
我已经删除了大部分参数来发送一封非常基本的电子邮件:
<?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";
}
?>
执行时出现以下错误
{"errors":[{"message":"Content-Type should be application/json.","field":null,"help":null}]}
看看原来的例子,我没有看到任何我可能删除的东西会导致这种情况,尽管我可能是错的。
感谢您的任何意见。
茅侃侃