Happy Coder
Happy Coder

Reputation: 4682

Sendgrid adding to mail list not working

I am using the following code for adding emails to a list in sendgrid. But it is returning inserted :0 response

$request_url =  "https://sendgrid.com/api/newsletter/lists/email/add.json";
$data = array("email"=>"[email protected]");
$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);

This is returning inserted :0 response. It should insert the mailid to the list I have specified.

I am using the following as reference :

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

This is actually given in their api here :

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

and I am adding the curl vebrose here :

* 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

These 1.1.1.1 is just a test IP that I have added here instead of the actual one.

Upvotes: 1

Views: 1488

Answers (2)

Jayson Sperling
Jayson Sperling

Reputation: 3

The SendGrid Newsletter API requires both 'email' and 'name' parameters at a minimum (as seen in the docs: http://sendgrid.com/docs/API_Reference/Marketing_Emails_API/emails.html)

I updated your test code to include "name" => '' to the code and it works beautifully (also fixed typo in the api_user's $sendgrid_user variable).

Cheers!

--Jayson

$request_url =  "https://sendgrid.com/api/newsletter/lists/email/add.json";
$data = array("email" => "[email protected]", "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);

Upvotes: 0

Yarin
Yarin

Reputation: 183429

The SendGrid Newsletter API is broken- specifically you can't add more than a single email to a list at a time, making it unusable. I reported it a week ago- they confirmed the bug, gave no timeline for a fix. They don't seem too concerned...

Upvotes: 2

Related Questions