1

我正在使用以下代码将电子邮件添加到 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。

4

2 回答 2

2

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

于 2014-02-15T17:04:10.263 回答
0

SendGrid Newsletter API 至少需要“电子邮件”和“名称”参数(如文档中所示:http://sendgrid.com/docs/API_Reference/Marketing_Emails_API/emails.html

我更新了您的测试代码以在代码中包含“名称”=> '',它运行良好(还修复了 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);
于 2014-02-20T21:30:36.680 回答