Adding an PDF attachment when using Zend_Mail

14,242

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(); 
Share:
14,242
Tudor Ravoiu
Author by

Tudor Ravoiu

Updated on June 04, 2022

Comments

  • Tudor Ravoiu
    Tudor Ravoiu almost 2 years

    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();
    
  • Tudor Ravoiu
    Tudor Ravoiu over 10 years
    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?