Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP : Send email to multiple addresses

I want to send an email with CakeEmail to multiple addresses (email address of the people who sign up in my website).

Here is my code I use :

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('[email protected]' => 'Hello'))
            ->subject('ALERTE')
            ->emailFormat('html')
            ->template('ouverture')->viewVars($d);
            return $mail->send();

        }

    else{
        return false;
    }

    }

And here's my error :

Invalid email : ""[email protected]","[email protected]","
like image 890
rohidjetha Avatar asked Apr 29 '14 17:04

rohidjetha


People also ask

How can I send email in cakephp 3?

use Cake\Mailer\Email; After you've loaded Email , you can send an email with the following: $email = new Email('default'); $email->from(['[email protected]' => 'My Site']) ->to('[email protected]') ->subject('About') ->send('My message');

How can I send email in cakephp 2?

Sending messages quickly Example: CakeEmail::deliver('[email protected]', 'Subject', 'Message', array('from' => '[email protected]')); This method will send an email to [email protected], from [email protected] with subject Subject and content Message. The return of deliver() is a CakeEmail instance with all configurations set.


2 Answers

Try

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

$mail = new CakeEmail();
$mail->to($tests) 
     ->from(array('[email protected]' => 'Hello'))
     ->subject('ALERTE')
     ->emailFormat('html')
     ->send('Your message here');
like image 165
Indrajeet Singh Avatar answered Oct 09 '22 09:10

Indrajeet Singh


Try

$mail = new CakeEmail();

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

$mail->from(array('[email protected]' => 'Hello'))
     ->subject('ALERTE')
     ->emailFormat('html')
     ->template('ouverture')->viewVars($d);
return $mail->send();
like image 26
cornelb Avatar answered Oct 09 '22 09:10

cornelb