为什么我的CSV文件为空

时间:2017-03-05 23:20:44

标签: php forms csv email-attachments

所以我有这个PHP脚本,在用户将数据输入到收集数据的表单中,然后将其放入.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));
}

抱歉我的格式化第一篇文章:)

1 个答案:

答案 0 :(得分:0)

file_get_contents需要文件路径作为参数,而不是文件指针。 尝试将其更改为

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