Adding an PDF attachment when using Zend_Mail
Asked Answered
S

1

5

What is the right way to add an attachment when using Zend_Mail? I keep getting the following error when I try to open the attached pdf in the sent mail: "Cannot extract the embedded font 'BAAAAAA+ArialMT'. Some characters may not display or print correctly." The PDF shows only the table but no characters.

This is very weird because the PDF opens corectly if i download it directly from the server or on my localhost.

This is the code I used for sending the attachement:

$html = $view->render('email/invoice.phtml');
$mail = new Zend_Mail("utf-8");

$file = PUBLIC_PATH . DS . 'data' . DS . $invoice . '.pdf';
$at = new Zend_Mime_Part(file_get_contents($file));
$at->filename = basename($file);
$at->disposition = Zend_Mime::DISPOSITION_ATTACHMENT;
$at->encoding = Zend_Mime::ENCODING_8BIT;
$mail->addAttachment($at);

/* Here i add the attachment */
$mail->setBodyHtml($html);
$mail->addTo($order->email, 'Factura '. $invoice . ' '.Zend_Registry::get('siteName'));
$mail->setFrom('[email protected]', Zend_Registry::get('siteName'));
$mail->setSubject('Factura '. $invoice . ' '.Zend_Registry::get('siteName'));
$mail->send();
Samiel answered 6/11, 2013 at 15:53 Comment(0)
M
26

Here is the right way to do it,

$mail = new Zend_Mail();
$mail->setBodyHtml("description");
$mail->setFrom('id', 'name');
$mail->addTo(email, name);
$mail->setSubject(subject);

$content = file_get_contents("path to pdf file"); // e.g. ("attachment/abc.pdf")
$attachment = new Zend_Mime_Part($content);
$attachment->type = 'application/pdf';
$attachment->disposition = Zend_Mime::DISPOSITION_ATTACHMENT;
$attachment->encoding = Zend_Mime::ENCODING_BASE64;
$attachment->filename = 'filename.pdf'; // name of file

$mail->addAttachment($attachment);                  

$mail->send(); 
Macrography answered 7/11, 2013 at 5:51 Comment(1)
thank you, this is working. does it matter that you also changed the order of the part where i'm loding the pdf attachment (it comes after setting subject, addTo and email Body), or only the ENCODING is important?Samiel

© 2022 - 2024 — McMap. All rights reserved.