【问题标题】:Advanced sendgrid settings - API for PHP高级 sendgrid 设置 - PHP 的 API
【发布时间】:2016-11-26 00:10:07
【问题描述】:

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

【问题讨论】:

    标签: php email sendgrid sendgrid-api-v3


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      如果您使用 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));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-08
        • 2015-03-21
        • 1970-01-01
        • 2016-03-27
        • 2021-01-23
        • 1970-01-01
        相关资源
        最近更新 更多