0

我需要知道如何配置 sendgrid v3 以使用 WEB API 发送电子邮件。我正在使用以下代码,但它显示了一个错误:

$request_body = json_decode('{
              "asm": {
              "content": [
                 {
                    "type": "text/html", 
                    "value": "'. $body .'"
                 }
              ], 
              "from": {
                 "email": "'. $sender_email .'", 
                 "name": "'. $sender_name .'"
              }, 
              "personalizations": [
                 {
                    "bcc": [
                      {
                         "email": "'. $receiver_email .'", 
                         "name": "'. $receiver_name .'"
                      }
                    ],
                    "headers": {
                      "X-Accept-Language": "en", 
                      "X-Mailer": "MailerKobsa"
                    }, 
                    "send_at": 1409348513, 
                    "subject": "'. $subject .'", 
                    "substitutions": {
                      "id": "substitutions", 
                      "type": "object"
                    }, 
                    "to": [
                      {
                         "email": "'. $receiver_email .'", 
                         "name": "'. $receiver_name .'"
                      }
                    ]
                 }
              ], 
              "reply_to": {
                 "email": "'. $sender_email .'", 
                 "name": "'. $sender_name .'"
              }, 
              "send_at": 1409348513, 
              "subject": "'. $subject .'"
            }');

但我收到以下错误:

415array(9) { [0]=> string(36) "HTTP/1.1 415 Unsupported Media Type " [1]=> string(14) "Server: nginx " [2]=> string(36) "Date: Fri, 25 Nov 2016 23:53:37 GMT " [3]=> string(31) "Content-Type: application/json " [4]=> string(19) "Content-Length: 92 " [5]=> string(23) "Connection: keep-alive " [6]=> string(22) "X-Frame-Options: DENY " [7]=> string(1) " " [8]=> string(0) "" } {"errors":[{"message":"Content-Type should be application/json.","field":null,"help":null}]}415array(9) { [0]=> string(36) "HTTP/1.1 415 Unsupported Media Type " [1]=> string(14) "Server: nginx " [2]=> string(36) "Date: Fri, 25 Nov 2016 23:53:38 GMT " [3]=> string(31) "Content-Type: application/json " [4]=> string(19) "Content-Length: 92 " [5]=> string(23) "Connection: keep-alive " [6]=> string(22) "X-Frame-Options: DENY " [7]=> string(1) " " [8]=> string(0) "" } {"errors":[{"message":"Content-Type should be application/json.","field":null,"help":null}]}

我必须提到我已经尝试使用 Github 中的“简单”版本发送电子邮件,我已经正确配置了我的 apikey,并且我已经能够在简单版本中发送电子邮件而没有任何问题。但在高级版本中,我需要添加更多信息,例如收件人姓名,而不仅仅是他的电子邮件。

我不使用广告系列,不从数据库中提取信息。我从 CSV 文件中提取信息。

4

2 回答 2

0

我有同样的问题,我修复了更改标题。

如果您使用 PHP 中的 CURL,这将是一个很大的帮助 我的代码解决了:

$authorization = 'authorization: Bearer '.trim($apiKey);
$header = [
'Content-Type: application/json',
$authorization
];
curl_setopt($session, CURLOPT_HTTPHEADER, $header);

我不知道为什么数组函数不起作用:

curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: 
application/json',
$authorization));
于 2019-08-28T18:19:49.523 回答
0

根据错误:"message":"Content-Type should be application/json."您是否确保正确声明了 Content-Type 标头?

于 2016-11-30T19:53:32.763 回答