Вопрос

The CakeEmail help page states that the data option has been added as of 2.4, so you no long have to have a physical file to add an attachment to an email.

I've got the following code:

$Email->from(array($this->Session->read('Auth.User.email') => $this->Session->read('Auth.User.name')))
->to($this->request->data['email-to'])
->subject($this->request->data['email-subject'])
->attachments(array('attachement1.pdf', array('data' => $pdf)))
->send($this->request->data['email-message']);

But whenever I run that I get an Internal Error saying File Not Found: "".

Это было полезно?

Решение

I had a look at the source code (which I'm beginning to learn is often more useful than reading the documentation!): https://github.com/cakephp/cakephp/blob/master/lib/Cake/Network/Email/CakeEmail.php

Changing my code to:

$Email = new CakeEmail('default');
$Email->from(array($this->Session->read('Auth.User.email') => $this->Session->read('Auth.User.name')))
->to($this->request->data['email-to'])
->subject($this->request->data['email-subject'])
->attachments(array('attachement1.pdf' => array('data' => $pdf, 'mimetype' => 'application/pdf')))
->send($this->request->data['email-message']);

Notice on the attachments line, the array is assigned to the filename variable, rather than passed in as a parameter!

For completeness, if anyone else is reading this and is wondering how I am generating my PDF with CakePDF:

// Create PDF for attachment
$CakePdf = new CakePdf();
$CakePdf->template('claim', 'default');
//get the pdf string returned
$pdf = $CakePdf->output();
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top