Skip to content

Commit

Permalink
EmailComponent: Accept arrays for $to variable (closes #293)
Browse files Browse the repository at this point in the history
  • Loading branch information
rchavik committed Feb 4, 2010
1 parent 3836592 commit a20c809
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions cake/libs/controller/components/email.php
Expand Up @@ -363,7 +363,7 @@ function send($content = null, $template = null, $layout = null) {
*/
function reset() {
$this->template = null;
$this->to = null;
$this->to = array();
$this->from = null;
$this->replyTo = null;
$this->return = null;
Expand Down Expand Up @@ -473,7 +473,11 @@ function __createBoundary() {
*/
function __createHeader() {
if ($this->delivery == 'smtp') {
$this->__header[] = 'To: ' . $this->__formatAddress($this->to);
if (is_array($this->to)) {
$this->__header[] = 'To: ' . implode(', ', array_map(array($this, '__formatAddress'), $this->to));
} else {
$this->__header[] = 'To: ' . $this->__formatAddress($this->to);
}
}
$this->__header[] = 'From: ' . $this->__formatAddress($this->from);

Expand Down Expand Up @@ -692,10 +696,15 @@ function __strip($value, $message = false) {
function __mail() {
$header = implode("\n", $this->__header);
$message = implode("\n", $this->__message);
if (is_array($this->to)) {
$to = implode(', ', array_map(array($this, '__formatAddress'), $this->to));
} else {
$to = $this->to;
}
if (ini_get('safe_mode')) {
return @mail($this->to, $this->__encode($this->subject), $message, $header);
return @mail($to, $this->__encode($this->subject), $message, $header);
}
return @mail($this->to, $this->__encode($this->subject), $message, $header, $this->additionalParams);
return @mail($to, $this->__encode($this->subject), $message, $header, $this->additionalParams);
}

/**
Expand Down Expand Up @@ -748,8 +757,15 @@ function __smtp() {
return false;
}

if (!$this->__smtpSend('RCPT TO: ' . $this->__formatAddress($this->to, true))) {
return false;
if (!is_array($this->to)) {
$tos = array($this->to);
} else {
$tos = $this->to;
}
foreach ($tos as $to) {
if (!$this->__smtpSend('RCPT TO: ' . $this->__formatAddress($to, true))) {
return false;
}
}

foreach ($this->cc as $cc) {
Expand Down Expand Up @@ -814,12 +830,17 @@ function __debug() {
$message = implode($nl, $this->__message);
$fm = '<pre>';

if (is_array($this->to)) {
$to = implode(', ', array_map(array($this, '__formatAddress'), $this->to));
} else {
$to = $this->to;
}
if ($this->delivery == 'smtp') {
$fm .= sprintf('%s %s%s', 'Host:', $this->smtpOptions['host'], $nl);
$fm .= sprintf('%s %s%s', 'Port:', $this->smtpOptions['port'], $nl);
$fm .= sprintf('%s %s%s', 'Timeout:', $this->smtpOptions['timeout'], $nl);
}
$fm .= sprintf('%s %s%s', 'To:', $this->to, $nl);
$fm .= sprintf('%s %s%s', 'To:', $to, $nl);
$fm .= sprintf('%s %s%s', 'From:', $this->from, $nl);
$fm .= sprintf('%s %s%s', 'Subject:', $this->__encode($this->subject), $nl);
$fm .= sprintf('%s%3$s%3$s%s', 'Header:', $header, $nl);
Expand Down

0 comments on commit a20c809

Please sign in to comment.