Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for submitting multiple recipients to a list simultaneously. #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/SendGridPHP/Connect.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,18 @@ protected function makeApiCall($url, $postData = NULL, $method = 'POST') {
//Set to FALSE to stop cURL from verifying the peer's certificate (needed for local hosts development mostly)
if(!$this->_curl_ssl_verify) curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);

// Tell curl to use HTTP POST
$postData = http_build_query($postData);

$this->debugCall('DEBUG - Original post querystring: ', urldecode($postData));

// Replace numerically indexed arrays, such as data[1], data[2], etc. with data[], data[], data[].
// This is needed, for instance, when we try to submit multiple email addresses to a List.
// The postdata is url_encoded at this point, so replace using %5B = [, %5D = ].
$postData = preg_replace('/%5B\d+%5D/', '%5B%5D', $postData);

$this->debugCall('DEBUG - Replaced post querystring: ', urldecode($postData));

// Tell curl to use HTTP POST
curl_setopt ( $session, CURLOPT_CUSTOMREQUEST, strtoupper ( $method ) );
// Tell curl that this is the body of the POST
curl_setopt ($session, CURLOPT_POSTFIELDS, $postData);
Expand Down
16 changes: 14 additions & 2 deletions src/SendGridPHP/Newsletter.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,22 @@ public function newsletter_lists_delete($list) {
public function newsletter_lists_email_add($list , $data) {
$url = "newsletter/lists/email/add";

/**
* SendGrid, in their wisdom, requires that we submit each recipient in the list
* as a separate parameter.
*
* Since PHP does not support multiple params with the same name, we stuff them into an array.
*
* SendGridPHP\Connect's makeApiCall method removes the numerical indexes in the generated querystring.
*/
array_walk($data, function(&$row) {
$row = json_encode($row);
});

$postData = array(
'list' => $list,
'data' => json_encode($data),
);
'data' => $data,
);

$results = $this->makeApiCall ( $url , $postData );

Expand Down