cakephp - $Email->attachments() does not attach file -
we using cake's e-mail class send emails attachments. works fine cases except 1 , not able figure out problem is.
process:
- a pdf-file created & written file-system (file written correctly , exists)
- when email sent attachment 0bytes in size (whereas file attach created correctly in file-system)
working code:
// write invoice file
$cakepdf->write(app . 'tmp' . ds . 'invoices' . ds . $invoiceno . '.pdf');
[...]
// send invoice customer
$email = new cakeemail('invoice');
$email->attachments(app . 'tmp' . ds . 'invoices' . ds . $invoiceno . '.pdf');
$email->to($this->invoice->customer->getemailadress($customerid));
$email->viewvars(array('invoice_no' => $invoiceno));
$email->send();
not working code (attachment 0 bytes in size):
$cakepdf->write(app . 'tmp'. ds .'certificates' . ds . $certloginid . $certcourseid . '.pdf');
[...]
// send certificate customer
$email = new cakeemail('certificate');
$email->attachments(app . 'tmp'. ds .'certificates' . ds . $certloginid . $certcourseid . '.pdf');
$email->to($emailofuser);
$email->viewvars(array('coursename' => $certcoursename, 'probandname' => $probandname));
$email->send();
edit - there no typo correctly set. problem seems be, generation of pdf tcpdf runs asycnronously in background. when cake tries attach file not written file system completly. cannot attached.
if tried let script sleep while no success:
echo '<br>'; echo $path_to_certificate; echo '<br>'; echo filesize($path_to_certificate); sleep(10); echo '<br>'; echo $path_to_certificate; echo '<br>'; echo filesize($path_to_certificate); echo '<br>'; sleep(10); echo $path_to_certificate; echo '<br>'; echo filesize($path_to_certificate);
outputs:
c:\xampp\htdocs\www\eflux_frontend\app\tmp\certificates\13750.pdf 0 c:\xampp\htdocs\www\eflux_frontend\app\tmp\certificates\13750.pdf 0 c:\xampp\htdocs\www\eflux_frontend\app\tmp\certificates\13750.pdf 0
whereas file generated in meantime, because can see & access file in filesystem. isn't locking problem because other code works in different place, file generated smaller not take time process.
how can ensure generation process complete?
it seems not able ensure, pdf created before attaching email (maybe someon can give me hand here).
due fact, created pdf written database ugly workaround possible: after pdf written database, can take out of database, write file using cakefilehandler , attach email works me:
// workaround $this->certificate->recursive = -1; $data = $this->certificate->findbyid($cert_id); $pdf = base64_decode($data['certificate']['certificate_pdf']); $path_to_certificate = app . 'tmp'. ds .'certificates' . ds . $certloginid . $certcourseid . '.pdf'; $certificate_file = new file($path_to_certificate); $certificate_file->write($pdf); [do mail stuff] $certificate_file->delete(); $certificate_file->close();
Comments
Post a Comment