CURLOPT_POSTFIELDS上的两个字段

时间:2018-05-29 12:29:13

标签: php curl sendgrid

我想使用Sendgrid来同时发送两封电子邮件。以下代码适用于单个电子邮件。我想发送第二个,而不重复所有这些代码。是否可以通过向CURLOPT_POSTFIELDS附加另一个值来实现,这是唯一可以更改的字段?

<?php
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.sendgrid.com/v3/mail/send",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{\n  \"personalizations\": [\n    {\n      \"to\": [\n        {\n          \"email\": \"you@example.com\"\n        }\n      ],\n      \"subject\": \"Test\"\n    }\n  ],\n  \"from\": {\n    \"email\": \"me@example.com\"\n  },\n  \"content\": [\n    {\n      \"type\": \"text/html\",\n      \"value\": \"Hello world!"\n    }\n  ]\n}",
  CURLOPT_HTTPHEADER => array(
    "authorization: Bearer SG.1234123412341234",
    "cache-control: no-cache",
    "content-type: application/json"
  ),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
?>

1 个答案:

答案 0 :(得分:0)

您需要做的就是将多个&#34;添加到&#34;个性化中的对象json。 如下 -

"to":["email":"abc@abc.com"],"to":["email":"abc2@abc2.com"] ....