【发布时间】:2014-04-03 13:28:12
【问题描述】:
我是 php 的大三学生。现在我尝试通过 php mail() 发送一些附件,但是当我要阅读邮件时,它仅以文本形式显示(请参阅此处)。附件内容也是空白的。我使用在互联网上找到的邮件表格。我认为错误在于 /r/n 或 /n 之类的东西。对不起床英语。如果有问题,我可以解释更多,等待您的帮助!抱歉发布了太多代码,但我认为有必要了解...
表格
<form action="mailme.php" method="post" enctype="multipart/form-data">
-+-+- other inputs and select -+-+-
<input name="upload[]" type="file">
<input name="upload[]" type="file">
...other file attachments like that above
</form>
PHP
Retrive variables and then
$headers ="From: ".$SenderName." <".$from.">\n";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . "
boundary=\"{$mime_boundary}\"";
// multipart boundary
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n"
. "Content-Type: text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$mime_boundary}\n";
// preparing attachments
for($x=0;$x<count($files);$x++){
$file = fopen($files[$x],"rb");
$data = fread($file,filesize($files[$x]));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" .
" name=\"$files[$x]\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}\n";
}
// send
$success = @mail($to, $subject, $message, $headers);
if ($success) {
echo "<p>mail sent to $to!</p>";
} else {
print_r($files);
echo "<p>mail could not be sent!</p>".$from.'<br>'.$message.'<br>'.$to.'<br>'.$subject.'<br>'.$SenderName;
}
输出
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="==Multipart_Boundary_x553132b5cf2c4c84ecdbd3285b8b590dx"
This is a multi-part message in MIME format.
--==Multipart_Boundary_x553132b5cf2c4c84ecdbd3285b8b590dx
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Message <b>with<b> some <a href="html">html</a>!
--==Multipart_Boundary_x9351bff6284cb1fc967281cfede5762cx
Content-Type: {"application/octet-stream"};
name=""
Content-Disposition: attachment;
filename=""
Content-Transfer-Encoding: base64
.....BLANK CONTENT! LIKE NO IMAGE WAS ATTACHED......
--==Multipart_Boundary_x553132b5cf2c4c84ecdbd3285b8b590dx
【问题讨论】:
-
没有想法?发送带有多个附件的电子邮件的另一个教程或其他东西?可能是表单样式不对……
-
$files来自哪里? -
抱歉耽搁了 $files = $_FILES["upload"];
-
对吗?还是我必须使用其他东西?
标签: php html email mime-types