0

i don't understand how send one email for all users, i do it this in my controller :

// Init
$data = $this->request->data['Email'];
$d = array(
    'subject' => $data['subject'],
    'message' => $data['message']
);

// QUERY
$all = $this->Spoutnik->find('all', array(
    'conditions' => array(
        'Spoutnik.role >=' => '1'
    ),
    'fields' => array('Spoutnik.email')
));
$this->set(compact('all'));

// list
$bcc = '';
foreach ($all as $user) {
    $bcc .= $user['Spoutnik']['email'].',';
}

// MAIL
App::uses('CakeEmail', 'Network/Email');
$CakeEmail = new CakeEmail('default');

$website_short_name = Configure::read('website.short_name');
$CakeEmail->bcc("$bcc");
$CakeEmail->subject(''.$website_short_name.' :: '.$d['subject'].'');

$CakeEmail->viewVars(array(
    'message' => (''.$d['message'].'')
));

$CakeEmail->emailFormat('html');
$CakeEmail->template('message_direct');

// final
$CakeEmail->send();

But i have error "no valid mail" , and after the liste of user's mail

what is wrong in my code ?

zelocalhost
  • 1,175
  • 3
  • 20
  • 39

3 Answers3

1

Couple of things I've noticed at a quick glance...

foreach ($all as $user) {
    $bcc .= $user['Spoutnik']['email'].',';
}

In that code, you're adding a comma after every email, so at the end of your string you'll have a comma. Try this:

$e = 0;
foreach ($all as $user) {
    if($e > 0) $bcc .= ',';
    $bcc .= $user['Spoutnik']['email'];
    $e++;
}

--edit-- good point Deepak, Cake's documentation suggests you give BCC an array. It's easier and more efficient to produce so do that.

Second, $CakeEmail->bcc("$bcc"); doesn't need the quotes. It should work fine with them, but I've seen Cake do some pretty weird things... Try taking them out:

$CakeEmail->bcc($bcc);

Third, you're setting all those emails to BCC which is fine, but I can't see a to address. If you want to send out to a lot of email address without them seeing each other, you still need to send the email to somewhere, even if its noreply@yourdomain.com. Add a to address in before you send:

$CakeEmail->to('noreply@yourdomain.com');
scrowler
  • 24,273
  • 9
  • 60
  • 92
1

I will just use the addBcc function of CakeEmail and modify the loop:

App::uses('CakeEmail', 'Network/Email');
$CakeEmail = new CakeEmail('default');

// list
foreach ($all as $user) {
    $CakeEmail->addBcc($user['Spoutnik']['email']);
}

$website_short_name = Configure::read('website.short_name');
$CakeEmail->subject(''.$website_short_name.' :: '.$d['subject'].'');

$CakeEmail->viewVars(array(
    'message' => (''.$d['message'].'')
));
Domingo C.
  • 789
  • 4
  • 15
  • not working :( i put a debug: `code` [protected] _bcc => array( 'example1@mail.com' => 'example1@mail.com', 'example2@mail.com' => 'example2@mail.com', 'example3@mail.com' => 'example3@mail.com' ) `code` – zelocalhost Oct 09 '13 at 11:50
  • If you are using an array for addBcc the key must be the email and the value the name of the person. But with my example you can just use an string, addBcc('string') or addBcc(array('email' => 'name')) – Domingo C. Oct 09 '13 at 11:55
  • still not working, and $CakeEmail->to is not send too – zelocalhost Oct 09 '13 at 12:05
  • Whats the output of debug($CakeEmail)? – Domingo C. Oct 09 '13 at 12:22
  • i don't understand the correct format for bcc or addBcc, please give me a example with 2 email adress , without query – zelocalhost Oct 09 '13 at 13:20
  • $CakeEmail->addBcc('1@m.com'); $CakeEmail->addBcc('2@m.com'); $CakeEmail->send(); – Domingo C. Oct 09 '13 at 14:30
  • Another option is $CakeEmail->addBcc(array('1@m.com' => 'name1', '2@m.com' => 'name2')); $CakeEmail->send(); – Domingo C. Oct 09 '13 at 14:32
  • For what it's worth, this way worked for me. Otherwise, I could not send to more than a single recipient. It would always complain that the "name" was not in proper email format (well DUH, it's supposed to use the "email"). It's probably because we're still using CakePHP version 2.3.6. – UncaAlby Aug 21 '18 at 22:12
0

Try changing your $bcc block to this:

// list
$bcc = array();
foreach ($all as $user) {
    $bcc[]= $user['Spoutnik']['email'];
}

Also refer to CakeEmail Documentation

Deepak
  • 6,684
  • 18
  • 69
  • 121