#6529: Attaching PDFs to Emails
---------------------------+------------------------------------------------
    Reporter:  oh4real     |          Type:  Enhancement
      Status:  new         |      Priority:  Medium     
   Milestone:  1.2.x.x     |     Component:  Components 
     Version:  1.2 Final   |      Severity:  Normal     
    Keywords:              |   Php_version:  PHP 5      
Cake_version:  1.2.1.8004  |  
---------------------------+------------------------------------------------
 For portability/replication/etc. reasons, my app generates and stores PDFs
 (invoices, purchase orders, etc.) as MySQL blobs. I could either spend
 time figuring out how to pull the field from db, write to a tmp file, add
 that tmp filepath to Email->attachments[], and after Email->send()
 completes (success or failure) delete the tmp filepath.

 [[BR]]
 Instead, I modified the EmailComponent class's _attachFiles() method as
 below to accomplish this for me.
 [[BR]]
 My current modified Email->_attachFiles():
 {{{
         function __attachFiles() {
                 $files = array();
                 foreach ($this->attachments as $attachment) {
                         if(is_array($attachment)) {
                                 $files[] = $attachment;
                         } else {
                                 $file = $this->__findFiles($attachment);
                                 if (!empty($file)) {
                                         $files[] = $file;
                                 }
                         }
                 }

                 foreach ($files as $file) {
                         if(is_array($file)) {
                                 $data = current($file);
                                 $name = key($file);
                                 $data = chunk_split(base64_encode($data));
                                 $this->__message[] = '--' .
 $this->__boundary;
                                 $this->__message[] = 'Content-Type:
 application/octet-stream';
                                 $this->__message[] = 'Content-Transfer-
 Encoding: base64';
                                 $this->__message[] = 'Content-Disposition:
 attachment; filename="' . $name . '"';
                                 $this->__message[] = '';
                                 $this->__message[] = $data;
                                 $this->__message[] = '';
                         } else {
                                 $handle = fopen($file, 'rb');
                                 $data = fread($handle, filesize($file));
                                 $data = chunk_split(base64_encode($data));
                                 fclose($handle);

                                 $this->__message[] = '--' .
 $this->__boundary;
                                 $this->__message[] = 'Content-Type:
 application/octet-stream';
                                 $this->__message[] = 'Content-Transfer-
 Encoding: base64';
                                 $this->__message[] = 'Content-Disposition:
 attachment; filename="' . basename($file) . '"';
                                 $this->__message[] = '';
                                 $this->__message[] = $data;
                                 $this->__message[] = '';
                         }
                 }
         }
 }}}


 [[BR]]
 I think y'all are considering expanding Email->_attachFiles() to include
 array() inputs for Email->attachments[] where the key is the name and the
 value is the filepath - instead of simply using basename($file).  Perhaps
 adding a specific key ('blob_files'?) that tells the EmailComponent to NOT
 open, read, close a file - but just chunk-encode it.


 [[BR]]
 One suggestion for future to prevent conflict with your proposed
 Email->attachments = array('customName' => 'path_to_file') would be
 something like:

 {{{
         function __attachFiles() {
                 $files = array();
                 foreach ($this->attachments as $attachment) {
                         if(is_array($attachment)) {
                                 $files[] = $attachment;
                         } else {
                                 $file = $this->__findFiles($attachment);
                                 if (!empty($file)) {
                                         $files[] = $file;
                                 }
                         }
                 }

                 foreach ($files as $file) {
                         if(is_array($file)) {
                                 foreach($file as $filename => $filepath) {
                                         if($filename == 'blob_files') {
                                                 foreach($filepath as $name
 => $blob) {
                                                         $data =
 chunk_split(base64_encode($blob));
                                                         $this->__message[]
 = '--' . $this->__boundary;
                                                         $this->__message[]
 = 'Content-Type: application/octet-stream';
                                                         $this->__message[]
 = 'Content-Transfer-Encoding: base64';
                                                         $this->__message[]
 = 'Content-Disposition: attachment; filename="' . $name . '"';
                                                         $this->__message[]
 = '';
                                                         $this->__message[]
 = $data;
                                                         $this->__message[]
 = '';
                                                 }
                                         }
                                         else {
                                                 $handle = fopen($filepath,
 'rb');
                                                 $data = fread($handle,
 filesize($filepath));
                                                 $data =
 chunk_split(base64_encode($data));
                                                 fclose($handle);

                                                 $this->__message[] = '--'
 . $this->__boundary;
                                                 $this->__message[] =
 'Content-Type: application/octet-stream';
                                                 $this->__message[] =
 'Content-Transfer-Encoding: base64';
                                                 $this->__message[] =
 'Content-Disposition: attachment; filename="' . $filename . '"';
                                                 $this->__message[] = '';
                                                 $this->__message[] =
 $data;
                                                 $this->__message[] = '';
                                         }
                                 }
                         } else {
                                 $handle = fopen($file, 'rb');
                                 $data = fread($handle, filesize($file));
                                 $data = chunk_split(base64_encode($data));
                                 fclose($handle);

                                 $this->__message[] = '--' .
 $this->__boundary;
                                 $this->__message[] = 'Content-Type:
 application/octet-stream';
                                 $this->__message[] = 'Content-Transfer-
 Encoding: base64';
                                 $this->__message[] = 'Content-Disposition:
 attachment; filename="' . basename($file) . '"';
                                 $this->__message[] = '';
                                 $this->__message[] = $data;
                                 $this->__message[] = '';
                         }
                 }
         }
 }}}

 One could, of course, just check to see if $filepath with something like
 this pseudo-code:
 {{{
 (is_file|file_exists($filepath)) ? process $filepath as file : (($filepath
 is NOT a file path by REGEX) ? chunk_split(encode($filepath)) : ignore);
 }}}

-- 
Ticket URL: <https://trac.cakephp.org/ticket/6529>
CakePHP : The Rapid Development Framework for PHP <https://trac.cakephp.org/>
Cake is a rapid development framework for PHP which uses commonly known design 
patterns like ActiveRecord, Association Data Mapping, Front Controller and MVC. 
Our primary goal is to provide a structured framework that enables PHP users at 
all levels to rapidly develop robust web applications, without any loss to 
flexibility.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"tickets cakephp" group.
To post to this group, send email to tickets-cakephp@googlegroups.com
To unsubscribe from this group, send email to 
tickets-cakephp+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/tickets-cakephp?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to