高级sendgrid设置 - PHP的API

时间:2016-11-26 00:10:07

标签: php email sendgrid sendgrid-api-v3

我需要知道如何配置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文件中提取信息。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

我遇到了同样的问题,并修复了更改标题的问题。

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

    ➜  CloudFoundry git:(2.x) ✗ cf logs actuator | grep Test
   2019-08-28T12:51:17.67-0400 [APP/PROC/WEB/0] OUT   Test Critical message
   2019-08-28T12:51:17.67-0400 [APP/PROC/WEB/0] OUT   Test Error message
   2019-08-28T12:51:17.67-0400 [APP/PROC/WEB/0] OUT   Test Warning message
   2019-08-28T12:51:17.67-0400 [APP/PROC/WEB/0] OUT   Test Informational message
   2019-08-28T12:51:17.67-0400 [APP/PROC/WEB/0] OUT   Test Debug message

----- after adjusting ------

   2019-08-28T12:52:16.29-0400 [APP/PROC/WEB/0] OUT   Test Critical message

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

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