我正在尝试使用PHP mail()函数发送带附件的电子邮件。电子邮件正确通过,也收到附件。但是当我尝试打开附件时,会显示一条消息,指出文件未正确解码。
我已经复制了下面的代码。请帮我查一下我的代码中的错误。
$recieverAdd ='abc@gmail.com';
$subject ='Email with attachment';
$filename ='abc.pdf';
$file = '/home/abc/abc.pdf';
$file_size = filesize($file);
$handle = fopen($file, "rb");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($file);
$from_name = 'ABC';
$from_mail = 'admin@gmail.net';
$header = "From: $from_name";
$header .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$uid}\"";
$message="<p><b>Hello</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=\"{$filename}\" ".
"Content-Description: {$filename} " .
"Content-Disposition: attachment ".
"filename=\"{$filename2}\"; size={$file_size}; " .
"Content-Transfer-Encoding: base64\n\n". $content."\n\n". "--{$uid}--\n";
if(mail($recieverAdd,$subject,$messageBody,$header))
return true;
else
return false;