0

我想在 php 中使用 Sendinblue API 发送电子邮件。

我在电子邮件代码中添加了一些 php 变量,但脚本不起作用!

<?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;
}

?>

但是,我收到此错误消息:

{"error":{"status":400,"message":"输入必须是有效的 JSON 对象","code":"bad_request"}}

4

1 回答 1

1

更新

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

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

将编码字符串放入 curl

 CURLOPT_POSTFIELDS =>  $postData
于 2019-07-19T10:41:14.113 回答