【问题标题】:Adding an PDF attachment when using Zend_Mail使用 Zend_Mail 时添加 PDF 附件
【发布时间】:2013-11-17 23:00:57
【问题描述】:

使用 Zend_Mail 时添加附件的正确方法是什么?当我尝试打开已发送邮件中附加的 pdf 时,我不断收到以下错误:“无法提取嵌入字体 'BAAAAAA+ArialMT'。某些字符可能无法正确显示或打印。” PDF 仅显示表格,不显示字符。

这很奇怪,因为如果我直接从服务器或本地主机上下载 PDF,它会正确打开。

这是我用来发送附件的代码:

$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('vanzari@anunt.com', Zend_Registry::get('siteName'));
$mail->setSubject('Factura '. $invoice . ' '.Zend_Registry::get('siteName'));
$mail->send();

【问题讨论】:

    标签: php email zend-framework attachment zend-mail


    【解决方案1】:

    这是正确的做法,

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

    【讨论】:

    • 谢谢,这行得通。您是否还更改了我正在加载 pdf 附件的部分的顺序(它在设置主题、addTo 和电子邮件正文之后),还是只有编码很重要?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-12
    • 2010-11-11
    • 2015-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多