【发布时间】: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);
?>
【问题讨论】:
-
这可能会有所帮助:github.com/sendgrid/sendgrid-php/blob/master/examples/helpers/… 换句话说,是的,发送多个个性化设置(将该 json 拆分为一个数组,并为第二封电子邮件添加第二个数组,依此类推,然后重新json 对其进行编码以用于 api 调用)。