2017-03-05 77 views
0

所以我有這個PHP腳本,用戶將數據輸入到表單後收集數據並將其放入.csv文件,然後作爲附件發送給用戶,但我可以得到電子郵件與附件,但.csv文件發送始終是空的,我不知道爲什麼我的繼承人代碼爲什麼我的CSV文件是空的

function send_csv_mail($csvData, $body, $to, $subject, $from) 
{ 
    $multipartSep = '-----' . md5(time()) . '-----'; 

    $headers = [ 
     "From: $from", 
     "Reply-To: $from", 
     "Content-Type: multipart/mixed; boundary=\"$multipartSep\"", 
    ]; 

    $fp = fopen('php://temp', 'w'); 

    fputcsv($fp, ["Supplier Number", "Supplier Name", "Product Code", "Product Description", "Unit of Measure", "Product Group", "Buy Price EX GST"]); 

    $testData = [ 
     ['123', '456', '789', '012', '345', '678', '901'], 
     ['abc', 'def', 'ghi', '123', '456', '789', '012'], 
     ['jkl', 'mno', 'pqr', '123', '456', '789', '012'], 
    ]; 

    foreach ($testData as $data) { 

     fputcsv($fp, $data); 
    } 

    fclose($fp); 

    $attachment = chunk_split(base64_encode(file_get_contents($fp))); 

    $newBody = "--$multipartSep\r\n" 
     . "Content-Type: text/html; charset=ISO-8859" . "\r\n" 
     . "\r\n" 
     . "$body" . "\r\n" 
     . "--$multipartSep" . "\r\n" 
     . "Content-Type: text/csv" . "\r\n" 
     . "Content-Transfer-Encoding: base64" . "\r\n" 
     . "Content-Disposition: attachment;    filename=\"New_Parts_and_Barcodes_Request.csv\"" . "\r\n" 
     . "\r\n" 
     . "$attachment" . "\r\n" 
     . "--$multipartSep--"; 

    return @mail($to, $subject, $newBody, implode("\r\n", $headers)); 
} 

對不起我的格式化的第一篇文章:)

回答

0

file_get_contents需要作爲參數文件路徑,而不是文件指針。 試圖去改變它變成

$attachment = chunk_split(base64_encode(file_get_contents("php://temp")));

+0

好,我取得了你的建議的改變,但仍然沒有得到通過,任何其他的想法 – Dath

+0

你可以嘗試移動'FCLOSE($ FP)的數據,到'腳本結尾? –

+0

fclose($ fp)已移至腳本結尾處的返回之前。我再次嘗試,但仍然沒有骰子。 – Dath