0

I'm trying to set name of a file when i send it using mail() function, i send it but in gmail what i received is noname file without extension.

Could anyone help me please?

<?php


$temp = tmpfile();

fwrite($temp, $ical);
rewind($temp);
$to_email = 'test@gmail.com';
$subject = 'Testing PHP Mail';
$message = 'Hola caracola';
$from_email = 'test@test.com';
$path = stream_get_meta_data($temp)['uri'];
$num = md5(time());

$headers = "MIME-Version: 1.0\r\n"; // Defining the MIME version 
$headers .= "From:".$from_email."\r\n"; // Sender Email 
$headers .= "Reply-To: ".$from_email."\r\n"; // Email addrress to reach back 
$headers .= "Content-Type: multipart/mixed;\r\n"; // Defining Content-Type 

$body ="Content-Type: text/calendar; name=invite.ics\r\n"; 
$body .="Content-Disposition: attachment; filename=\"invite.ics\"\r\n";  
$body .= $content; // Attaching the encoded file with email 
    
mail($to_email,$subject,$body,$headers);
    
fclose($temp);

} ?>

Thankyou

josectual
  • 1
  • 2

1 Answers1

0

I just made it, I paste the code below, the problem is that php applies new policies to new lines headers so i just change a bit the code to avoid this limitation and solved the problem:

<?php
$temp = tmpfile();

fwrite($temp, $ical);
rewind($temp);
$to_email = 'test@gmail.com';
$subject = 'Testing PHP Mail';
$message = 'Hello';
$from_email = 'test@test.com';
$path = stream_get_meta_data($temp)['uri'];

$eol = PHP_EOL;

$filename = "invite.ics";
$mailto = $to_email;
$from_mail = $from_email;
$from_name = "Events";
$replyto = $from_email;

$file = $path;
$file_size = filesize($path);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$header = "From: ".$from_name." <".$from_mail.">".$eol;
$header .= "Reply-To: ".$replyto.$eol;
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"";

$message = "--".$uid.$eol;
$message .= "Content-Type: application/pdf; name=\"".$filename."\"".$eol; // use different content types here
$message .= "Content-Transfer-Encoding: base64".$eol;
$message .= "Content-Disposition: attachment; filename=\"".$filename."\"".$eol;
$message .= $content.$eol;
$message .= "--".$uid."--";

mail($mailto, $subject, $message, $header);
    
fclose($temp);
?>
josectual
  • 1
  • 2