You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP使用FPDM生成PDF作为邮件附件无法打开的技术问题

PDF表单填充后作为邮件附件损坏无法打开

我正在尝试将HTML表单填写的内容生成PDF表单并作为邮件附件发送。目前已通过fpdm的$pdf->Output()方法成功在浏览器中展示填充好数据的PDF,但将其作为邮件附件发送后,收到的.PDF附件无法打开。相关PHP代码如下:

require_once 'fpdm-master/fpdm.php';

if (isset($_POST['rentstatus']) && $_POST['rentstatus'] == 'Yes') {
    $rentstatus = "Yes";
} else {
    $rentstatus = "No";
}

/***************************
  Sample using a PHP array
****************************/
$name = $_POST['name'];
$email = $_POST['email'];
$useraddress = $_POST['address'];
$usercity = $_POST['city'];
$userstate = $_POST['state'];
$usercode = $_POST['code'];
$usercell = $_POST['cell'];
$userarival = $_POST['arival'];
$userdeparture = $_POST['departure'];

$fields = array(
    'name'=> $name,
    'phone'=> $usercell,
    'date'=> $userdeparture,
    'email'=> $email,
    'signature'=> $name,
    'datenow' => date("M d, Y"),
    'yes' => true,
    'no' => false
);

$pdf = new FPDM('template.pdf');
$pdf->Load($fields, false); // second parameter: false if field values are in ISO-8859-1, true if UTF-8
$pdf->Merge();
// encode data (puts attachment in proper format)
// attachment name
$filename = "test.pdf";

// encode data (puts attachment in proper format)
$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc)); 

$message = "\r\n Strata Lot: 28 of Strata Plan: EPS 7373 \r\n Name(s) of tenant(s): " . $name . "\r\n Telephone: " . $usercell . "\r\n Email: " . $email . "\r\n Tenancy commencing: " . $userdeparture . "\r\n Is this unit rented to a family member? - " . $rentstatus .  "\r\n Date: " . $datenow . "\r\n Address, Phone Number and Email Address of Landlord or Agent of Landlord: 103-1550 Duchess Ave, West Vancouver, B.C. V7V 1P5 \r\n Name of Landlord or Agent of Landlord: Donald Kim \r\n \r\n \r\n For more information, reply to this email and we will get back to you shortly. \r\n - Restavio Team";

$subject = "Form K for " . $name;
$fromname = " ";
$fromemail = " "; //if u dont have an email create one on your cpanel
$mailto = ' '; //the email which u want to recv this email
$secondmail = ' '; //the email which u want to recv this email

// carriage return type (RFC)
$eol = "\r\n";
// main header (multipart mandatory)
$headers = "From: " . $fromname . " <" . $fromemail . ">" . $eol;
$headers .= 'Cc: acubaniti@gmail.com' . "\r\n";
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
$headers .= "This is a MIME encoded message." . $eol;
// message
$body = "--" . $separator . $eol;
$body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
$body .= "Content-Transfer-Encoding: 8bit" . $eol;
$body .= $message . $eol;
// attachment
$body .= "--" .$separator.$eol;
$body .= "Content-Type: application/octet-stream; name=\"" .$filename. "\"" .$eol;
$body .= "Content-Transfer-Encoding: base64" .$eol;
$body .= "Content-Disposition: attachment" .$eol.$eol;
$body .= $attachment.$eol;
$body .= "--" .$separator."--";

//SEND Mail
if (mail($mailto, $subject, $body, $headers)) {
    echo "<p style='font-size:2rem; text-align:center'>Thank you. Your information was sent. You will be redirected shortly.</p>";
    header("refresh:2;url=https://restavio.com/1111_richards_st_vc/index.html");
} else {
    echo "mail send ... ERROR!";
    print_r(error_get_last());
}

简言之:PDF作为邮件附件时损坏无法打开。


内容的提问来源于stack exchange,提问作者Alexandru-Valentin Cubaniti

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 06:05:27