Added the methods to format message.

This commit is contained in:
Juan Basso 2011-03-03 23:05:30 -03:00
parent 3a1ebf159a
commit c5cbb603a0
2 changed files with 61 additions and 5 deletions

View file

@ -606,6 +606,22 @@ class CakeEmail {
$headers['Subject'] = $this->_subject;
}
if (!empty($this->attachments)) {
$this->_createBoundary();
$headers['MIME-Version'] = '1.0';
$headers['Content-Type'] = 'multipart/mixed; boundary="' . $this->_boundary . '"';
$headers[] = 'This part of the E-mail should never be seen. If';
$headers[] = 'you are reading this, consider upgrading your e-mail';
$headers[] = 'client to a MIME-compatible client.';
} elseif ($this->_emailFormat === 'text') {
$headers['Content-Type'] = 'text/plain; charset=' . $this->charset;
} elseif ($this->_emailFormat === 'html') {
$headers['Content-Type'] = 'text/html; charset=' . $this->charset;
} elseif ($this->_emailFormat === 'both') {
$headers['Content-Type'] = 'multipart/alternative; boundary="alt-' . $this->_boundary . '"';
}
$headers['Content-Transfer-Encoding'] = '7bit';
return $headers;
}
@ -764,7 +780,7 @@ class CakeEmail {
$message = $this->_wrap($content);
if (empty($this->template)) {
//$message = $this->_formatMessage($message);
$message = $this->_formatMessage($message);
} else {
//$message = $this->_render($message);
}
@ -933,4 +949,36 @@ class CakeEmail {
return $formatted;
}
/**
* Create unique boundary identifier
*
* @return void
*/
function _createboundary() {
$this->_boundary = md5(uniqid(time()));
}
/**
* Format the message by seeing if it has attachments.
*
* @param array $message Message to format
* @return array
*/
function _formatMessage($message) {
if (!empty($this->_attachments)) {
$prefix = array('--' . $this->_boundary);
if ($this->_emailFormat === 'text') {
$prefix[] = 'Content-Type: text/plain; charset=' . $this->charset;
} elseif ($this->_emailFormat === 'html') {
$prefix[] = 'Content-Type: text/html; charset=' . $this->charset;
} elseif ($this->_emailFormat === 'both') {
$prefix[] = 'Content-Type: multipart/alternative; boundary="alt-' . $this->_boundary . '"';
}
$prefix[] = 'Content-Transfer-Encoding: 7bit';
$prefix[] = '';
$message = array_merge($prefix, $message);
}
return $message;
}
}

View file

@ -232,7 +232,9 @@ class CakeEmailTest extends CakeTestCase {
$expected = array(
'X-Something' => 'nice',
'X-Mailer' => 'CakePHP Email Component',
'Date' => date(DATE_RFC2822)
'Date' => date(DATE_RFC2822),
'Content-Type' => 'text/plain; charset=UTF-8',
'Content-Transfer-Encoding' => '7bit'
);
$this->assertIdentical($this->CakeEmail->getHeaders(), $expected);
@ -241,7 +243,9 @@ class CakeEmailTest extends CakeTestCase {
'X-Something' => 'very nice',
'X-Other' => 'cool',
'X-Mailer' => 'CakePHP Email Component',
'Date' => date(DATE_RFC2822)
'Date' => date(DATE_RFC2822),
'Content-Type' => 'text/plain; charset=UTF-8',
'Content-Transfer-Encoding' => '7bit'
);
$this->assertIdentical($this->CakeEmail->getHeaders(), $expected);
@ -253,7 +257,9 @@ class CakeEmailTest extends CakeTestCase {
'X-Something' => 'very nice',
'X-Other' => 'cool',
'X-Mailer' => 'CakePHP Email Component',
'Date' => date(DATE_RFC2822)
'Date' => date(DATE_RFC2822),
'Content-Type' => 'text/plain; charset=UTF-8',
'Content-Transfer-Encoding' => '7bit'
);
$this->assertIdentical($this->CakeEmail->getHeaders(array('from' => true)), $expected);
@ -268,7 +274,9 @@ class CakeEmailTest extends CakeTestCase {
'X-Something' => 'very nice',
'X-Other' => 'cool',
'X-Mailer' => 'CakePHP Email Component',
'Date' => date(DATE_RFC2822)
'Date' => date(DATE_RFC2822),
'Content-Type' => 'text/plain; charset=UTF-8',
'Content-Transfer-Encoding' => '7bit'
);
$this->assertIdentical($this->CakeEmail->getHeaders(array('from' => true, 'to' => true)), $expected);
}