sendgrid-php
sendgrid-php copied to clipboard
Adding users to a list via PHP
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;
}