邮件到达时没有文件 php
Posted
技术标签:
【中文标题】邮件到达时没有文件 php【英文标题】:mail arrive without the file php 【发布时间】:2014-07-16 19:42:41 【问题描述】:下一页可以,但是提交时我没有收到带有邮件的文件。
收到的邮件:
MIME 版本:1.0 内容类型:多部分/混合; 边界="==Multipart_Boundary_xa18882382b8d92109533240902ace32ex"
--==Multipart_Boundary_xa18882382b8d92109533240902ace32ex 内容类型:文本/纯文本;字符集="iso-8859-1" 内容传输编码:7bit
...然后是输入的值($msg)
...然后
--==Multipart_Boundary_xa18882382b8d92109533240902ace32ex
**没有文件
<?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'];
$path_parts = pathinfo($file_name);
$ext = $path_parts['extension'];
if(!in_array($ext,$allowedExtensions))
die("File extensions not allowed");
$server_file = "/tmp/$path_parts[basename]";
move_uploaded_file($temp_name,$server_file);
array_push($files,$server_file);
$mail_to = 'dorozenman@gmail.com'; // specify your email here
// Assigning data from the $_POST array to variables
$name = $_POST['sender_name'];
$suname = $_POST['sender_suname'];
$Bday = $_POST['sender_Bday'];
$nowWork = $_POST['sender_work'];
$phone = $_POST['sender_phone'];
$mail_from = $_POST['sender_email'];
$free = $_POST['sender_way'];
// Construct email headers
$headers = 'From: ' . $mail_from . "\r\n";
// Construct email subject
$subject = 'בקשה לרעיון עבודה' . $name;
// Construct email body
$msg = 'name: ' . $name . "\r\n";
$msg .= 'suname: ' . $suname . "\r\n";
$msg .= 'Bday: ' . $Bday . "\r\n";
$msg .= 'nowWork: ' . $nowWork . "\r\n";
$msg .= 'phone: ' . $phone . "\r\n";
$msg .= 'mail_from: ' . $mail_from . "\r\n";
$msg .= 'free: ' . $free;
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x$semi_randx";
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n";
$headers .= " boundary=\"$mime_boundary\"";
$message ="\n\n--$mime_boundary\n";
$message .="Content-Type: text/plain; charset=\"iso-8859-1\"\n";
$message .="Content-Transfer-Encoding: 7bit\n\n" . $msg . "\n\n";
$message .= "--$mime_boundary\n";
foreach($files as $file)
$aFile = fopen($file,"rb");
$data = fread($aFile,filesize($file));
fclose($aFile);
$data = chunk_split(base64_encode($data));
$massage .= "Content-Type: \"application/octet-stream\";\n";
$massage .= " name=\"$file\"\n";
$massage .= "Content-Disposition: attachment;\n";
$massage .= " filename=\"$file\"\n";
$massage .= "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$massage .= "--$mime_boundary\n";
$mail_sent = mail($mail_to, $subject, $message, $headers);
if ($mail_sent) ?>
<script language="javascript" type="text/javascript">
alert('yay');
</script>
<?php else ?>
<script language="javascript" type="text/javascript">
alert('nay');
</script>
<?php
?>
怎么了?如果我没有从页面收到任何 php 警告,为什么我没有得到文件?
【问题讨论】:
简单:不要建立自己的 mime 电子邮件。所有生成 mime 的代码都可以简化为 SINGLE 行 PHPMailer 或 Swiftmailer 调用。 【参考方案1】:您在 if 语句中将您的 $_FILES (数组)类型转换为布尔值。你应该写 is_array($_FILES) 而不是 (bool) $_FILES,然后你应该得到你的文件。
【讨论】:
我把: if(is_array($_FILES) )... 换成 if(isset($_FILES) && (bool) $_FILES) 还是一样的结果以上是关于邮件到达时没有文件 php的主要内容,如果未能解决你的问题,请参考以下文章