我有简单的 sendgrid php 脚本来发送电子邮件,这里唯一的问题是我需要添加更多收件人,所以这段代码只适用于一个收件人,我正在查看官方文档但找不到任何有用的信息,是有谁知道我需要如何更改以及我需要在此处更改什么以添加更多收件人/电子邮件。
function sendEmail($subject, $to, $message) {
$from = new SendGrid\Email(null, "sample@email.com");
$subject = $subject;
$to = new SendGrid\Email(null, $to);
$content = new SendGrid\Content("text/html", $message);
$mail = new SendGrid\Mail($from, $subject, $to, $content);
$apiKey = 'MY_KEY';
$sg = new \SendGrid($apiKey);
$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();
}
最佳答案
SendGrid\Mail
类支持通过SendGrid\Personalization
类添加多个to
地址。
您可以在此处查看示例:https://github.com/sendgrid/sendgrid-php/blob/master/examples/helpers/mail/example.php#L31-L35
将 Personalization
视为您电子邮件的信封。它包含收件人的地址和其他类似数据。每个 Sendgrid\Mail
对象,必须至少有一个 Personalization
。
通过您正在使用的构造函数,已经为您创建了一个Personalization
对象,参见此处:https://github.com/sendgrid/sendgrid-php/blob/master/lib/helpers/mail/Mail.php#L951-L958
您可以创建一个Mail
对象without this及以后add your own 个性化
。
关于php - Sendgrid php发送给多个收件人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43618340/