so i have this php script that after the user inputs data into a form that data is collected and some it is put into a .csv file and then sent as an attachment to the user, but i can get the email to send with the attachment but the .csv file is always empty and i dont know why heres my code
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
"
. "Content-Type: text/html; charset=ISO-8859" . "
"
. "
"
. "$body" . "
"
. "--$multipartSep" . "
"
. "Content-Type: text/csv" . "
"
. "Content-Transfer-Encoding: base64" . "
"
. "Content-Disposition: attachment; filename=\"New_Parts_and_Barcodes_Request.csv\"" . "
"
. "
"
. "$attachment" . "
"
. "--$multipartSep--";
return @mail($to, $subject, $newBody, implode("
", $headers));
}
sorry about my formatting first post :)
file_get_contents
requires file path as argument, not a file pointer. Try to change it into
$attachment = chunk_split(base64_encode(file_get_contents("php://temp")));