我有这段PHP代码,虽然它有效,但问题是电子邮件内容是作为附件与预期附件一起发送的,这意味着当电子邮件到达时,正文是空白但有两个附件。一个是预期的附件,另一个是应该在电子邮件中显示的电子邮件内容。有什么我做错了吗?
以下是完整的源代码:
<?php
require('fpdf/fpdf.php');
include('./config.php');
$pdf = new FPDF('P', 'pt', 'Letter');
$pdf->SetAutoPageBreak(true, $margin);
$pdf->AddPage();
$pdf->SetFont('arial','',12);
if(isset($_POST['accept'])){
$id = $_POST['id'];
$to = 'me@somewhere.com';
$subject = urldecode($_POST['subject']);
$message = urldecode($_POST['message']);
$data = '';
$result = mysql_query("select * from applications where id = $id");
$row = $data_array = mysql_fetch_assoc($result);
$data .= "Name: " . $row['name'] . " \n\n";
$data .= "Surname: " . $row['surname'] . "\n\n";
$data .= "Age: ". $row['age'] . "\n\n";
$data .= "Dob: ". $row['dob'] . "\n\n";
$data .= "Height: ". $row['height'] . "\n\n";
$data .= "Add1 : ".$row['address1'] . "\n\n";
$data .= "Add2: ".$row['address2'] . "\n\n";
$data .= "Add3: ".$row['address3'] . "\n\n";
$data .= "Postcode: ".$row['postcode'] . "\n\n";
$data .= "Town: ".$row['town'] . "\n\n";
$data .= "County: ". $row['county']. "\n\n";
$pdf->SetX(140);
$pdf->Ln(2);
$pdf->MultiCell(0, 15, $data);
$from = "me@mydomain.com";
$separator = md5(time());
$eol = PHP_EOL;
// attachment name
$filename = "form.pdf";
// encode data (puts attachment in proper format)
$pdfdoc = $pdf->Output("", "S");
$attachment = chunk_split(base64_encode($pdfdoc));
// main header
$headers = "From: ".$from.$eol;
$headers .= "MIME-Version: 1.0".$eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"";
$body = "--".$separator.$eol;
$body .= "Content-Transfer-Encoding: 7bit".$eol.$eol;
// message
$body .= "--".$separator.$eol;
$body .= "Content-Type: text/html; charset=\"iso-8859-1\"".$eol;
$body .= "Content-Transfer-Encoding: 8bit".$eol.$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 message
if(mail($to, $subject, $body, $headers)){
echo "<b>Email successfully sent</b>";
}
else{
echo "Your message could not be sent.";
}
}
?>
任何帮助都将受到高度赞赏。
答案 0 :(得分:1)
我实际上有一些从头开始单手写这种东西的经验。经过多年维护家庭酿造的PHP电子邮件构建器,我的建议是你不应该。 肯定花费更少的时间和资源来研究和找到当今最受支持的主流解决方案(无论何时何地),而不是自己编写(以及用你的时间支付所有固有的错误) /或资源)。