我正在尝试发送一封电子邮件,其中包含从HTML表单完成的pdf表单。
我已经成功地获取了HTML输入值,并用这些值创建了一个PDF,并通过$PDF->输出((;使用fpdm,但我无法将其作为附件发送。现在我收到一封无法打开的PDF文件的电子邮件。
代码如下:
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 = "rn Strata Lot: 28 of Strata Plan: EPS 7373 rn Name(s) of tenant(s): " . $name . "rn Telephone: " . $usercell . "rn Email: " . $email . "rn Tenancy commencing: " . $userdeparture . "rn Is this unit rented to a family member? - " . $rentstatus . "rn Date: " . $datenow . "rn Address, Phone Number and Email Address of Landlord or Agent of Landlord: 103-1550 Duchess Ave, West Vancouver, B.C. V7V 1P5 rn Name of Landlord or Agent of Landlord: Donald Kim rn rn rn For more information, reply to this email and we will get back to you shortly. rn - 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 = "rn";
// main header (multipart mandatory)
$headers = "From: " . $fromname . " <" . $fromemail . ">" . $eol;
$headers .= 'Cc: acubaniti@gmail.com' . "rn";
$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());
}
作为TL;DR->PDF文件在尝试将其用作电子邮件中的附件时中断
我能够通过在本地保存pdf并在之后附加它来完成这项工作。
// encode data (puts attachment in proper format)
$pdfdoc = $pdf->Output("", $filename);
$content = file_get_contents($filename);
$content = chunk_split(base64_encode($content));
当然,我的原始代码在传递字符串时有问题,可能我不知道如何传递内容。这只是一个简单的解决方案,可以满足我的需求,当然还有一种更优雅的方法可以做到这一点,比如CBroe说的PHPMailer。
如果你使用这个代码。。。?
// encode data (puts attachment in proper format)
$pdfdoc = $pdf->Output("S");
$attachment = chunk_split(base64_encode($pdfdoc));
您在使用";输出";函数,你不是。。。?