CakePHP:发送电子邮件到多个地址

时间:2014-04-29 17:16:17

标签: php email cakephp cakephp-2.0 cakeemail

我想将CakeEmail的电子邮件发送到多个地址(在我的网站上注册的人的电子邮件地址)。

这是我使用的代码:

public function send($d){
    $this->set($d);
    if($this->validates()){
        App::uses('CakeEmail','Network/Email');

        $users = $this->User->find('all');

        $this->set($tests);
        foreach($users as $user)
        {
            $tests .= '"'.$user['User']['email'].'",';
        }

        $mail = new CakeEmail();
        $mail
            ->to(array($tests)) 
            ->from(array('test2@test.fr' => 'Hello'))
            ->subject('ALERTE')
            ->emailFormat('html')
            ->template('ouverture')->viewVars($d);
            return $mail->send();

        }

    else{
        return false;
    }

    }

这是我的错误:

Invalid email : ""test@test.com","test@test.fr","

2 个答案:

答案 0 :(得分:10)

尝试

$tests = array();
foreach($users as $user)
{
    $tests[] = $user['User']['email'];
}

$mail = new CakeEmail();
$mail->to($tests) 
     ->from(array('test2@test.fr' => 'Hello'))
     ->subject('ALERTE')
     ->emailFormat('html')
     ->send('Your message here');

答案 1 :(得分:3)

尝试

$mail = new CakeEmail();

foreach($users as $user) {
    $mail->addTo($user['User']['email']);
}

$mail->from(array('test2@test.fr' => 'Hello'))
     ->subject('ALERTE')
     ->emailFormat('html')
     ->template('ouverture')->viewVars($d);
return $mail->send();