【问题标题】:PHP attachment form after submitting getting blank page提交后获得空白页面的PHP附件表单
【发布时间】:2016-12-25 04:25:39
【问题描述】:

提交我的表单后,我收到了邮件,但它会变成白色的黑屏,并显示消息发送消息。

我在提交表单后尝试重新加载页面但出现错误。

<?php
if ($_SERVER['REQUEST_METHOD'] == "POST") {

  $to = "name@gmail.com";
  $subject = "E-mail with attachment";
  $from = stripslashes($_POST['fromname']) . "<" . stripslashes($_POST['fromemail']) . ">" . "<" . stripslashes($_POST['designation']) . ">";

  // generate a random string to be used as the boundary marker
  $mime_boundary = "==Multipart_Boundary_x" . md5(mt_rand()) . "x";

  // now we'll build the message headers
  $headers = "From: $from\r\n" .
      "MIME-Version: 1.0\r\n" .
      "Content-Type: multipart/mixed;\r\n" .
      " boundary=\"{$mime_boundary}\"";

  $message = "Canditade Resume";
  // when we use it
  $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";

  // iterating each File type
  print_r($_FILES);

  foreach ($_FILES as $userfile) {

    $tmp_name = $userfile['tmp_name'];
    $type = $userfile['type'];
    $name = $userfile['name'];
    $size = $userfile['size'];


    if (file_exists($tmp_name)) {
      if (is_uploaded_file($tmp_name)) {
        $file = fopen($tmp_name, 'rb');
        $data = fread($file, filesize($tmp_name));
        fclose($file);
        $data = chunk_split(base64_encode($data));
      }
      $message .= "--{$mime_boundary}\n" .
          "Content-Type: {$type};\n" .
          " name=\"{$name}\"\n" .
          "Content-Disposition: attachment;\n" .
          " filename=\"{$tmp_name}\"\n" .
          "Content-Transfer-Encoding: base64\n\n" .
          $data . "\n\n";
    }
  }
  // here's our closing mime boundary that indicates the last of the message
  $message.="--{$mime_boundary}--\n";
  // now we just send the message
  if (@mail($to, $subject, $message, $headers))
    echo "Message Sent";
  else
    echo "Failed to send";
} else {
  ?>
  <form action="index.php" method="post"    enctype="multipart/form-data" name="form1">
      <label>Name</label>
      <input type="text" name="fromname" class="input-block-level" style="width: 100%" required placeholder="Your First Name">
      <label>Email Address</label>
      <input type="text" style="width: 100%" class="input-block-level" required placeholder="Your email address" name="fromemail">
      <label>Designation</label>
      <input type="text" style="width: 100%" class="input-block-level" name="designation" required placeholder="Designation">
      <label>Upload Your CV</label>
      <input type="file" class="input-block-level" required placeholder="Upload Your CV" name="file1">
      </div>
      <input type="submit" name="Submit" value="Submit" class="btn btn-primary btn-large pull-left" onclick="javascript: form.action='index.php';">
  </form>
<?php } ?>

请帮帮我

【问题讨论】:

  • 你想达到什么目的?哪些错误是您意想不到的?
  • 表单工作正常,提交后我想验证表单然后想重定向到其他页面
  • 请用您现在拥有的代码更新您问题中的代码。

标签: php forms attachment


【解决方案1】:

header location 中提供您要重定向到的页面名称。

改变

if (@mail($to, $subject, $message, $headers))
    echo "Message Sent";
  else
    echo "Failed to send";
} else {

if (@mail($to, $subject, $message, $headers))
    header("location:yourpage.php?message=Message Sent");
  else
    header("location:yourpage.php?message=Failed to send");
} else {

yourpage.php

<?php
if(isset($_GET['message'])){
  echo $_GET['message'];
}
.
.

?>

【讨论】:

    【解决方案2】:
    if (@mail($to, $subject, $message, $headers))
        //echo "Message Sent";
        header('location:email-success.php');
      else
        //echo "Failed to send";
        header('location:email-error.php');
    }
    

    【讨论】:

    • 仍然出现这个错误
    • Array ([file1] => Array ([name] => sendemail.php [type] => application/octet-stream [tmp_name] => /tmp/phphNCMKH [error] => 0 [大小] => 801 ) )
    • 这不是错误,那是你的 print_r 语句print_r($_FILES); !删除该行,它将起作用。
    • 我只在白屏后删除的print_r,我想重定向其他页面
    【解决方案3】:

    您的编码有点混乱。这是我所做的调整:

    我还注释掉了 print_r($_FILES),因为这将显示已通过的数组,当用户按下提交时您不想显示该数组。

    所以我重命名了 $_POST 并将其设为 isset:

    <?php
    if (isset($_POST['send'])) {
    
        $to = "name@gmail.com";
        $subject = "E-mail with attachment";
        $from = stripslashes($_POST['fromname']) . "<" . stripslashes($_POST['fromemail']) . ">" . "<" . stripslashes($_POST['designation']) . ">";
    
        // generate a random string to be used as the boundary marker
        $mime_boundary = "==Multipart_Boundary_x" . md5(mt_rand()) . "x";
    
        // now we'll build the message headers
        $headers = "From: $from\r\n" .
            "MIME-Version: 1.0\r\n" .
            "Content-Type: multipart/mixed;\r\n" .
            " boundary=\"{$mime_boundary}\"";
    
        $message = "Canditade Resume";
        // when we use it
        $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";
    
        // iterating each File type
        //print_r($_FILES);
    
        foreach ($_FILES as $userfile) {
    
            $tmp_name = $userfile['tmp_name'];
            $type = $userfile['type'];
            $name = $userfile['name'];
            $size = $userfile['size'];
    
    
            if (file_exists($tmp_name)) {
                if (is_uploaded_file($tmp_name)) {
                    $file = fopen($tmp_name, 'rb');
                    $data = fread($file, filesize($tmp_name));
                    fclose($file);
                    $data = chunk_split(base64_encode($data));
                }
                $message .= "--{$mime_boundary}\n" .
                    "Content-Type: {$type};\n" .
                    " name=\"{$name}\"\n" .
                    "Content-Disposition: attachment;\n" .
                    " filename=\"{$tmp_name}\"\n" .
                    "Content-Transfer-Encoding: base64\n\n" .
                    $data . "\n\n";
            }
        }
        // here's our closing mime boundary that indicates the last of the message
        $message .= "--{$mime_boundary}--\n";
        // now we just send the message
        if (@mail($to, $subject, $message, $headers)) {
            echo "Message Sent";
        } else {
            echo "Failed to send";
        }
    }
        ?>
    

    不知道为什么您将 PHP 包裹在 html 端,但这就是为什么您没有被带回表单端而只是一个空白页面的原因。

     <form action="" method="post"    enctype="multipart/form-data" name="form1">
            <label>Name</label>
            <input type="text" name="fromname" class="input-block-level" style="width: 100%" required placeholder="Your First Name">
            <label>Email Address</label>
            <input type="text" style="width: 100%" class="input-block-level" required placeholder="Your email address" name="fromemail">
            <label>Designation</label>
            <input type="text" style="width: 100%" class="input-block-level" name="designation" required placeholder="Designation">
            <label>Upload Your CV</label>
            <input type="file" class="input-block-level" required placeholder="Upload Your CV" name="file1">
            </div>
            <input type="submit" name="send" value="Submit" class="btn btn-primary btn-large pull-left" onclick="javascript: form.action='index.php';">
        </form>
    

    我已经测试了这段代码,它会重定向回表单,并在表单上方显示“已发送消息”(正如您在任何联系表单上看到的那样)。

    【讨论】:

    • 你真的应该为每个字段添加条件if (!empty($var)) { // do something },因为表单必填字段很容易被绕过。
    • 感谢您的效果,现在它的重定向但附件文件无法发送到邮件
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-04
    相关资源
    最近更新 更多