【发布时间】:2013-04-25 21:15:32
【问题描述】:
单击按钮时,我正在使用 mPDF 生成 PDF。我正在寻找一种使用 PHPmailer 将 PDF 添加到附件的方法。这是我尝试过的:
$mpdf = new mPDF();
$mpdf->WriteHTML($output);
$emailAttachment = $mpdf->Output('filename.pdf','S');
$emailAttachment = chunk_split(base64_encode($emailAttachment));
require_once('/inc/user_controller.php');
require_once('/inc/PHPMailer/class.phpmailer.php');
$user = new user();
$mail = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch
$currentUserInfo = $user->userDetails($userId);
try {
$mail->AddAddress($currentUserInfo['emailAddress'], $currentUserInfo['firstName']);
$mail->SetFrom('name@yourdomain.com', 'First Last');
$mail->AddReplyTo('name@yourdomain.com', 'First Last');
$mail->Subject = 'Your file is attached';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML("This is a test");
$mail->AddAttachment($emailAttachment); // attachment
$mail->Send();
echo "Message Sent OK</p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
我收到此错误“无法访问文件:”然后是一堆垃圾,我认为它们来自 base64_encode。理想情况下,PDF 将显示或下载并通过电子邮件发送,而无需在我的服务器上保存副本。我可以忍受正在创建的临时文件,但还没有尝试过。
我已经测试了电子邮件功能,它无需添加附件即可工作,因此我知道问题出在 mPDF 并正确附加。
【问题讨论】:
-
我正在寻找对 mpdf 创建的空白电子邮件 pdf 的修复。您是否能够找到可以让您成功发送的修复程序?
-
这可能很有用,如果你使用 Swiftmailer:stackoverflow.com/a/34785821/386579