2017-03-05 34 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