Sendgrid添加到邮件列表不起作用

时间:2013-09-13 07:15:43

标签: php curl sendgrid

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

2 个答案:

答案 0 :(得分:1)

SendGrid Newsletter API已被破解 - 具体而言,您不能一次向列表添加多封电子邮件,使其无法使用。我在一周前报道了它们 - 他们证实了这个错误,没有提供修复的时间表。他们似乎并不太在意......

答案 1 :(得分:0)

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

我更新了您的测试代码,以包含“name”=&gt; ''代码,它工作得很漂亮(在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);