I am trying to send email with attachment using PHP mail() function. The email gets delivered but in web mail filename appears as “untitle”. With Outlook Avast heuristic blocks email completely.
Please guide me to overcome this issue.
Here is code that I am using
$recieverAdd = 'r@gamil.com';
$subject = 'Attachment';
$attDescription="emailing lessons";
$filename ='example.pdf';
$file = '/home/user/example.pdf';
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($file);
$from_name = $uname;
$from_mail = 'admin@gmail.com';
$replyto ='noreply@gmail.com';
$header = "From: $from_name";
$header .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$uid}\"";
$message = "<p><b>Hello </b></p>";
$messageBody = "--{$uid}\n" . "Content-Type: text/html; charset=\"iso-8859-1 \"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$messageBody .= "--{$uid}\n". "Content-Type: application/octet-stream;\n".
"name=\"{$filename1}\"\n".
"Content-Description: {$filename}\n" .
"Content-Disposition: attachment;\n".
"filename=\"{$filename}\"; size={$file_size};\n" .
"Content-Transfer-Encoding: base64\n\n". $content."\n\n". "--{$uid}--\n";
mail($recieverAdd,$subject,$messageBody,$header)
The
name=...andfilename=...parts of these headers are considered separate (invalid) header lines, because you’re outputting a newline afterContent-Type: application/octet-stream;andContent-Disposition: attachment;, but no whitespace after those newlines. In order to split a header into multiple lines, each continuation line must start with whitespace.