SendGrid和PHP到多个配方

时间:2017-04-02 09:46:26

标签: php email

我正在使用sendgrid从我正在构建的文档系统发送HTML电子邮件。当文件上传时,sendgrid需要通过电子邮件发送与该案例相关的所有内容。我有一切为个人电子邮件工作,我可以关闭我保存的模板但不能发送电子邮件给多个收件人

我已经生成了一系列收件人

$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;
    }
}

我尝试使用个性化 - &gt;来传递数组但得到错误

Call to a member function to() on null in includes/classes/mail.php on line <b>82</b><br />

2 个答案:

答案 0 :(得分:0)

我刚刚创建了一个遍历每个收件人的循环,并向他们发送了一封电子邮件,不像生成多个to:list那样高效,但现在直到我收到sendgrid开发团队的回复

答案 1 :(得分:0)

$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.
}