Re: CakeEmail -> attachments
Hi Paul, Is there a reason you are not using "$this->render()->body()" like this? public function sendConfirmation() { $booking = $this->Booking->find('first', array( 'conditions'=>array('Booking.id'=>$this->Booking->id), )); $this->set(array('booking' => $booking)); $email->viewVars(array('booking' => $booking)); $this->autoRender = false; $invoiceData = $this->render('pdf/invoice', 'pdf/default')->body(); $filename = TMP . 'invoice-'.$this->Booking->id.'.pdf'; file_put_contents($filename, $invoiceData); $email = new CakeEmail('default'); $email->template('Bookings/confirmation', 'default') ->emailformat('both') ->to($booking['Organisation']['bookings_email']) ->subject('NEAS Training System: Booking Confirmation') ->attachments(array('invoice.pdf'=>$filename)) ->send(); unlink($filename); } Even like that I am not happy. I really wanted this line to change: ->attachments(array('invoice.pdf'=>$invoiceData)) to: ->attachments(array('invoice.pdf'=>array('data'=>$invoiceData))) and it was not that hard, see "email.diff" in attachment. /lib/Cake/Network/Email/CakeEmail.php: Attach a file from string and specify additional properties: $email->attachments(array('custom_name.png' => array( 'data' => file_get_contents('path/to/file'), 'mimetype' => 'image/png', 'contentId' => 'abc123' )); Unfortunately the 'data' key is not (yet) supported. Hopefully it will be in some new version. Kind regards, Maurits On 02/12/2012 01:06 PM, phpMagpie wrote: OK, I ended up with a solution which saved me making an extra request when I realised I could render /bookings/invoice/%id%.pdf into a variable and temporarily save that to the server to be attached: public function sendConfirmation() { $email = new CakeEmail('default'); $booking = $this->Booking->find('first', array( 'conditions'=>array('Booking.id'=>$this->Booking->id), )); $email->viewVars(array( 'booking' => $booking )); $invoicePath = $this->getTmpFile(); $email->template('Bookings/confirmation', 'default') ->emailformat('both') ->to($booking['Organisation']['bookings_email']) ->subject('NEAS Training System: Booking Confirmation') ->attachments(array('invoice.pdf'=>$invoicePath)) ->send(); unlink($invoicePath); } public function getTmpFile() { $this->autoRender = false; $view = new View($this, false); $view->set('booking', $this->Booking->find('first', array( 'conditions' => array('Booking.id' => $this->Booking->id) ))); $view->viewPath = 'Bookings'; $file = $view->render('pdf/invoice', 'pdf/default'); $filename = TMP . 'invoice-'.$this->Booking->id.'.pdf'; $handle = fopen($filename, 'w+'); fwrite($handle, $file); fclose($handle); return $filename; } There may be a better way to render a view into a variable, but I got this method from reading: http://wp.headynation.com/cakephp-get-htmloutput-of-view-without-view-displaying-or-outputting-using-render/ HTH, Paul. -- Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions. To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php -- Like Us on FacekBook https://www.facebook.com/CakePHP Find us on Twitter http://twitter.com/CakePHP --- You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com. To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com. Visit this group at http://groups.google.com/group/cake-php?hl=en. diff --git a/lib/Cake/Network/Email/CakeEmail.php b/lib/Cake/Network/Email/CakeEmail.php index 9e5ed09..70d1e13 100644 --- a/lib/Cake/Network/Email/CakeEmail.php +++ b/lib/Cake/Network/Email/CakeEmail.php @@ -949,6 +949,16 @@ class CakeEmail { * 'contentId' => 'abc123' * )); * }}} + * + * Attach a file from string and specify additional properties: + * + * {{{ + * $email->attachments(array('custom_name.png' => array( + * 'data' => file_get_contents('path/to/file'), + * 'mimetype' => 'image/png', + * 'contentId' => 'abc123' + * )); + * }}} * * The `contentId` key allows you to specify an inline attachment. In your email text, you * can use `` to display the image inline. @@ -967,14 +977,20 @@ class CakeEmail { $fileInfo = array('file' => $fileInfo); } if (!isset($fileInfo['file'])) { -throw new SocketException(__d('cake_dev', 'File not specified.')); - } - $fileInfo['file'] = realpath($fileInfo['file']); - if ($fileInfo['file'] === false || !file_exists($fileInfo['file'])) { -throw new SocketException(__d('cake_dev
Re: CakeEmail -> attachments
Hi Paul, Is there a reason you are not using "$this->render()->body()" like this? public function sendConfirmation() { $booking = $this->Booking->find('first', array( 'conditions'=>array('Booking.id'=>$this->Booking->id), )); $this->set(array('booking' => $booking)); $email->viewVars(array('booking' => $booking)); $this->autoRender = false; $invoiceData = $this->render('pdf/invoice', 'pdf/default')->body(); $filename = TMP . 'invoice-'.$this->Booking->id.'.pdf'; file_put_contents($filename, $invoiceData); $email = new CakeEmail('default'); $email->template('Bookings/confirmation', 'default') ->emailformat('both') ->to($booking['Organisation']['bookings_email']) ->subject('NEAS Training System: Booking Confirmation') ->attachments(array('invoice.pdf'=>$filename)) ->send(); unlink($filename); } Even like that I am not happy. I really wanted this line to change: ->attachments(array('invoice.pdf'=>$invoiceData)) to: ->attachments(array('invoice.pdf'=>array('data'=>$invoiceData))) and it was not that hard, see "email.diff" in attachment. /lib/Cake/Network/Email/CakeEmail.php: Attach a file from string and specify additional properties: $email->attachments(array('custom_name.png' => array( 'data' => file_get_contents('path/to/file'), 'mimetype' => 'image/png', 'contentId' => 'abc123' )); Unfortunately the 'data' key is not (yet) supported. Hopefully it will be in some new version. Kind regards, Maurits On 02/12/2012 01:06 PM, phpMagpie wrote: OK, I ended up with a solution which saved me making an extra request when I realised I could render /bookings/invoice/%id%.pdf into a variable and temporarily save that to the server to be attached: public function sendConfirmation() { $email = new CakeEmail('default'); $booking = $this->Booking->find('first', array( 'conditions'=>array('Booking.id'=>$this->Booking->id), )); $email->viewVars(array( 'booking' => $booking )); $invoicePath = $this->getTmpFile(); $email->template('Bookings/confirmation', 'default') ->emailformat('both') ->to($booking['Organisation']['bookings_email']) ->subject('NEAS Training System: Booking Confirmation') ->attachments(array('invoice.pdf'=>$invoicePath)) ->send(); unlink($invoicePath); } public function getTmpFile() { $this->autoRender = false; $view = new View($this, false); $view->set('booking', $this->Booking->find('first', array( 'conditions' => array('Booking.id' => $this->Booking->id) ))); $view->viewPath = 'Bookings'; $file = $view->render('pdf/invoice', 'pdf/default'); $filename = TMP . 'invoice-'.$this->Booking->id.'.pdf'; $handle = fopen($filename, 'w+'); fwrite($handle, $file); fclose($handle); return $filename; } There may be a better way to render a view into a variable, but I got this method from reading: http://wp.headynation.com/cakephp-get-htmloutput-of-view-without-view-displaying-or-outputting-using-render/ HTH, Paul. -- Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions. To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php -- Like Us on FacekBook https://www.facebook.com/CakePHP Find us on Twitter http://twitter.com/CakePHP --- You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to cake-php@googlegroups.com. To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com. Visit this group at http://groups.google.com/group/cake-php?hl=en. diff --git a/lib/Cake/Network/Email/CakeEmail.php b/lib/Cake/Network/Email/CakeEmail.php index 9e5ed09..70d1e13 100644 --- a/lib/Cake/Network/Email/CakeEmail.php +++ b/lib/Cake/Network/Email/CakeEmail.php @@ -949,6 +949,16 @@ class CakeEmail { * 'contentId' => 'abc123' * )); * }}} + * + * Attach a file from string and specify additional properties: + * + * {{{ + * $email->attachments(array('custom_name.png' => array( + * 'data' => file_get_contents('path/to/file'), + * 'mimetype' => 'image/png', + * 'contentId' => 'abc123' + * )); + * }}} * * The `contentId` key allows you to specify an inline attachment. In your email text, you * can use `` to display the image inline. @@ -967,14 +977,20 @@ class CakeEmail { $fileInfo = array('file' => $fileInfo); } if (!isset($fileInfo['file'])) { -throw new SocketException(__d('cake_dev', 'File not specified.')); - } - $fileInfo['file'] = realpath($fileInfo['file']); - if ($fileInfo['file'] === false || !file_exists($fileInfo['file'])) { -throw new SocketException(__d('cake_dev', 'File not found:
Re: CakeEmail -> attachments
OK, I ended up with a solution which saved me making an extra request when I realised I could render /bookings/invoice/%id%.pdf into a variable and temporarily save that to the server to be attached: public function sendConfirmation() { $email = new CakeEmail('default'); $booking = $this->Booking->find('first', array( 'conditions'=>array('Booking.id'=>$this->Booking->id), )); $email->viewVars(array( 'booking' => $booking )); $invoicePath = $this->getTmpFile(); $email->template('Bookings/confirmation', 'default') ->emailformat('both') ->to($booking['Organisation']['bookings_email']) ->subject('NEAS Training System: Booking Confirmation') ->attachments(array('invoice.pdf'=>$invoicePath)) ->send(); unlink($invoicePath); } public function getTmpFile() { $this->autoRender = false; $view = new View($this, false); $view->set('booking', $this->Booking->find('first', array( 'conditions' => array('Booking.id' => $this->Booking->id) ))); $view->viewPath = 'Bookings'; $file = $view->render('pdf/invoice', 'pdf/default'); $filename = TMP . 'invoice-'.$this->Booking->id.'.pdf'; $handle = fopen($filename, 'w+'); fwrite($handle, $file); fclose($handle); return $filename; } There may be a better way to render a view into a variable, but I got this method from reading: http://wp.headynation.com/cakephp-get-htmloutput-of-view-without-view-displaying-or-outputting-using-render/ HTH, Paul. -- Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions. To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php
Re: CakeEmail -> attachments
Update: Been on IRC and as I expected my code is a horrible hack ... I'm trying to understand how to improve it and will hopefully post some sensible code shortly. -- Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions. To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php
Re: CakeEmail -> attachments
OK, ended up delving into things myself and although I have committed the cardinal sin of hacking the core, I got things working as I needed: I got inspiration from: http://ask.cakephp.org/questions/view/dynamic_creating_a_pdf_and_send_as_attachment And here's a pastbin of the modificaions I made: http://bin.cakephp.org/saved/72500 This allows me to add dynamic attachments using the following: $email->attachments('invoice.pdf'=>array( 'url' => '/bookings/invoice/'.$id, 'ext' => 'pdf', 'dynamic' => 1 ) Would love someone to have a look at the changes and suggest how I could do this in a less hacky way. HTH, Paul -- Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions. To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php
CakeEmail -> attachments
Afternoon all, I am trying to attach a PDF (which I generate on the fly by accessing /bookings/invoice/%id%.pdf) to an email sent via CakeEmail, but as CakeEmail->attachment expects a /full/path/to/file the following results in a 'File not found: ""' error: $email->attachments(array('invoice.png' => '/bookings/invoice/1.pdf')) ; The idea is not to generate and store emails on the web server but only generate them when requested, so there is no physical file to attach. Is there a way I can get it to attach the output of /bookings/invoice/%id%.pdf? Thanks, Paul. -- Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions. To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php