我使用以下代码通过电子邮件附加文件,收到带有附件符号的电子邮件,但打开邮件时不包含任何内容。
$semi_rand = md5(time());
$from = "test@domainname.com";
$headers = "From: " . $fromEmailName . " ". "\r\n" ;
$headers .= "cc: " . $fromEmailName . " <" . $cc . ">" . "\r\n" ;
$boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers used for send attachment with email
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\" {$boundary}\"";
$message = "--{$boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content- Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$boundary}\n";
$fileatt = $_FILES["file"]["tmp_name"]; // Path to the tmp file
$fileatt_type = "text/plain"; // File Type
$fileatt_name = "upload.doc"; // Filename that will be used for the file as the attachment
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$fname\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$boundary}\n";
mail($to, $subject, $message, $headers, "-f " . $from)