doubiaozhan0745 2012-02-01 21:20
浏览 41
已采纳

PHP邮件脚本错误

I have an submit page where client scan submit a fillable PDF form along with a photo, using the attached mailer script. The form uploads to the server correctly, however when it is sent to the client, the PDF is blank.

<?php
// did files get sent
if(isset($_FILES) && (bool) $_FILES) {

  // define allowed extensions
  $allowedExtensions = array("pdf","doc","docx","gif","jpeg","jpg","png","rtf","txt");
  $files = array();

  // loop through all the files
  foreach($_FILES as $name=>$file) {

     // define some variables
     $file_name = $file['name']; 
     $temp_name = $file['tmp_name'];

     // check if this file type is allowed
     $path_parts = pathinfo($file_name);
     $ext = $path_parts['extension'];
     if(!in_array($ext,$allowedExtensions)) {
        die("extension not allowed");
     }

     // move this file to the server YOU HAVE TO DO THIS
     $server_file = "/home/castmeb1/public_html/uploads/$path_parts[basename]";
     move_uploaded_file($temp_name,$server_file);

     // add this file to the array of files
     array_push($files,$server_file);
  }  

  // define some mail variables
  $to = "castmebg@gmail.com";
  $from = "castmebg@gmail.com"; 
  $subject ="test attachment"; 
  $msg = "Please see attached";
  $headers = "From: $from";

  // define our boundary
  $semi_rand = md5(time()); 
  $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

  // tell the header about the boundary
  $headers .= "
MIME-Version: 1.0
";
  $headers .= "Content-Type: multipart/mixed;
";
  $headers .= " boundary=\"{$mime_boundary}\""; 

  // part 1: define the plain text email
  $message ="

--{$mime_boundary}
";
  $message .="Content-Type: text/plain; charset=\"iso-8859-1\"
";
  $message .="Content-Transfer-Encoding: 7bit

" . $msg . "

";
  $message .= "--{$mime_boundary}
";

  // part 2: loop and define mail attachments
  foreach($files as $file) {
     $aFile = fopen($file,"rb");
     $data = fread($aFile,filesize($file));
     fclose($aFile);
     $data = chunk_split(base64_encode($data));
     $message .= "Content-Type: {\"application/octet-stream\"};
";
     $message .= " name=\"$file\"
";
     $message .= "Content-Disposition: attachment;
";
     $message .= " filename=\"$file\"
";
     $message .= "Content-Transfer-Encoding: base64

" . $data . "

";
     $message .= "--{$mime_boundary}
";
  }

  // send the email
  $ok = mail($to, $subject, $message, $headers); 
  if ($ok) { 
     echo "<p>mail sent to $to!</p>"; 
  } else { 
     echo "<p>mail could not be sent!</p>"; 
  }
  die();
}
?>

Thanks,

JB

  • 写回答

1条回答 默认 最新

  • dss67853 2012-02-01 21:26
    关注
    if(isset($_FILES) && (bool) $_FILES) {
    

    is an invalid test for a successful upload. The $_FILES array is always set, and assuming a file upload ATTEMPT was made, the array will NEVER be empty.

    You should check for errors like this:

    if ($_FILES['name_of_file_field']['error'] === UPLOAD_ERR_OK) {
       ... file was successfully uploaded ...
    }
    

    The error codes are defined here: http://php.net/manual/en/features.file-upload.errors.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?