SendGrid和PHP到多个Recipient
我正在使用sendgrid从我正在构建的文档系统发送html电子邮件。当文件上传时,sendgrid需要向所有与该案例相关的人发送电子邮件。我有所有的工作为个人电子邮件,我可以自定义模板,我已经保存,但不能发送电子邮件给多个收件人SendGrid和PHP到多个Recipient,php,email,Php,Email,我正在使用sendgrid从我正在构建的文档系统发送html电子邮件。当文件上传时,sendgrid需要向所有与该案例相关的人发送电子邮件。我有所有的工作为个人电子邮件,我可以自定义模板,我已经保存,但不能发送电子邮件给多个收件人 $email = array(j.bloggs@bloggs.com, j.doe@me.net, d.smith@smith.co.uk); 我已经生成了一个收件人数组 $email = array(j.bloggs@bloggs.com, j.doe@me.ne
$email = array(j.bloggs@bloggs.com, j.doe@me.net, d.smith@smith.co.uk);
我已经生成了一个收件人数组
$email = array(j.bloggs@bloggs.com, j.doe@me.net, d.smith@smith.co.uk);
我想将其传递到我的sendgrid电子邮件对象中,以便发送给所有人
private function send() {
$sg = new \SendGrid(self::$key);
$response = $sg->client->mail()->send()->post(self::$mail);
return $response->statusCode();
}
public function file_saved($file="", $case="") {
self::$from = new SendGrid\Email($this->fromName, $this->fromEmail);
self::$to = new SendGrid\Email($this->toName, $this->toEmail);
self::$content = new SendGrid\Content("text/html", "Hello, Email!");
self::$mail = new SendGrid\Mail(
self::$from,
$this->subject,
self::$to,
self::$content
);
$str = "<p>A file has been successfully uploaded to {$case->case_name} ({$case->case_code}).</p>
<br />
<p>{$file->file_name} - ".size($file->file_size)."</p>";
self::$mail->personalization[0]->addSubstitution("-name-", $this->toName);
self::$mail->personalization[0]->addSubstitution("-str-", $str);
self::$mail->personalization[0]->addSubstitution("-btn-", "Download File");
self::$mail->personalization[0]->addSubstitution("-url-", HTTP.BASE_URL.DS.'uploads'.DS.$file->file_path);
self::$mail->setTemplateId("2f845487-6243-4562-b6fb-022185b7fde7");
if (!$this->send() == 202) {
return false;
}
else {
return true;
}
}
专用函数send(){
$sg=new\SendGrid(self:$key);
$response=$sg->client->mail()->send()->post(self::$mail);
返回$response->statusCode();
}
已保存公用函数文件($file=“”,$case=“”){
self::$from=newsendgrid\Email($this->fromName,$this->frommemail);
self::$to=newsendgrid\Email($this->toName,$this->toEmail);
self::$content=newsendgrid\content(“text/html”,“你好,电子邮件!”);
self::$mail=new SendGrid\mail(
self::$from,
$this->subject,
self::$to,
self::$content
);
$str=“文件已成功上载到{$case->case\u name}({$case->case\u code})
{$file->file\u name}-“.size($file->file\u size)。”“;
self::$mail->personalization[0]->addSubstitution(“-name-”,$this->toName);
self::$mail->personalization[0]->addSubstitution(“-str-”,$str);
self::$mail->personalization[0]->addSubstitution(“-btn-”,“下载文件”);
self::$mail->personalization[0]->addSubstitution(“-url-”,HTTP.BASE\u url.DS.'uploads.DS.$file->file\u path);
self::$mail->setTemplateId(“2f845487-6243-4562-b6fb-022185b7fde7”);
如果(!$this->send()==202){
返回false;
}
否则{
返回true;
}
}
我试图使用personalization->to并将其传递给数组,但得到了错误
Call to a member function to() on null in includes/classes/mail.php on line <b>82</b><br />
在includes/classes/mail.php的第82行调用成员函数to()on null
我刚刚创建了一个循环,它遍历每个收件人并发送电子邮件,并不像生成多个to:list那样高效,但目前它仍在工作,直到我从sendgrid开发团队获得响应为止(j。bloggs@bloggs.comJdoe@me.netDsmith@smith.co.uk);
$this->email = array(j.bloggs@bloggs.com, j.doe@me.net, d.smith@smith.co.uk);
private function send() {
$sg = new \SendGrid(self::$key);
$response = $sg->client->mail()->send()->post(self::$mail);
return $response->statusCode();
}
public function file_saved($file="", $case="") {
$str = "<p>A file has been successfully uploaded to {$case->case_name} ({$case->case_code}).</p>
<br />
<p>{$file->file_name} - ".size($file->file_size)."</p>";
self::$from = new SendGrid\Email($this->fromName, $this->fromEmail);
self::$content = new SendGrid\Content("text/html", "Hello, Email!");
foreach($this->email as $email_addr) {
self::$to = new SendGrid\Email($this->toName, $email_addr);
self::$mail = new SendGrid\Mail(
self::$from,
$this->subject,
self::$to,
self::$content
);
self::$mail->personalization[0]->addSubstitution("-str-", $str);
self::$mail->personalization[0]->addSubstitution("-btn-", "Download File");
self::$mail->personalization[0]->addSubstitution("-url-", HTTP.BASE_URL.DS.'uploads'.DS.$file->file_path);
self::$mail->setTemplateId("2f845487-6243-4562-b6fb-022185b7fde7");
if (!$this->send() == 202) {
$response[$address] = false;
}
else {
$response[$address] = true;
}
}
return $response; // contains true/false for each email address if sent or not.
}
私有函数send(){
$sg=new\SendGrid(self:$key);
$response=$sg->client->mail()->send()->post(self::$mail);
返回$response->statusCode();
}
已保存公用函数文件($file=“”,$case=“”){
$str=“文件已成功上载到{$case->case\u name}({$case->case\u code})
{$file->file\u name}-“.size($file->file\u size)。”“; self::$from=newsendgrid\Email($this->fromName,$this->frommemail); self::$content=newsendgrid\content(“text/html”,“你好,电子邮件!”); foreach($this->email as$email\u addr){ self::$to=newsendgrid\Email($this->toName,$Email\u addr); self::$mail=new SendGrid\mail( self::$from, $this->subject, self::$to, self::$content ); self::$mail->personalization[0]->addSubstitution(“-str-”,$str); self::$mail->personalization[0]->addSubstitution(“-btn-”,“下载文件”); self::$mail->personalization[0]->addSubstitution(“-url-”,HTTP.BASE\u url.DS.'uploads.DS.$file->file\u path); self::$mail->setTemplateId(“2f845487-6243-4562-b6fb-022185b7fde7”); 如果(!$this->send()==202){ $response[$address]=false; } 否则{ $response[$address]=true; } } return$response;//包含每个电子邮件地址的true/false(如果已发送或未发送)。 }