0

这是用于发送附有 doc 文件的电子邮件的代码,正在接收电子邮件,但电子邮件中的内容以编码文本格式显示

<?php

$to = $_POST['to'];
$from = $_POST['from'];
$subject = $_POST['subject'];
$message = $_POST['message'];


$fileatt = $_FILES['upload_file']['tmp_name'];
$fileatt_type = $_FILES['upload_file']['type'];//application/msword
$fileatt_name = $_FILES['upload_file']['name'];

$headers = "From: $from";

if (is_uploaded_file($fileatt)) {

$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);


$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";


$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";


$data = chunk_split(base64_encode($data));


$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
}

mail($to, $subject, $message, $headers);
?>

我收到的电子邮件是这样的

message:This is a multi-part message in MIME format. --==Multipart_Boundary_x788f8ac40875c8c7a6540be6654be9c8x Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit --==Multipart_Boundary_x788f8ac40875c8c7a6540be6654be9c8x Content-Type: application/msword; name="File_name.doc" Content-Disposition: attachment; filename="File_name.doc" Content-Transfer-Encoding: base64 s pcEAW4AJBAAA+BK/AAAAAAAAEAAAAAAACAAAMhMAAA4AYmpiaqz6rPoAAAAAAAAAAAAAAAAAAAAA AAAJBBYAuEAAAM6QAQDOkAEAFwsAAAAAAAAaAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD//w8AAAAA AAAAAAD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AzSwAAAAAAAIwYAABQAgAAM0sAAEgAAAAqQgAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAADNLAAAAAAAAAAAAAAAAAACLFAAAAAAAACpCAADQAgAAXhcAAAAAAABeFwAAAAAAANwa AAAAAAAAXhcAAAAAAABeFwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAXhcA AAAAAABeFwAAAAAAAF4XAAAAAAAANEUAAAAAAAA0RQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA  AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAA== --==Multipart_Boundary_x788f8ac40875c8c7a6540be6654be9c8x--

headers:From:  MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="==Multipart_Boundary_x788f8ac40875c8c7a6540be6654be9c8x"

所以任何人都可以帮助我一个完美的代码提前谢谢你

4

2 回答 2

0

通过真正调试手头的问题而不是猜测自己找出原因:

  1. 将您生成的消息内容转储到文本文件中。
  2. 将您在电子邮件客户端中收到的任何有效消息内容转储到文件中(消息源代码)。
  3. 比较两个文件并从差异中得到答案。

这确实有效。

于 2013-04-15T05:24:54.163 回答
0

PHP 不支持默认的 utf-8 编码,您需要将下面给出的相关编码函数传递给您的 headers

Replace your code 

Content-Type: text/plain; charset=\"iso-8859-1\"\n

to

"Content-Type:text/html;charset=utf-8"
于 2013-04-15T05:32:16.407 回答