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
[email protected] 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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
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 `<img src="cid:abc123" />` 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: "%s"', $fileInfo['file']));
-			}
-			if (is_int($name)) {
-				$name = basename($fileInfo['file']);
+			  if (!isset($fileInfo['data'])) {
+			    throw new SocketException(__d('cake_dev', 'No file or data specified.'));
+			  }
+			  if (is_int($name)) {
+			    throw new SocketException(__d('cake_dev', 'No filename specified.'));
+			  }
+			} else {  
+  			$fileInfo['file'] = realpath($fileInfo['file']);
+  			if ($fileInfo['file'] === false || !file_exists($fileInfo['file'])) {
+  				throw new SocketException(__d('cake_dev', 'File not found: "%s"', $fileInfo['file']));
+  			}
+  			if (is_int($name)) {
+  				$name = basename($fileInfo['file']);
+  			}
 			}
 			if (!isset($fileInfo['mimetype'])) {
 				$fileInfo['mimetype'] = 'application/octet-stream';
@@ -1350,8 +1366,8 @@ class CakeEmail {
 			if (!empty($fileInfo['contentId'])) {
 				continue;
 			}
-			$data = $this->_readFile($fileInfo['file']);
-
+			$data = isset($fileInfo['data'])?$fileInfo['data']:$this->_readFile($fileInfo['file']);
+			
 			$msg[] = '--' . $boundary;
 			$msg[] = 'Content-Type: ' . $fileInfo['mimetype'];
 			$msg[] = 'Content-Transfer-Encoding: base64';
@@ -1393,8 +1409,8 @@ class CakeEmail {
 			if (empty($fileInfo['contentId'])) {
 				continue;
 			}
-			$data = $this->_readFile($fileInfo['file']);
-
+			$data = isset($fileInfo['data'])?$fileInfo['data']:$this->_readFile($fileInfo['file']);
+			
 			$msg[] = '--' . $boundary;
 			$msg[] = 'Content-Type: ' . $fileInfo['mimetype'];
 			$msg[] = 'Content-Transfer-Encoding: base64';

Reply via email to