0

I want to send email using Sendinblue API in php.

I added some php variable in the email code, but the script is not working!

<?php

$email = "12345@gmail.com";
$username = "12345";
$html = "<h1>Hi!</h1>";
$subject = "Hello";

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.sendinblue.com/v3/smtp/email",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{\"sender\":{\"name\":\"ABC\",\"email\":\"abc@abc.com\"},\"to\":[{\"email\":\".$email.\",\"name\":\".$username.\"}],\"bcc\":[{\"email\":\"admin@abc.com\",\"name\":\"Admin\"}],\"htmlContent\":\".$html.\",\"subject\":\".$subject.\"\"replyTo\":{\"email\":\"support@abc.com\",\"name\":\"Support\"}}",
  CURLOPT_HTTPHEADER => array(
    "accept: application/json",
    "api-key: API-KEY",  //hidden because of privacy reason
    "content-type: application/json"
  ),
));

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

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

?>

However, I receive this error message:

{"error":{"status":400,"message":"Input must be a valid JSON object","code":"bad_request"}}

HosteyMega
  • 25
  • 7

1 Answers1

1

Update

$email = "12345@gmail.com";
$username = "12345";
$html = "<h1>Hi!</h1>";
$subject = "Hello";

with

$arr = [ 
          'email'=>"12345@gmail.com",
          'username'=>"12345",
          'html'=>"<h1>Hi!</h1>",
          'subject'=>"Hello",
       ];
$postData = json_encode( $arr );

Put encode string in curl

 CURLOPT_POSTFIELDS =>  $postData
Andreas
  • 23,610
  • 6
  • 30
  • 62
PHP Ninja
  • 1,055
  • 2
  • 9
  • 28