我有一个表单,用户可以编辑某组信息并将文件附加到所述信息。如果文件已上传,我需要通过电子邮件将其发送给其他人。我有以下功能来处理发送带附件的电子邮件:
function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message)
{
$file = $path.$filename;
$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);
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n";
// use different content types here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
mail($mailto, $subject, $message, $header);
}
函数DOES发送一封电子邮件(附件文件名正确),但邮件附带的文件总是几百字节。例如,我尝试通过上传一个45 kb的pdf文件进行测试,该函数发送了一封带有232字节pdf文件的电子邮件。我尝试使用$ _SERVER ['DOCUMENT_ROOT']作为路径以及本地路径(即./uploads /)......两者都得到相同的结果。
有没有人看到一些我不知道的东西......或者可能不那么明显?