PHP 邮件脚本错误

发布于 2025-01-01 16:16:20 字数 2760 浏览 4 评论 0原文

我有一个提交页面,客户可以使用随附的邮件脚本扫描提交可填写的 PDF 表单和照片。表单正确上传到服务器,但是当它发送到客户端时,PDF 是空白的。

<?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 = "[email protected]";
  $from = "[email protected]"; 
  $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 .= "\nMIME-Version: 1.0\n";
  $headers .= "Content-Type: multipart/mixed;\n";
  $headers .= " boundary=\"{$mime_boundary}\""; 

  // part 1: define the plain text email
  $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";

  // 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\"};\n";
     $message .= " name=\"$file\"\n";
     $message .= "Content-Disposition: attachment;\n";
     $message .= " filename=\"$file\"\n";
     $message .= "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
     $message .= "--{$mime_boundary}\n";
  }

  // 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();
}
?>

谢谢,

杰比

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 = "[email protected]";
  $from = "[email protected]"; 
  $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 .= "\nMIME-Version: 1.0\n";
  $headers .= "Content-Type: multipart/mixed;\n";
  $headers .= " boundary=\"{$mime_boundary}\""; 

  // part 1: define the plain text email
  $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";

  // 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\"};\n";
     $message .= " name=\"$file\"\n";
     $message .= "Content-Disposition: attachment;\n";
     $message .= " filename=\"$file\"\n";
     $message .= "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
     $message .= "--{$mime_boundary}\n";
  }

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

○愚か者の日 2025-01-08 16:16:20
if(isset($_FILES) && (bool) $_FILES) {

是对成功上传的无效测试。 $_FILES 数组始终被设置,并且假设进行了文件上传尝试,该数组永远不会为空。

您应该检查如下错误:

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

错误代码在此处定义: http: //php.net/manual/en/features.file-upload.errors.php

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文