我想使用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;
}
?>
但是,我收到此错误消息:
{“错误”:{“状态”:400,“消息”:“输入必须是有效的JSON对象”,“代码”:“ bad_request”}}
答案 0 :(得分: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