我有這段PHP代碼,雖然它可以工作,但問題在於電子郵件內容作爲附件發送以及打算的附件,這意味着當電子郵件到達時,是空白的,但有兩個附件。一個是預定的附件,另一個是應該在電子郵件中顯示的電子郵件內容。有什麼我做錯了嗎?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 = '[email protected]';
$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 = "[email protected]";
$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.";
}
}
?>
任何幫助將得到高度讚賞。
如果你沒有足夠的信心來實現手動發送電子郵件和/或調試它 - 爲什麼不使用那些對你來說很好的庫?喜歡phpmailer – zerkms
我第二@zerkms,你最好只使用庫來做你的發送,除非你是誰必須直接控制它。 [PHPMailer](http://sourceforge.net/projects/phpmailer/) –
phpmailer是使用php發送郵件的最簡單方法。我一直都在使用它,認爲它比手動更簡單。 – Ishikawa