【问题标题】:Sendgrid adding to mail list not workingSendgrid添加到邮件列表不起作用
【发布时间】:2013-09-13 07:15:42
【问题描述】:

我正在使用以下代码将电子邮件添加到 sendgrid 的列表中。但它返回插入的:0 响应

$request_url =  "https://sendgrid.com/api/newsletter/lists/email/add.json";
$data = array("email"=>"testemail@test.com");
$params = array(
    'api_user'  => $sengrid_user,
    'api_key'   => $sendgrid_pass,
    'list'=>"TestAlwin",
    'data' =>json_encode($data)
  );

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $request_url);
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$resp = curl_exec($ch);
curl_close($ch);

这将返回插入的 :0 响应。它应该将 mailid 插入到我指定的列表中。

我使用以下内容作为参考:

curl -d 'api_user=your_sendgrid_username&api_key=your_sendgrid_password&list=my_list&data[]={"email":"address1@domain.com","name":"contactName1"}&data[]={"email":"address2@domain.com","name":"contactName2"}' https://sendgrid.com/api/newsletter/lists/email/add.json

这实际上是在他们的 api 中给出的:

http://sendgrid.com/docs/API_Reference/Marketing_Emails_API/emails.html

我在这里添加 curl vebrose:

* About to connect() to sendgrid.com port 80 (#0)
*   Trying 1.1.1.1... * connected
* Connected to sendgrid.com (1.1.1.1) port 80 (#0)
> POST /api/newsletter/lists/email/add.json?list=TestAlwin HTTP/1.1
Host: sendgrid.com
Accept: */*
Content-Length: 395
Expect: 100-continue
Content-Type: multipart/form-data; boundary=----------------------------4435bfc2eb00

< HTTP/1.1 100 Continue
< HTTP/1.1 200 OK
< Server: nginx
< Date: Fri, 13 Sep 2013 07:04:42 GMT
< Content-Type: text/html
< Transfer-Encoding: chunked
< Connection: keep-alive
< Vary: Accept-Encoding
< 
* Connection #0 to host sendgrid.com left intact
* Closing connection #0

这些 1.1.1.1 只是我在此处添加的测试 IP,而不是实际 IP。

【问题讨论】:

  • 您是否从 API 收到任何错误响应?
  • 不。只是 {"inserted": 0} 当我打印 $resp
  • 这实际上是因为我这边的一个错误,我也需要传递名称。现在我怎样才能在那里传递多个值?
  • 如果您的意思是多封电子邮件等,只需将它们作为数组添加到您的数组中

标签: php curl sendgrid


【解决方案1】:

SendGrid Newsletter API 已损坏 - 特别是您一次只能向列表添加一封电子邮件,使其无法使用。我一周前报告了它——他们确认了这个错误,没有给出修复的时间表。他们似乎并不太担心......

【讨论】:

    【解决方案2】:

    SendGrid Newsletter API 至少需要“email”和“name”参数(参见文档:http://sendgrid.com/docs/API_Reference/Marketing_Emails_API/emails.html

    我更新了您的测试代码,将“name” => '' 添加到代码中,它运行良好(还修复了 api_user 的 $sendgrid_user 变量中的拼写错误)。

    干杯!

    --杰森

    $request_url =  "https://sendgrid.com/api/newsletter/lists/email/add.json";
    $data = array("email" => "hello@world.com", "name" => '');
    $params = array(
        'api_user'  => $sendgrid_user,
        'api_key'   => $sendgrid_pass,
        'list'=>"TestAlwin",
        'data' =>json_encode($data)
      );
    
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL, $request_url);
    curl_setopt ($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    $resp = curl_exec($ch);
    curl_close($ch);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      相关资源
      最近更新 更多