Php 邮件附件问题-为什么我不能正确获取文件?
我尝试创建一个表单,其中包含上传文件并将其发送到电子邮件的选项。 例如,用户可以将其信息放入输入字段并添加一个文件,当用户提交时,我会将其全部发送到我的邮件中 这是我的代码:Php 邮件附件问题-为什么我不能正确获取文件?,php,forms,email,file-upload,attachment,Php,Forms,Email,File Upload,Attachment,我尝试创建一个表单,其中包含上传文件并将其发送到电子邮件的选项。 例如,用户可以将其信息放入输入字段并添加一个文件,当用户提交时,我会将其全部发送到我的邮件中 这是我的代码: <?php if(isset($_FILES) && (bool) $_FILES) { $allowedExtensions = array("pdf","doc","docx","gif","jpeg","jpg","png","rtf","txt"); $files = array();
<?php
if(isset($_FILES) && (bool) $_FILES) {
$allowedExtensions = array("pdf","doc","docx","gif","jpeg","jpg","png","rtf","txt");
$files = array();
foreach($_FILES as $name=>$file) {
$file_name = $file['name'];
$temp_name = $file['tmp_name'];
$file_type = $file['type'];
$path_parts = pathinfo($file_name);
$ext = $path_parts['extension'];
if(!in_array($ext,$allowedExtensions)) {
die("File $file_name has the extensions $ext which is not allowed");
}
array_push($files,$temp_name);
}
// email fields: to, from, subject, and so on
$to = "dorozenman@gmail.com";
$from = $_POST['sender_email'];
$subject ="test attachment";
$message = "here ya go";
$headers = "From: $from";
// 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/plain; 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
$ok = @mail($to, $subject, $message, $headers);
if ($ok) {
echo "<p>mail sent to $to!</p>";
} else {
echo "<p>mail could not be sent!</p>";
}
}
?>
(文件名为…tmp/phpcVjk4w/)有什么建议吗?这绝对正常 基本上,文件被上传到您的服务器上,PHP将它们临时存储在
/tmp/
目录中
$files = array();
foreach($_FILES as $name=>$file) {
$temp_name = $file['tmp_name'];
array_push($files,$temp_name);
}
在上面,您将该临时名称放入数组,在下面,您将该名称放入邮件:
for($x=0;$x<count($files);$x++){
$message .= name=\"$files[$x]\"; //...
}
对于($x=0;$x更改:
array_push($files,$temp_name);
致:
和变化:
// 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";
}
//准备附件
对于($x=0;$XOBLIGATOY对其他人的警告:不要复制和粘贴如此糟糕的手动MIME/附件构造代码。有很多方法可以大大简化此类任务。首先感谢您的帮助,第二,我应该将to函数放置在上面的确切位置?第三,我希望脚本将文件上载到我的服务器并发送到我的邮件在这种情况下我建议先将文件上传到服务器,然后从保存的位置将其附加到电子邮件。谢谢你!通过上面的帮助开始工作,我也感谢你的努力!!文件到达(具有良好的逻辑名称,但无法打开)还有其他的问题…对不起,兄弟,我的错误,它起作用了!!!!!!!!!!!!!!!!!!!!!!!!!!!!!你是最好的!!谢谢你!!!!!!!!
// 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";
}
// preparing attachments
for($x=0;$x<count($files);$x++){
$temp_name = $files[$x]['temp_name']; // temporary file location
$file_name = $files[$x]['file_name']; // filename
$file = fopen($temp_name,"rb");
$data = fread($file,filesize($temp_name));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$file_name\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$file_name\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}\n";
}