Skip to content

Commit 1b8165d

Browse files
committed
Enhancing email component to allow using alias for attachments. Closes #6
1 parent fdd8a51 commit 1b8165d

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

cake/libs/controller/components/email.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -556,15 +556,15 @@ function _createHeader() {
556556
if ($this->delivery == 'smtp') {
557557
$headers['Subject'] = $this->_encode($this->subject);
558558
}
559-
559+
560560
if ($this->messageId !== false) {
561561
if ($this->messageId === true) {
562562
$headers['Message-ID'] = '<' . String::UUID() . '@' . env('HTTP_HOST') . '>';
563563
} else {
564-
$headers['Message-ID'] = $this->messageId;
564+
$headers['Message-ID'] = $this->messageId;
565565
}
566566
}
567-
567+
568568
$headers['X-Mailer'] = $this->xMailer;
569569

570570
if (!empty($this->headers)) {
@@ -624,14 +624,17 @@ function _formatMessage($message) {
624624
*/
625625
function _attachFiles() {
626626
$files = array();
627-
foreach ($this->attachments as $attachment) {
627+
foreach ($this->attachments as $filename => $attachment) {
628628
$file = $this->_findFiles($attachment);
629629
if (!empty($file)) {
630-
$files[] = $file;
630+
if (is_int($filename)) {
631+
$filename = basename($file);
632+
}
633+
$files[$filename] = $file;
631634
}
632635
}
633636

634-
foreach ($files as $file) {
637+
foreach ($files as $filename => $file) {
635638
$handle = fopen($file, 'rb');
636639
$data = fread($handle, filesize($file));
637640
$data = chunk_split(base64_encode($data)) ;
@@ -640,7 +643,7 @@ function _attachFiles() {
640643
$this->__message[] = '--' . $this->__boundary;
641644
$this->__message[] = 'Content-Type: application/octet-stream';
642645
$this->__message[] = 'Content-Transfer-Encoding: base64';
643-
$this->__message[] = 'Content-Disposition: attachment; filename="' . basename($file) . '"';
646+
$this->__message[] = 'Content-Disposition: attachment; filename="' . basename($filename) . '"';
644647
$this->__message[] = '';
645648
$this->__message[] = $data;
646649
$this->__message[] = '';

cake/tests/cases/libs/controller/components/email.test.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,32 @@ function testMultibyte() {
779779
*
780780
* @return void
781781
* @access public
782+
*/
783+
function testSendWithAttachments() {
784+
$this->Controller->EmailTest->to = 'postmaster@localhost';
785+
$this->Controller->EmailTest->from = 'noreply@example.com';
786+
$this->Controller->EmailTest->subject = 'Attachment Test';
787+
$this->Controller->EmailTest->replyTo = 'noreply@example.com';
788+
$this->Controller->EmailTest->template = null;
789+
$this->Controller->EmailTest->delivery = 'debug';
790+
$this->Controller->EmailTest->attachments = array(
791+
__FILE__,
792+
'some-name.php' => __FILE__
793+
);
794+
$body = '<p>This is the body of the message</p>';
795+
796+
$this->Controller->EmailTest->sendAs = 'text';
797+
$this->assertTrue($this->Controller->EmailTest->send($body));
798+
$msg = $this->Controller->Session->read('Message.email.message');
799+
$this->assertPattern('/' . preg_quote('Content-Disposition: attachment; filename="email.test.php"') . '/', $msg);
800+
$this->assertPattern('/' . preg_quote('Content-Disposition: attachment; filename="some-name.php"') . '/', $msg);
801+
}
802+
803+
/**
804+
* testSendAsIsNotIgnoredIfAttachmentsPresent method
805+
*
806+
* @return void
807+
* @access public
782808
*/
783809
function testSendAsIsNotIgnoredIfAttachmentsPresent() {
784810
$this->Controller->EmailTest->to = 'postmaster@localhost';
@@ -812,7 +838,7 @@ function testSendAsIsNotIgnoredIfAttachmentsPresent() {
812838
}
813839

814840
/**
815-
* undocumented function
841+
* testNoDoubleNewlinesInHeaders function
816842
*
817843
* @return void
818844
* @access public

0 commit comments

Comments
 (0)