我正在使用下面的代碼,它正在工作。但是附加的代碼不起作用,並且發送郵件失敗。這可能是做錯了頭?我正在嘗試將表單數據發送到pdf並附上郵件
這裏的$htmlTable
變量用於收集表單後值並使用html轉換爲pdf函數將其轉換爲pdf。 但郵件不是用pdf發送的。
$pdf->WriteHTML2("<br><br><br>$htmlTable");
$pdf->SetFont('Arial','B',6);
//Create PDF file with data
$semi_rand = md5(time());
$pdf1 = $htmlTable;
$file = $semi_rand . ".pdf";
$handle = fopen($file, 'w+');
fwrite($handle, $pdf);
fclose($handle);
// Additional headers
$subject = "User Form";
$content = chunk_split(file_get_contents($file));
$uid = md5(uniqid(time())); //unique identifier
//standard mail headers
$header = "From: ".$from."\r\n";
$header .= "Reply-To: ".$_POST['email']. "\r\n";
$header .= "MIME-Version: 1.0\r\n";
//declare multiple kinds of email (plain text + attch)
$header .="Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .="This is a multi-part message in MIME format.\r\n";
//plain txt part
//$header .= "--".$uid."\r\n";
// $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
// $header .= "Content-Transfer-Encoding: bit\r\n\r\n";
//$header .= 'sample'. "\r\n\r\n";
//attch part
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/octet-stream; name=\"".$file."\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$file."\"\r\n\r\n";
$header .= $content."\r\n\r\n"; //chucked up 64 encoded attch
$success = mail($to,$subject,'PFA',$header);
if (!$success) {
echo "Mail to " . $to . " failed .";
}else {
echo "Success : Mail was send to " . $to ;
print_r ($header);
}
你用[PHPMailer](https://github.com/PHPMailer/PHPMailer)標記了這個問題,但你沒有使用它。你應該,那麼你不必擔心如何正確構建附件。通常,一次解決一個問題 - 在嘗試發送PDF之前檢查您的PDF版本是否正在運行。另外,看看'file_put_contents',這比用fopen拖拽要簡單得多。 – Synchro
是的,我的pdf版本正在工作,但我遇到問題發送郵件附件... – sourabh
如何「附加代碼」工作和「下面的代碼」不工作? – symcbean