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 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

    点赞 评论 复制链接分享

相关推荐