我正在使用此脚本通过cURL发送电子邮件。我没有使用sendgrid库,我已经查看了API文档。我想选择发送到多个“到”地址。我该怎么做呢?
$params = array(
'to' => $to,
'subject' => $title,
'text' => 'Subject',
'from' => '[email protected]',
);
$request = $url.'api/mail.send.json';
$headr = array();
// set authorization header
$headr[] = 'Authorization: Bearer '.$pass;
$session = curl_init($request);
curl_setopt ($session, CURLOPT_POST, true);
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// add authorization header
curl_setopt($session, CURLOPT_HTTPHEADER,$headr);
$response = curl_exec($session);
curl_close($session);
查看SendGrid API文档(例如,对于v2 API):https://sendgrid.com/docs/API_Reference/Web_API/mail.html
这也可以作为数组传递,以发送到多个位置。示例:to [] = [email protected]&to [] = [email protected]
所以你可以将这个$添加到param作为数组:
$to = ["[email protected]", "[email protected]"]; // etc.
尝试
$params = array(
'to[0]' => $to1,
'to[1]' => $to2,
);
它对我有用。
以下是SendGrid在文档中的用法:Sending a Basic Email to Multiple Recipients。
在您的示例中,to
字符串是本机SMTP TO:
值。因此,您不能使用数组,但可以提供逗号分隔的多个地址字符串。这些消息中的每一个都将由SendGrid处理,并且每个接收者将以经典的“对话”方式看到彼此的所有地址。
如果您想利用SendGrid的“向同一个人发送相同的电子邮件”能力,您需要利用上述v3 API。
对于API V2存储数组中的电子邮件,例如:
$json_string = array(
'to' => array(
'[email protected]', '[email protected]'
),
'category' => 'test'
);
然后将其添加到$ param数组中:
'x-smtpapi' => json_encode($json_string),
请注意,在这种情况下,正常的地址,将不会收到电子邮件。欲了解更多详情,请访问:https://sendgrid.com/docs/Integrate/Code_Examples/v2_Mail/php.html
试试这个:
$params = array(
'to[0]' => '[email protected]',
'to[1]' => '[email protected]',
'subject' => $title,
'text' => 'Subject',
'from' => '[email protected]',
);
$request = $url.'api/mail.send.json';
$headr = array();
// set authorization header
$headr[] = 'Authorization: Bearer '.$pass;
$session = curl_init($request);
curl_setopt ($session, CURLOPT_POST, true);
curl_setopt ($session, CURLOPT_POSTFIELDS, $params);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// add authorization header
curl_setopt($session, CURLOPT_HTTPHEADER,$headr);
$response = curl_exec($session);
curl_close($session);
您需要做的唯一更改是添加此代码=>
'to[0]' => '[email protected]',
'to[1]' => '[email protected]',