Adding an PDF attachment when using Zend_Mail

Tudor Ravoiu picture Tudor Ravoiu · Nov 6, 2013 · Viewed 14k times · Source

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();

Answer

Ronak K picture Ronak K · Nov 7, 2013

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();