sendgrid / sendgrid-php

The Official Twilio SendGrid PHP API Library
https://sendgrid.com
MIT License
1.49k stars 624 forks source link

Adding users to a list via PHP #1110

Open Rvnd0m opened 2 months ago

Rvnd0m commented 2 months ago

Good morning,

I come from a Sendgrid ticket, as they told me to continue here as they can not discuss about PHP code via ticket...

I wonder if it is possible to add an email address to an specific user list, via PHP code execution. The objective with that code is to insert it on an Elementor Form, and then when the user submits the form, the PHP code is executed to add that user email address to the automation marketing list.

I am trying with this code, but I get an error when I submit the form (= when the PHP is executed).

Thank you in advance if someone could help! :)

$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.sendgrid.com/v3/marketing/contacts",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => json_encode(array(
  "list_ids" => ["b*****my_list_ID*****uhrega15"],
  "contacts" => [
    ["email" => $email]
  ]
)),
CURLOPT_HTTPHEADER => array(
  "authorization: Bearer SG.****my_api_key*****igedRsD",
  "content-type: application/json"
),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
ahmadzissa commented 1 month ago

I suggest you to add more info like what error do you get

I believe the array should be like this

"list_ids" => ["bmy_list_IDuhrega15"], "contacts" => [ "email" => $email, //rest of the data ]