【问题标题】:Two fields on CURLOPT_POSTFIELDSCURLOPT_POSTFIELDS 上的两个字段
【发布时间】:2018-05-29 12:29:12
【问题描述】:

我想使用 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);
?>

【问题讨论】:

标签: php curl sendgrid


【解决方案1】:

您需要做的就是在个性化 json 中添加多个“to”对象。 如下 -

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

【讨论】:

  • 在这种情况下,它将向不同的收件人发送不同的电子邮件内容。我尝试在 JSON 中添加一个新的“个性化”对象,但它似乎不起作用。
  • 使用新对象时会发生什么?它只发送到第一封电子邮件吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-08
  • 2021-12-02
  • 2014-04-07
  • 1970-01-01
相关资源
最近更新 更多