Skip to content

Commit

Permalink
Added the methods to format message.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrbasso committed Apr 13, 2011
1 parent 3a1ebf1 commit c5cbb60
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
50 changes: 49 additions & 1 deletion lib/Cake/Network/CakeEmail.php
Expand Up @@ -606,6 +606,22 @@ public function getHeaders($include = array()) {
$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;
}

Expand Down Expand Up @@ -764,7 +780,7 @@ public function send($content = null) {

$message = $this->_wrap($content);
if (empty($this->template)) {
//$message = $this->_formatMessage($message);
$message = $this->_formatMessage($message);
} else {
//$message = $this->_render($message);
}
Expand Down Expand Up @@ -933,4 +949,36 @@ function _wrap($message) {
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;
}

}
16 changes: 12 additions & 4 deletions lib/Cake/tests/Case/Network/CakeEmailTest.php
Expand Up @@ -232,7 +232,9 @@ public function testHeaders() {
$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);

Expand All @@ -241,7 +243,9 @@ public function testHeaders() {
'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);

Expand All @@ -253,7 +257,9 @@ public function testHeaders() {
'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);

Expand All @@ -268,7 +274,9 @@ public function testHeaders() {
'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);
}
Expand Down

0 comments on commit c5cbb60

Please sign in to comment.