EmailComponent: Accept arrays for $to variable (closes #293)

This commit is contained in:
rchavik 2010-02-04 09:13:11 +07:00
parent 38365924eb
commit a20c809796

View file

@ -363,7 +363,7 @@ class EmailComponent extends Object{
*/ */
function reset() { function reset() {
$this->template = null; $this->template = null;
$this->to = null; $this->to = array();
$this->from = null; $this->from = null;
$this->replyTo = null; $this->replyTo = null;
$this->return = null; $this->return = null;
@ -473,8 +473,12 @@ class EmailComponent extends Object{
*/ */
function __createHeader() { function __createHeader() {
if ($this->delivery == 'smtp') { if ($this->delivery == 'smtp') {
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[] = 'To: ' . $this->__formatAddress($this->to);
} }
}
$this->__header[] = 'From: ' . $this->__formatAddress($this->from); $this->__header[] = 'From: ' . $this->__formatAddress($this->from);
if (!empty($this->replyTo)) { if (!empty($this->replyTo)) {
@ -692,10 +696,15 @@ class EmailComponent extends Object{
function __mail() { function __mail() {
$header = implode("\n", $this->__header); $header = implode("\n", $this->__header);
$message = implode("\n", $this->__message); $message = implode("\n", $this->__message);
if (ini_get('safe_mode')) { if (is_array($this->to)) {
return @mail($this->to, $this->__encode($this->subject), $message, $header); $to = implode(', ', array_map(array($this, '__formatAddress'), $this->to));
} else {
$to = $this->to;
} }
return @mail($this->to, $this->__encode($this->subject), $message, $header, $this->additionalParams); if (ini_get('safe_mode')) {
return @mail($to, $this->__encode($this->subject), $message, $header);
}
return @mail($to, $this->__encode($this->subject), $message, $header, $this->additionalParams);
} }
/** /**
@ -748,9 +757,16 @@ class EmailComponent extends Object{
return false; return false;
} }
if (!$this->__smtpSend('RCPT TO: ' . $this->__formatAddress($this->to, true))) { 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; return false;
} }
}
foreach ($this->cc as $cc) { foreach ($this->cc as $cc) {
if (!$this->__smtpSend('RCPT TO: ' . $this->__formatAddress($cc, true))) { if (!$this->__smtpSend('RCPT TO: ' . $this->__formatAddress($cc, true))) {
@ -814,12 +830,17 @@ class EmailComponent extends Object{
$message = implode($nl, $this->__message); $message = implode($nl, $this->__message);
$fm = '<pre>'; $fm = '<pre>';
if (is_array($this->to)) {
$to = implode(', ', array_map(array($this, '__formatAddress'), $this->to));
} else {
$to = $this->to;
}
if ($this->delivery == 'smtp') { if ($this->delivery == 'smtp') {
$fm .= sprintf('%s %s%s', 'Host:', $this->smtpOptions['host'], $nl); $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', 'Port:', $this->smtpOptions['port'], $nl);
$fm .= sprintf('%s %s%s', 'Timeout:', $this->smtpOptions['timeout'], $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', 'From:', $this->from, $nl);
$fm .= sprintf('%s %s%s', 'Subject:', $this->__encode($this->subject), $nl); $fm .= sprintf('%s %s%s', 'Subject:', $this->__encode($this->subject), $nl);
$fm .= sprintf('%s%3$s%3$s%s', 'Header:', $header, $nl); $fm .= sprintf('%s%3$s%3$s%s', 'Header:', $header, $nl);