Reputation: 61
I have below code
$files = array('/home/my_scripts/my_csvfiles/Multiple_Tracking/Multiple_Tracking_Result_Output_'.$date.'.csv','/home/my_scripts/my_csvfiles/Multiple_Tracking/Multiple_Tracking_Result_Not_match_Output_'.$date.'.csv');
$to = "[email protected]";
$from = "[email protected]";
$subject ="Multiple Tracking Data Checked Result";
$message = "PFA for Multiple Tracking Data Sheet Checked by Script. Date - " . $date ;
$headers = "From: $from";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$mime_boundary}\n";
// preparing attachments
for($x=0;$x<count($files);$x++){
$file = fopen($files[$x],"rb");
$data = fread($file,filesize($files[$x]));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}\n";
}
// send
$ok = @mail($to, $subject, $message, $headers);
if ($ok) { echo "Mail sent to $to!";}
else { echo "Mail could not be sent!"; }
but it is sending complete file path as file name in attachments.
As I have mention absolute path in $files
array. But whenever I put relative path it in array it send empty csv files in email attachment.
I just want to send file name in attachment. so is there any solution I can get only file name in with proper data in mail attachment.
Upvotes: 1
Views: 2009
Reputation: 745
Get filename:
$info = new SplFileInfo($files[$x]);
$fileName = $info->getFilename();
And put it on:
"Content-Disposition: attachment;\n" . " filename=\"$fileName\"\n" .
Upvotes: 1