1

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.

2
  • You could try using PHPMailer to do this for you, since you've tagged the question with it. It's much easier and more reliable than doing it yourself. Commented Aug 10, 2016 at 9:26
  • I am using magento. I an this is custom scripts for Reports. I dont want to install extra plugin like phpmailer. Commented Aug 10, 2016 at 9:30

1 Answer 1

1

Get filename:

$info = new SplFileInfo($files[$x]);
$fileName = $info->getFilename();

And put it on:

"Content-Disposition: attachment;\n" . " filename=\"$fileName\"\n" . 
Sign up to request clarification or add additional context in comments.

2 Comments

it is not working. it send same name with absolute path. I also tried to print $fileName it shows nothing.
Thanks its working. My mistake I was putting code outside for loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.